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

Unification of functions on union types #34605

Closed
pauldraper opened this issue Oct 20, 2019 · 3 comments
Closed

Unification of functions on union types #34605

pauldraper opened this issue Oct 20, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@pauldraper
Copy link

TypeScript Version: 3.6.3

Search Terms:

Union type functions

Code

type A = number[] | string[];
const a = <A>[1];
a.forEach(element => console.log(element)); // error

Expected behavior:

No compile error. (element is inferred to be number | string)

Actual behavior:

Compile error. (Parameter 'element' implicity has 'any' type.)

Playground Link:

http://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=4&pc=1#code/C4TwDgpgBAglC8UB2BXAtgIwgJwNoF0oAfKAZ2GwEskBzAgbgCgBjAeyXKgEMEoAeGAD5cARnxMuAOgBmrbAFEuzABYAKCABsIaCEmAJBUNh1ZbJG1jXVadegJR36UAPTOoObHMZA

Related Issues:


I'm unsure whether or not it is realistic to improve this. Recently I have been working with a number of A[] | B[] types and noticed that most array operations cannot be done on these.

@jack-williams
Copy link
Collaborator

I believe there is a PR that would address this here #17819, but it's closed from inactivity. The PR links to related issues.

I think the root issue is contextual typing for overloaded / intersected function signatures, rather than union types. Simplified repos:

const x: ((x: number) => void) & ((x: string) => void) =
    x => console.log(x); // error on x;


interface Foo {
    (x: string): void;
    (x: number): void;
}    

const y: Foo = x => console.log(x); // error on x;

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

See #7294 and its related issues

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

4 participants