microsoft / microsoft/TypeScript

3.4 Regression on Type inference with union types

Aperta
#30,694 1 commento 2 reazioni 1 assegnatario Vedi su GitHub

@weswigham ci sta già lavorando.

Dal 9/4/2019.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.