4 ms·
>2. Nominal types, so you can have for example "Id" type that is a number but not to be mixed with other numbers, you can't just call a function that expects an
by moonchrome 5y ago
>2. Nominal types, so you can have for example "Id" type that is a number but not to be mixed with other numbers, you can't just call a function that expects an Id with literal 42. Classes are nominal too, which I think is a solid choise.
Structural typing is one of the best features of TS in my opinion, I like it in general but it's a better fit for JS ecosystem.
- wk_end 5y agoStructural typing is a sane default in JS land, but there are times that you really really want nominal typing and its complete absence from TS kind of hurts.
- jareware 5y agoFWIW, io-ts offers this via its “userland” implementation of branded types.
- fiddlerwoaroof 5y agoYeah, for modeling with types, you frequently want nominal types (e.g. you want to make sure that a certain types always means “the data has passed through such and such a function). Flow’s combination of structural and nominal types seems better to me than TS’s pure structural system.
- rtpg 5y agofor what it's worth you can totally fake nominal typing in a hacky way through a sort of shiboleth key. You say "this type has `____type: 'foo'`, and on constructions of the object you just cast (or hell, just put the key in!), and you're off to the races. This is far from perfect, but when you're working with very tricky code and want to be sure, you at least have the option
- deleted 5y ago[deleted]