|
|
|
|
|
by spankalee
478 days ago
|
|
You can always cast your way around nominal typing, even with enums. So you can do: doSomethingBar('No' as Bar);
But you can make my Enum<> utility tighter by including the object type in the brand: type Enum<T> = {
[K in keyof T]: T[K] & {__brand: T};
}
Then, if you had another const object Baz with the same value as Foo, you would get an error here: doSomething(Baz.one);
The only time when you wouldn't get an error there is if the whole Baz enum object was assignable to Foo. |
|
I think you can avoid that by not export type `Bar`. I think Bar then acts as an abstract type.
On the other hand with, the branded version, even if you do not avoid exporting the type, even with when branded the object type, you can still get one enum masquerading as another by using the same name. See below where the original Foo is in enums.ts:
I thought enums was the only way to get truly unique types in typescript, but I would be happy to be wrong here.