found a neat use for 's new opaque type aliases: type safe stringified values. errors if an unknown/arbitrary string is passed
Conversation
Was wanting this recently for tagging various IDs. Would really reduce the chance of mixing them up and allow for type-driven refactors.
3
2
Isn't it still safer / easier to enumerate possible values in `type S = 'someid' | 'otherid'`? Works even in the same file.
1
1
if it's practical to enumerate all valid ids, sure, but if eg. valid ids = string of any number in a large range then it's not practical
1
1
Ah yeah, agree, TS and Flow always felt a bit limited in that regard (I'd love to be able to specify regexp as a type).
1
3
Opaque types plus a fallible constructor function could help with that! Check the regex in the function :)
Nah, I don't want it to be a runtime check, exactly the opposite. E.g. to strengthen defs of parseInt and similar funcs.
2


