microsoft / microsoft/TypeScript
Unique type
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the proposed unique type examples and the linked TypeScript design goals, then inspect how unique symbols and type relationships are represented. Done means a pure compile-time unique type can distinguish separately declared values without emitting runtime JavaScript, with the behavior covered by appropriate type-checking tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100