In TypeScript, I have an API whose input is a union of tuples and I have a value whose type is a tuple of unions that I want to pass into it. It seems like this works in most cases, but I can’t figure out why it doesn’t work in my case, and what I can do to get it to work without needing a type assertion. Here is a reproduction of the issue (TS Playground link):
(x: [1 | 2]): [1] | [2] => x; // OK
(x: [1 | 2]): [1 | 11] | [2 | 22] => x; // OK
(x: [1 | 2]): [1 | []] | [2 | 22] => x; // OK
(x: [1 | 2]): [1 | []] | [2 | []] => x; // ERROR
Even though the first three examples are okay, the fourth example gives the error:
Type '[1 | 2]' is not assignable to type '[1 | []] | [2 | []]'.
Type '[1 | 2]' is not assignable to type '[1 | []]'.
Type '1 | 2' is not assignable to type '1 | []'.
Type '2' is not assignable to type '1 | []'.