Repository navigation
Unions of more than 25 values cannot be used to discriminate other unions #42518
Description
Bug Report
🔎 Search Terms
25, union, limit, max
These other issues may be related:
- A limit of 25 items in a different union context: Limit of 25 members when using union of mapped type #40803
- Like in Long conditional type resolves to any #28663, my unions are machine-generated
🕗 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!
Activity
MartinJohns commentedon Jan 27, 2021
Duplicate of #40803. Third issue when searching for
union 25(including yours).etlovett commentedon Jan 27, 2021
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!
Martin Johns (@MartinJohns) I assume we should we close this then?
typescript-bot commentedon Jan 30, 2021
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.