3 ms·
Structural 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.
by wk_end 5y ago
Structural 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