Skip to content

Overriding generic method inconsistently errors #26631

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

Open
saschanaz opened this issue Aug 23, 2018 · 1 comment
Open

Overriding generic method inconsistently errors #26631

saschanaz opened this issue Aug 23, 2018 · 1 comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@saschanaz
Copy link
Contributor

saschanaz commented Aug 23, 2018

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

@ghost
Copy link

ghost commented Aug 23, 2018

Think this has to do with overloading.
No error here:

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

class Bar implements Foo {
    method<T extends string>(): T { throw new Error(); }
}

But there is an error if method is written only once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

2 participants