Re:TypeScript’s quirks: How inconsistencies make the languag...
by alexcole on Friday January 31, 2020 @11:35PM
(#22208697)
I 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?
Re:TypeScript’s quirks: How inconsistencies make the languag...
by jolux on Saturday February 01, 2020 @10:25AM
(#22210961)
Objects in OCaml are all structurally typed.
Re:TypeScript’s quirks: How inconsistencies make the languag...
by jolux on Saturday February 01, 2020 @02:01AM
(#22209170)
Java interfaces are not structurally typed, are they?
Re:TypeScript’s quirks: How inconsistencies make the languag...
by garethrowlands on Saturday February 01, 2020 @07:57AM
(#22210261)
No 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.
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.