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

Inherited types - array of unions vs union of arrays. #13097

Closed
jkjustjoshing opened this issue Dec 21, 2016 · 1 comment
Closed

Inherited types - array of unions vs union of arrays. #13097

jkjustjoshing opened this issue Dec 21, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@jkjustjoshing
Copy link

jkjustjoshing commented Dec 21, 2016

I'm trying to upgrade some Typescript 2.0 code to 2.1 and running into an issue. It distills down to the following:

TypeScript Version: 2.1.4

Code

interface Common {
    common: boolean;
}

interface A extends Common {
    a: A[];
}

interface B extends Common {
    b: B[];
}

let items: (A[] | B[]) = [];    
items.map(item => item.common); // Error

let otherItems: (A | B)[] = [];    
otherItems.map(item => item.common); // No error

In both of these situations I would expect no error to occur. Regardless of whether the array has all the same type or a mixture of types, both can be simplified to Common[] which is valid. However, I get the following error:

Cannot invoke an expression whose type lacks a call signature. Type '{ <U>(this: [A, A, A, A, A], callbackfn: (value: A, index: number, array: A[]) => U, thisArg?: an...' has no compatible call signatures.

(A[] | B[]) is the type that truly matches what my data model is, since my code guaranteed that the array only contains one type of object. Therefore, I'd rather not use (A | B)[].

@RyanCavanaugh
Copy link
Member

See #11205, #10620, #7294

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 21, 2016
@mhegazy mhegazy closed this as completed Apr 21, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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

3 participants