镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Bug: Class extending & incompatible methods #14288

Description

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.

Activity

  1. alexcorvi commented on Feb 24, 2017

    @alexcorvi
    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.

  2. mhegazy commented on Feb 8, 2018

    @mhegazy
    Contributor

    Duplicate of #1373

    Related discussion in #6118 and #10570

  3. added
    DuplicateAn existing issue was already created
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Feb 8, 2018
  4. typescript-bot commented on Feb 26, 2018

    @typescript-bot
    Contributor

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

  5. locked and limited conversation to collaborators on Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions