Open
Description
π Search Terms
"unique type"
β Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β 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:
type Flag = 'Flag'
type MyType<T extends string|Flag>=T extends Flag? 'IsFlag':'A normal string'
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
type Flag1 = unique('Flag')
type Flag2 = unique('Flag')
Flag1
is not Flag2
like unique symbols.
The unique types only extends itself for checking what it is.
Pass a text to unique
keyword to create a unique type.
π» Use Cases
- What do you want to use this for?
To control the type calculation especially in recursive type definitions. - What shortcomings exist with current approaches?
If I define a unique symbol, it should produce a runtime symbol. - What workarounds are you using in the meantime?
Write flag with string sub-type carefully.