Skip to content
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

Array element type inference not working in array methods when array is union type #40370

Closed
IvanZatravkin opened this issue Sep 3, 2020 · 4 comments

Comments

@IvanZatravkin
Copy link

IvanZatravkin commented Sep 3, 2020

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:

@IllusionMH
Copy link
Contributor

IllusionMH commented Sep 3, 2020

This looks like duplicate of #36390 which in turn related to #7294

@IvanZatravkin
Copy link
Author

I don't get This expression is not callable error, for me it's just el is inferred as any.

@jack-williams
Copy link
Collaborator

@IvanZatravkin A closer duplicate would be #34605.

@IvanZatravkin
Copy link
Author

Looks like it, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants