Skip to content

Overriding generic method inconsistently errors #26631

Open
@saschanaz

Description

@saschanaz

TypeScript Version: 3.1.0-dev.201xxxxx

Search Terms: override generic

Code

interface Foo {
    method<T>(): T;
}

// Error: Interface 'Bar<T>' incorrectly extends interface 'Foo'.
interface Bar<T extends Node> extends Foo {
    method<T2 extends T>(): T2;
}

// Uncommenting this suddenly fixes the error
// interface Foo {
//     method<T>(): T;
// }

Full error message:

Interface 'Bar<T>' incorrectly extends interface 'Foo'.
  Types of property 'method' are incompatible.
    Type '<T2 extends T>() => T2' is not assignable to type '<T>() => T'.
      Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated.
        Type 'Node' is not assignable to type 'T'.

Expected behavior: The error should always appear, or always not appear.

Actual behavior: A partial interface mysteriously removes the error

Playground Link: https://www.typescriptlang.org/play/#src=interface%20Foo%20%7B%0D%0A%20%20%20%20method%3CT%3E()%3A%20T%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20Bar%3CT%20extends%20Node%3E%20extends%20Foo%20%7B%0D%0A%20%20%20%20method%3CT2%20extends%20T%3E()%3A%20T2%3B%0D%0A%7D%0D%0A%0D%0A%2F%2F%20Uncommenting%20this%20suddenly%20fixes%20the%20error%0D%0A%2F%2F%20interface%20Foo%20%7B%0D%0A%2F%2F%20%20%20%20%20method%3CT%3E()%3A%20T%3B%0D%0A%2F%2F%20%7D

Related Issues: #23960

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions