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

Method inferred as property for inheritance and method overrides #11512

Closed
panthus opened this issue Oct 11, 2016 · 2 comments
Closed

Method inferred as property for inheritance and method overrides #11512

panthus opened this issue Oct 11, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@panthus
Copy link

panthus commented Oct 11, 2016

TypeScript Version: 2.0.3

Code

class Animal {
    public getA(a: Animal) { }
}

class Cat extends Animal {
    public x: Cat[];
    public getA(a: Cat) { }
}

class Dog extends Animal {
    public getA(a: Dog) {}
}

let x: Cat | Dog;
// Inferred type: (property) getA: ((a: Cat) => void) | ((a: Dog) => void)
x.getA(new Animal()); // Error

Expected behavior:
Matching method overload is selected.

Actual behavior:
The method is inferred to be a property if subclasses have a different signature.

@panthus panthus changed the title Wrong type inferrance for inheritance and method overrides Method inferred as property for inheritance and method overrides Oct 12, 2016
@dead-claudia
Copy link

Seems working as intended. It's unsafe to elevate those, because ((a: Cat) => void) | ((a: Dog) => void) are two incompatible types. If x is in fact a Cat, you would want x.getA(dog) to be a compile-time error, and similarly, if x is a Dog, you'd expect the same with x.getA(cat). The function incompatibility is why you need to narrow it first (there's no overload here because the types don't intersect).

@RyanCavanaugh
Copy link
Member

Duplicate #7294

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 13, 2016
@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