3 ms·
You can sue Tagged/Branded types for that: https://medium.com/@KevinBGreene/surviving-the-typescript-ecosystem-branding-and-type-tagging-6cf6e516523d https://me
by nikeee 4y ago
You can sue Tagged/Branded types for that:
https://medium.com/@KevinBGreene/surviving-the-typescript-ecosystem-branding-and-type-tagging-6cf6e516523d https://medium.com/@KevinBGreene/surviving-the-typescript-ec...
It's a simple hack that takes a type and adds an additional field to it. You can then use a User-Defined Type-Guard to ensure a value is valid:
https://levelup.gitconnected.com/user-defined-type-guards-in-typescript-fad639e4944f https://levelup.gitconnected.com/user-defined-type-guards-in...
- nivertech 4y agoSo the only change is one line, and there are no additional dependencies? type Brand<K, T> = K & { __brand: T } Looks like a great solution, also the constant syntax is very readable: 10 as USD