microsoft / microsoft/TypeScript

Unique type

Open
#59,431 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔍 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:

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
  1. What do you want to use this for?
    To control the type calculation especially in recursive type definitions.
  2. What shortcomings exist with current approaches?
    If I define a unique symbol, it should produce a runtime symbol.
  3. What workarounds are you using in the meantime?
    Write flag with string sub-type carefully.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.