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

Can't call array methods on type (A[] | B[]) #18602

Closed
testerez opened this issue Sep 20, 2017 · 5 comments
Closed

Can't call array methods on type (A[] | B[]) #18602

testerez opened this issue Sep 20, 2017 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@testerez
Copy link

testerez commented Sep 20, 2017

TypeScript Version: 2.5.2

Code

function test(items: string[] | number[]) {
  items.forEach(item => console.log(item));
}

Expected behavior:
no error

Actual behavior:

Cannot invoke an expression whose type lacks a call signature. Type '((callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any) => void) | ...' has no compatible call signatures.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 20, 2017
@RyanCavanaugh
Copy link
Member

Duplicate #7294

@RyanCavanaugh
Copy link
Member

Also #10620

@rdeneau
Copy link

rdeneau commented Sep 25, 2017

It's not exactly the same but you could use a type alias to circumvent this issue:

type stringOrNumber = string | number;
function test(items: stringOrNumber[]) {
  items.forEach(item => console.log(item));
}

@kitsonk
Copy link
Contributor

kitsonk commented Sep 25, 2017

circumvent this issue

It isn't really an issue, in that the compiler is doing what it is instructed to do, albeit it can be slightly confusing. Also a type alias is not required:

function test(items: (string | number)[]) {
  items.forEach(item => console.log(item));
}

@testerez
Copy link
Author

@rdeneau actually is't what I did :)

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants