_type?: FlavorT;
}
export type Flavor<T, FlavorT> = T & `Flavoring<FlavorT>;
We can then use Flavor to define new ID types:
type PersonId = Flavor<number, “Person”>
type BlogPostId = Flavor<number, “BlogPost”>
Because the _type property of Flavoring is optional, implicit conversion is supported:
const personId : PersonId = 1; // OK
const person: Person = personLikeStructure // OK
but we get the safety of branding in that incompatible types don’t mix:
const blogPostId : BlogPostId = personId; // Error!
Еще раз, такой тип нельзя использовать как индекс
Обсуждают сегодня