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

Bug: Class extending & incompatible methods #14288

Closed
alexcorvi opened this issue Feb 24, 2017 · 3 comments
Closed

Bug: Class extending & incompatible methods #14288

alexcorvi opened this issue Feb 24, 2017 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@alexcorvi
Copy link

alexcorvi commented Feb 24, 2017

TypeScript Version: 2.1.1

Code (playground link)

interface Details { 
    name: "Alex" | "Dan",
    age:number
}

class A {
    method(): Details { 
        return {
            name: "Alex", // this doesn't fire any error
            age:30
        }
    }
}

class B extends A {
    method() {
        return {
            name: "Alex", // this fires an error
            age:50
        }
    }
}

Expected behavior:

No compilation errors

Actual behavior:

An error that says:

Class 'B' incorrectly extends base class 'A'.
Types of property 'method' are incompatible.
Type '() => { name: string; age: number; }' is not assignable to type '() => Details'.
Type '{ name: string; age: number; }' is not assignable to type 'Details'.
Types of property 'name' are incompatible.
Type 'string' is not assignable to type '"Alex" | "Dan"'.

But method in class B is returning an object that is compatible with the declared interface an the error doesn't show up on class A method.

@alexcorvi
Copy link
Author

Adding the Details interface as a declaration for the return type in the class B method does solve the issue.

interface Details { 
    name: "Alex" | "Dan",
    age:number
}

class A {
    method(): Details { 
        return {
            name: "Alex",
            age:30
        }
    }
}

class B extends A {
    method(): Details {
        return {
            name: "Alex",
            age:50
        }
    }
}

However, I don't have to add a declaration for the method() in class B since the class B is extending another class (B) where the method has a return declaration.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 24, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Feb 8, 2018

Duplicate of #1373

Related discussion in #6118 and #10570

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. labels Feb 8, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 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