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

Using a union of arrays breaks array methods #12845

Closed
eamodio opened this issue Dec 12, 2016 · 3 comments
Closed

Using a union of arrays breaks array methods #12845

eamodio opened this issue Dec 12, 2016 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@eamodio
Copy link

eamodio commented Dec 12, 2016

TypeScript Version: 2.1.4

Code

abstract class ChildBase {
    id: string;
}

class ChildA extends ChildBase {
    foo: string;
}

class ParentA {
    children: ChildA[];
}

class ChildB extends ChildBase {
    bar: string;
}

class ParentB {
    children: ChildB[];
}

function findChild(parent: ParentA | ParentB, id: string) {
    return parent.children.findIndex(_ => _.id === id); // Compile error here
}

// Note that the following works (but it changes the meaning of the array)
function findChild2(parent: ParentA | ParentB, id: string) {
    return (<(ChildA|ChildB)[]>parent.children).findIndex(_ => _.id === id);
}

Expected behavior:

Compiles without error

Actual behavior:

Compiles with error:

Cannot invoke an expression whose type lacks a call signature. Type '((predicate: (value: ChildA, index: number, obj: ChildA[]) => boolean, thisArg?: any) => number) ...' has no compatible call signatures.
@aluanhaddad
Copy link
Contributor

It seems like this is what you want

function findChild(parent: { children: ChildBase[] }, id: string) {
    return parent.children.findIndex(_ => _.id === id);
}

or, more likely

abstract class ChildBase {
    id: string;
}

abstract class ParentBase {
    children: ChildBase[];
}

class ChildA extends ChildBase {
    foo: string;
}

class ParentA extends ParentBase {
    children: ChildA[];
}

class ChildB extends ChildBase {
    bar: string;
}

class ParentB extends ParentBase {
    children: ChildB[];
}

function findChild(parent: ParentBase, id: string) {
    return parent.children.findIndex(_ => _.id === id);
}

@eamodio
Copy link
Author

eamodio commented Dec 12, 2016

@aluanhaddad thanks for you reply and appreciate those workarounds. I posted above because I feel that there is a bug here. Also this issue exists without any inheritance, which makes both of your workaround less appealing.

For example, this exhibits the same behavior:

class ChildA {
    id: string;
    foo: string;
}

class ParentA {
    children: ChildA[];
}

class ChildB {
    id: string;
    bar: string;
}

class ParentB {
    children: ChildB[];
}

function findChild(parent: ParentA | ParentB, id: string) {
    return parent.children.findIndex(_ => _.id === id);
}

It seems to be caused by the type definition:

findIndex(predicate: (value: T, index: number, obj: Array<T>) => boolean, thisArg?: any): number;

Since T comes in as a union of 2 (or more) arrays.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 12, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Dec 12, 2016

Please see #10620 for details.

@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
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants