Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique type #59431

Open
6 tasks done
ruojianll opened this issue Jul 26, 2024 · 5 comments
Open
6 tasks done

Unique type #59431

ruojianll opened this issue Jul 26, 2024 · 5 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@ruojianll
Copy link

ruojianll commented Jul 26, 2024

πŸ” 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.
@jcalz
Copy link
Contributor

jcalz commented Jul 26, 2024

related #33038

@ruojianll
Copy link
Author

@jcalz Shall we use it? Why was it closed?

@nmain
Copy link

nmain commented Jul 27, 2024

See #55691 (comment)

@RyanCavanaugh
Copy link
Member

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

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jul 29, 2024
@ruojianll
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants