2 ms·
I truly wish I could get Opaque Types (Newtypes?) in Typescript. Great writeup!
by jbm 4y ago
I truly wish I could get Opaque Types (Newtypes?) in Typescript. Great writeup!
- jbm 4y agoI was secretly hoping someone would respond with an insult and the correct way to do Opaque Types in TypeScript, but sadly no such luck. I took a look and found that it is possible to do something similar here (albeit with more heavy lifting) https://www.ferreira.io/posts/opaque-branded-types-in-typescript https://www.ferreira.io/posts/opaque-branded-types-in-typesc...
- shirogane86x 4y agoAs far as I know, the only available approaches are brands and just wrapping it in a one element object (but that's even worse). But the branding is a lot easier if you just use something like zod [1] for all your types. [1] https://github.com/colinhacks/zod#brand https://github.com/colinhacks/zod#brand
- jbm 4y agoThis is great! Thank you for the share.