1

In typescript, the following compiles.

class Person {
  name : string;
}

class Employee {
  name : string;
}

var person : Person = new Employee();

Why does that work?? This does not:

class Person {
  name : string;
  surname : string;
}

class Employee {
  name : string;
}

var person : Person = new Employee();

With the exception that:

Type employee is not assignable to type Person, the property surname is missing in type Employee

So. Types are compared at compile time using property names??!

Edit: Yep. This is the code from the typescript compiler doing the checking.

            for (var i = 0; i < targets.length; i++) {
                var related = isRelatedTo(sources[i], targets[i], reportErrors);
                if (!related) {
                    return 0;
                }
                result &= related;
            }
            return result;
1
  • 2
    I was about to write an answer, but there where inaccuracies in there because i rarly use TypeScript, so I'll just post a link to Type Compatibility [...]Type compatibility in TypeScript is based on structural subtyping. Structural typing is a way of relating types based solely on their members. This is in contrast with nominal typing.[...] Commented Feb 16, 2016 at 13:36

1 Answer 1

4

Effectively, Typescript determines types of objects based on their 'shape' - if two classes have the same members and their types and accessibility match, they're said to be compatible and can be used interchangeably. The same goes for functions. As t.niese mentioned in their comment, you can find more information of the reasoning behind this and the implications in the TypeScript handbook. It basically stems from the fact that JavaScript makes heavy use of anonymous functions and objects, so having a stricter type system may have made using existing JS libraries from within TypeScript an awkward task.

Sign up to request clarification or add additional context in comments.

4 Comments

This is colloquially known as "Duck Typing".
Sort of - based on my limited understanding of the topic, fully 'duck typed' languages don't care what shape an object has at all, as long as it can fulfill what's being asked of it at runtime (JavaScript, for example). Since TypeScript throws compile errors if you use an object of the wrong shape, it can't really be fully classed as duck typing; I imagine this is why they use the term 'structural typing' in the handbook. That said, it basically becomes duck typed if you ignore the error messages :p
A further reason for ducktyping: in the end you'll get pure JavaScript, nothing else. If it runs correctly as JS then it's good.
Just to clarify - this is not a limitation because Javascript doesn't support it. I "fixed" it in the typescript compiler so it throws a compiler error if the types aren't the same... it's trivial. This is a limitation to support old javascript libraries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.