Skip to content

Unions of more than 25 values cannot be used to discriminate other unions #42518

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

Closed
etlovett opened this issue Jan 27, 2021 · 3 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@etlovett
Copy link

Bug Report

🔎 Search Terms

25, union, limit, max

These other issues may be related:

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about unions, 25, etc.

This reproduces in the Playground in the following versions: v3.9.7, v4.0.5, v4.1.3, v4.2.0-beta, v4.2.0-dev.20210127. I haven't tried any other versions.

⏯ Playground Link

Playground link with sample code

💻 Code

export type Union1 =
  | 'A'
  | 'B'
  | 'C'
  | 'D'
  | 'E'
  | 'F'
  | 'G'
  | 'H'
  | 'I'
  | 'J'
  | 'K'
  | 'L'
  | 'M'

export type Union2 =
  | 'N'
  | 'O'
  | 'P'
  | 'Q'
  | 'R'
  | 'S'
  | 'T'
  | 'U'
  | 'V'
  | 'W'
  | 'X'
  | 'Y'
  | 'Z'

function one(arg: { val: Union1, other?: string } | { val: Union2}) {}

function two(vals: Array<Union1 | Union2>) {
  vals.map(val => one({ val }))
}

🙁 Actual behavior

In this code, I have two (machine-generated) string unions, which I then want to use to create discriminated union in a function signature. This approach works fine if the total number of entries in the two unions is <=25, but once a 26th item is added to the two unions, a type error is raised.

The type error is in the one({ val }) call:

Argument of type '{ val: "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"; }' is not assignable to parameter of type '{ val: Union1; other?: string | undefined; } | { val: Union2; }'.
  Type '{ val: "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"; }' is not assignable to type '{ val: Union2; }'.
    Types of property 'val' are incompatible.
      Type '"A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"' is not assignable to type 'Union2'.
        Type '"A"' is not assignable to type 'Union2'.(2345)

This is incorrect because each val value that might be passed there is, by definition from the two function signature, a valid value from Union1 or Union2. Indeed, if you comment out a single item from either Union1 or Union2, you'll see that the type error goes away. I'm guessing there's a hard-coded limit somewhere of 25 that this is hitting.

🙂 Expected behavior

There should not be a type error, because the val in two's map is guaranteed to be Union1 | Union2 based on the type of vals, and therefore can always fulfill one side of the arg: { val: Union1, other?: string } | { val: Union2} union.

Thanks!

@MartinJohns
Copy link
Contributor

Duplicate of #40803. Third issue when searching for union 25 (including yours).

@etlovett
Copy link
Author

Ah, ok, so it is a duplicate. I did find it, as you can see from my original report, but it wasn't clear to me that it was exactly a duplicate since the context of how the large union is generated and used is different. Apologies for the duplicate report!

@MartinJohns I assume we should we close this then?

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants