microsoft / microsoft/TypeScript

Overriding generic method inconsistently errors

Open
#26,631 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Variance Relationships Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example from the Playground link in TypeScript 3.1.0-dev.201xxxxx, then compare it with and without the repeated Foo declaration. Read related issue #23960 for context; the work is done when the generic override either consistently reports the incompatibility or consistently accepts it.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.