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

Union of arrays doesn't get converted to array of unions #35294

Closed
reverofevil opened this issue Nov 22, 2019 · 3 comments
Closed

Union of arrays doesn't get converted to array of unions #35294

reverofevil opened this issue Nov 22, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@reverofevil
Copy link

TypeScript Version: 3.8.0-dev.20191121

Search Terms: array union

Code

declare const x: 'a'[] | 'b'[];
x.forEach(v => { }); // FAIL

// it doesn't work as is, so we have to resort
// to enforcing the shape of the type
const shape = <T>(t: T[]): T[] => t;
const x1 = shape(x); // converts to ('a' | 'b')[]
x1.forEach(v => { }); // OK

declare const y: 'a'[] | 'b'[] | never[]
y.forEach(v => { }); // FAIL
const y1 = shape(y); // cannot convert anymore
y1.forEach(v => { }); // FAIL

declare const z: 'a'[] | never[]
z.forEach(v => { }); // OK. apparently 3 cases was too much

Expected behavior:
No error messages in either case.

Actual behavior:
Conversion of union of arrays to array of unions happens only if it's enforced manually, and only if there is no never[] cases.

Playground Link:
Link

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 22, 2019
@RyanCavanaugh
Copy link
Member

Many duplicates of this - start at #34605 and explore from there

@reverofevil
Copy link
Author

I thought the issue with call signatures was only related.

Closing as duplicate of #7294.

@reverofevil
Copy link
Author

For other suffering people: it works in both cases if you use another shaping function. So at least there is some way to make code work without any.

const thiccShape = <T extends any[]>(t: T): T[number][] => t;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants