5 ms·
"Stringly typed", the way I've heard it, is a valid criticism when people replace type safety with magic strings which may or may not be checked at runtime but
by kristiandupont 3y ago
"Stringly typed", the way I've heard it, is a valid criticism when people replace type safety with magic strings which may or may not be checked at runtime but certainly not at compile time.
However, that's not the case when it comes to Typescript, because literal and union string types are actually checked at compile time. So what is the problem?
- yencabulator 3y agoIf everything is a string, you can accidentally use a UserID as a PostID.
- shermantanktop 3y agoExactly. Especially easy if the variable name is “id”. For API methods which take multiple ids, the order is easy to mix up - though in TS that is conventionally handled with options objects.
- kristiandupont 3y agoBut this is exactly what the article is about? I don't know what you mean by "option object" but it doesn't sound any more conventional than union types to me.
- shermantanktop 3y agoWhat I meant by option objects is a dictionary object holding all parameters. doStuff({userId: foo, itemId: bar}); This allows the order of key/val pairs to move around, making it more robust to mistakes than doStuff: (string,string) => void.