3 ms·
It just means that structural types are the default and you need to opt in to nominal types, when necessary: https://www.typescriptlang.org/play/?ssl=13&ssc=1&p
by potatoz2 7y ago
It just means that structural types are the default and you need to opt in to nominal types, when necessary: https://www.typescriptlang.org/play/?ssl=13&ssc=1&pln=13&pc=22#code/C4TwDgpgBAIg9gcygXigbwFBW1ARgJwggBMAuKAZ2HwEsA7BLHQgQ2LjoBsQoASUSOQCudGgEch0CiAC2uOJwwBfDBgHQAwi2Ap0TbASJlK1eoxxRW7Lj37gIw0RKmz5ilRgDGHKlE-byLR1UNDxCEnIAIhgAZQAJSKglKBYKKCCAbi8fHX9gACZA7V08rO86X3YEcngkVFKgA https://www.typescriptlang.org/play/?ssl=13&ssc=1&pln=13&pc=... (there are other ways as well)
In something like Java, it's the opposite: you get nominal types by default and you need to opt in to structural types (via interfaces).
Because TS is built on a duck typed language, tons of existing libraries would break if they had to declare explicit nominal types as arguments. Or you'd need to add custom interfaces for each library you use. Structural typing is what you want 99% of the time.
- alexcole 7y agoI think that Flow’s approach of using nominal types for classes and structural for everything else makes more sense. Are there really cases you expect classes to be structural types?
- jolux 7y agoObjects in OCaml are all structurally typed.
- jolux 7y agoJava interfaces are not structurally typed, are they?
- garethrowlands 7y agoNo but it's the closest available. Two classes C1 and C2 that both implement interfaces I1 and I2 can be said to have a common structure. That is, if they both implement I1 and I2 then they have that structure in common.
- jolux 7y agoYeah but that’s still fully nominal typing, you can’t implement the interface implicitly, it has to be identified by name.