2 ms·
TS is structurally typed. You declare the fields you expect on a type or interface, and as long as an object has at least those fields, it will typecheck. Nomin
by cowmoo728 9y ago
TS is structurally typed. You declare the fields you expect on a type or interface, and as long as an object has at least those fields, it will typecheck. Nominal typing would mean that you could declare type A and type B with identical fields, but if something is declared as A, it cannot be used in place of B.
TS has a strange workaround to get nominal typing for now:
https://basarat.gitbooks.io/typescript/docs/tips/nominalTyping.html https://basarat.gitbooks.io/typescript/docs/tips/nominalTypi...
- klibertp 9y agoThat's interesting - I'm almost sure last time I checked (possibly a couple years back) TS used nominal typing. Do you know if this changed at some point, or is it just my memory being bad?
- RyanCavanaugh 9y agoTypeScript has used structural typing since the beginning. Classes used to be nominal, but changed to structural (except for classes with private fields) at some point.
- klibertp 9y ago> Classes used to be nominal, but changed to structural Ah, ok, makes sense. I think I was last looking at TS around version 1.1 or something like that. Thanks!