|
|
|
|
|
by spankalee
478 days ago
|
|
I don't know how important it is to bad use of the values directly, but that is also possible. You have to intersect every value with a brand, like: type Enum<T> = {
[K in keyof T]: T[K] & {__brand: never};
}
const _Foo = {
one: '1',
two: '2',
three: '3'
} as const;
const Foo = _Foo as Enum<typeof _Foo>;
type Foo = ValueOf<typeof Foo>;
And now, this will work: doSomething(Foo.two);
But this will error: doSomething('2');
|
|
So you can do something like
without triggering a type error too.With built in enums that would trigger an error