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

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