microsoft / microsoft/TypeScript

3.4 Regression on Type inference with union types

Open
#30,694 1 comment 2 reactions 1 assignee View on GitHub

@weswigham is already working on this.

Since Apr 9, 2019.

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

Description

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms: Type inference, discriminated union types

Code

type AllBoxes = ABox | BBox;

export interface ICanFindBoxes {
    findBoxes<T extends AllBoxes, K extends string>(boxName: K): T extends { type: K } ? T[] : never;
}

class Box<T extends string = string> implements ICanFindBoxes{
    type: T;
    
    public findBoxes<T extends AllBoxes, K extends string>(boxName: K): T extends { type: K } ? T[] : never {
      throw 'argg';
    }
   
}

export class ABox extends Box<'a'>{

}

export class BBox extends Box<'b'>{}

Expected behavior:
It compiles
Actual behavior:
Compile time error:

 Property 'findBoxes' in type 'Box<T>' is not assignable to the same property in base type 'ICanFindBoxes'.
  Type '<T extends AllBoxes, K extends string>(boxName: K) => T extends { type: K; } ? T[] : never' is not assignable to type '<T extends AllBoxes, K extends string>(boxName: K) => T extends { type: K; } ? T[] : never'. Two different types with this name exist, but they are unrelated.
    Type '(T extends { type: K; } ? T[] : never) | ({ type: K; } & ABox)[] | ({ type: K; } & BBox)[]' is not assignable to type 'T extends { type: K; } ? T[] : never'.
      Type '({ type: K; } & ABox)[]' is not assignable to type 'T extends { type: K; } ? T[] : never'.

11     public findBoxes<T extends AllBoxes, K extends string>(boxName: K): T extends { type: K } ? T[] : never {

The compiler is incorrectly identifying that the signatures are different when they are not. If I simplify the union type to only one it works (but this defeats the purpose of the signature).

This was working in < 3.4.1

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.