|
|
|
|
|
by davorak
478 days ago
|
|
Unlike the enum solution this is not nominal to my understanding. const t = ('2' as '2' & {__brand: never});
doSomething(t);
Does not trigger an error.So you can do something like const _Foo2 = {
two: '2',
} as const;
const Foo2 = _Foo as Enum<typeof _Foo>;
type Foo2 = ValueOf<typeof Foo2>;
doSomething(Foo2.two);
without triggering a type error too.With built in enums that would trigger an error enum Bar {
No = 'No',
Yes = 'Yes',
}
function doSomethingBar(message: Bar): void {
}
// no type error
doSomethingBar(Bar.No);
// type error
doSomethingBar('No');
enum Bar2 {
No = 'No',
Yes = 'Yes',
}
// type error
doSomethingBar(Bar2.No);
|
|