Open ruojianll opened 3 months ago
related #33038
@jcalz Shall we use it? Why was it closed?
declare const _flag: unique symbol;
type Flag = typeof _flag;
type MyType<T extends string|Flag>=T extends Flag? 'IsFlag':'A normal string'
type M1 = MyType<"Flag">;
type M2 = MyType<Flag>;
Putting unique
in type space quickly gets weird for a bunch of reasons because generic instantiation becomes a much different operation from a simple substitution, e.g. if you have
type Pair<T> = { x: T, y: T };
// __very__ different from writing { x: unique, y: unique }
type S = Pair<unique>;
This would mean many common identities would no longer be true
type S = Pair<unique>;
It has an error Type expected.
@RyanCavanaugh
declare const _flag: unique symbol;
means there is a unique symbol constant _flag
in execution context, but no. This is not type safe.
🔍 Search Terms
"unique type"
✅ Viability Checklist
⭐ Suggestion
Sometimes I just need a unique type as flags in my type calculations. If I use a string subtype or others it couldn't be identity well. Such as:
In the code,
Flag
is also a string. I don't want to post some complex types which references each other and using flag types but it is really useful for me. Define a pure unique type without any runtime content.📃 Motivating Example
Flag1
is notFlag2
like unique symbols. The unique types only extends itself for checking what it is. Pass a text tounique
keyword to create a unique type.💻 Use Cases