microsoft / microsoft/TypeScript

[BUG] union type generic extends child class with generic method

Open
#41,477 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: all, tested from 3.3 to 4.2

Search Terms: union type generic extends child class with generic method

Code

class A<GValue> {
  a<GKey extends GValue>(value: GKey): void {}
}

class B<GValue> extends A<GValue> {
  a<GKey extends GValue>(value: GKey): void {}
}


const a1: (('a' | 'b') extends any ? true : false) = true;
const a2: (('a') extends any ? true : false) = true;

const a3: (B<'a' | 'b'> extends A<any> ? true : false) = true; // error before v3.7
const a4: (B<'a'> extends A<any> ? true : false) = true; // error
const a5: (B<'a'> extends B<any> ? true : false) = true;

type Constrained<G extends A<any>> = true;

const a6: Constrained<A<'a'>> = true; // ok
const a7: Constrained<B<'a' | 'b'>> = true; // ok
const a8: Constrained<B<'a'>> = true; // error

Details:

  • a1 and a2 shows us that a string or an union of string extends any => legit

  • a3 shows us that it's still true, except before typescript 3.7 => true seems legit

  • a4 creates an error because (B<'a'> extends A<any> ? true : false) is actually false => bug, true expected.
    So, an union is true, but not a single value.

  • a5 is true which is correct

  • a6, a7 and a8 shows the same but with a type constraint

Notes:

  • a3 has not the same behavior between before and after v3.7
  • the bug appears only when we have a generic type on a method which extends another generic type coming from the class

Playground Link: playground link

Related Issues: this bug is pretty hard to reach and to describe, I was not able to find any related issue.
But a good start could be to search from a fix or update from the 3.6 to the 3.7, as a3 has not the same behavior between these versions.

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

Start with the supplied TypeScript repro and Playground link, comparing compiler behavior between 3.6 and 3.7 as suggested. Trace the type-checking path for generic methods on B extending A; done means the a4 and a8 cases accept true while the existing a5, a6, and a7 behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.