- Published on
Flavoring - Flexible Nominal Typing for TypeScript
- Authors
- Name
- Lucas Floriani
- @lucasfloriani13
Flavoring - Flexible Nominal Typing for TypeScript
Flavoring - Flexible Nominal Typing for TypeScript
Branding is the usual approach of Opaque Types where we add an extra property like type to make numbers different from each other (PersonId !== ProductId)
The issue comes when we have a Product type and we need to assing a type generated from GraphQL for example, it wont match the id because codegen doesnt understand Opaque Types/Branding.
PS: Branding is the act of adding a name to something
How to solve this issue?
Using the approach called Flavor! Its a technic to allow unbranded values to be implicity converted into the branded type, but doesnt allow implicit conversion between branded types.
When we should choose Branding?
- When we want to write a code that can safely assume some upstream validation has occurred–e.g. a
DateStr
which must be in a valid ISO8601 format. - A type error admitted by implicit conversion could lead to a dangerous error