Microsoft/TypeScript

Overriding generic method inconsistently errors

Open

#26,631 opened on 2018年8月23日

GitHub で見る
 (1 comment) (0 reactions) (0 assignees)TypeScript (48,455 stars) (6,726 forks)batch import
BugDomain: check: Variance RelationshipsHelp Wanted

説明

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

コントリビューターガイド