Skip to content

Unique typeΒ #59431

Open
Open
@ruojianll

Description

@ruojianll

πŸ” 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions