|
|
|
|
|
by WorldMaker
877 days ago
|
|
If you want to make this easier to keep private between encapsulation boundaries the additional suggestion is make sure the Brand type extends symbol: type Brand<BaseType, Brand extends symbol> = BaseType & { readonly __brand__: Brand };
const FooIdBrand = Symbol('FooId');
type FooId = Brand<string, typeof FooIdBrand>;
function fooBar(asdf: FooId | 'foobar'): void { }
Using a private shared symbol your authoritative validation/sources can share your brand symbol and no one else can create one without using your validation. Private symbol brands in this way become the closest Typescript gets to "nominal types". |
|