TypeScript Version: 4.1.0-dev.20200903
Search Terms:
Union array type element inference in methods
Code
type Unpack<T> = T extends ReadonlyArray<infer P> ? P : never;
const test: ['a', 'b'] | ['c', 'd'] = ['a', 'b'] as any;
test.forEach(el => el); //error: Parameter 'el' implicitly has an 'any' type
test.forEach((el: Unpack<typeof test>) => el);
const element = test[1];
Expected behavior:
Both forEach calls should infer col in the same way: el should be 'a' | 'b' | 'c' | 'd'
Actual behavior:
First call can't infer el type.
Playground Link:
https://www.typescriptlang.org/play?ts=4.1.0-dev.20200903#code/C4TwDgpgBAqgdmAhgYwNYB4AqA+KBeKTKCAD2AjgBMBnKAJQkUoHs4AbEAQQCdvER0ASzgAzCNygAFXAH4pUAFxQ4EAG7iA3ACgtyVtWBRyBpQG0A5InMAaKOYBG5gLpQAPlAvIbdys-wfLbwc-RFpEOBBtLWNgADoRZm4AURQACwAKCDZ8XCyASm0Y+MSU5AzMtiV4JDR0UEhmESMIA2w8nOI2At19QyyIAFsKQwIY0wBGJw0gA
Related Issues:
~This looks like duplicate of https://github.com/microsoft/TypeScript/issues/36390 which in turn related to https://github.com/microsoft/TypeScript/issues/7294~
I don't get This expression is not callable error, for me it's just el is inferred as any.
@IvanZatravkin A closer duplicate would be #34605.
Looks like it, thanks