Skip to content

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

Closed

Description

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions