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

LiteralList<Union> #904

Open
macmillen opened this issue Jul 3, 2024 · 2 comments
Open

LiteralList<Union> #904

macmillen opened this issue Jul 3, 2024 · 2 comments

Comments

@macmillen
Copy link

macmillen commented Jul 3, 2024

I want to be able to define an array that contains exactly the elements of all possible values of a union once.

type Union = "prevStep" | "nextStep" | "submit" | "order";

// this works:
const list: LiteralList<Union> = ["nextStep", "order", "prevStep", "submit"];

// this fails:
const list: LiteralList<Union> = ["nextStep", "order", "prevStep", "submit", "submit"];

// this fails:
const list: LiteralList<Union> = ["nextStep", "order", "prevStep"];

I already made this work with this type:

type LiteralList<T extends string, U = T> = [T] extends [never]
  ? []
  : U extends T
  ? [U, ...LiteralList<Exclude<T, U>>]
  : [];

I'm not sure though if it makes sense since I asked ChatGPT to do it.

Is it possible to add this type helper to type-fest?

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@macmillen
Copy link
Author

I realized that this:

type LiteralList<T extends string, U = T> = [T] extends [never]
  ? []
  : U extends T
  ? [U, ...LiteralList<Exclude<T, U>>]
  : [];

for some reason is really slowing down the TS language server.

Is there maybe another way to achieve this?

@Emiyaaaaa
Copy link
Collaborator

Related to #686

@fregante fregante changed the title Feature Request: LiteralList<Union> LiteralList<Union> Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants