microsoft / microsoft/TypeScript

Generic function does not accept argument of type with union and intersection

Aperta
#44,315 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms

generic function union intersection type optional property

🕗 Version & Regression Information
  • This changed between versions 3.6.0-dev.20190723 and version 3.6.0-dev.20190724
⏯ Playground Link

Playground link with relevant code

💻 Code
type A = ({ a: any } | { b: any }) & { c?: any };

declare function testA<Param extends A>(param: Param & A): Param;

declare const s1: A;

const a1 = testA(s1);
/**
 * Argument of type 'A' is not assignable to parameter of type '{ b: any; } & A'.
 *   Type '{ a: any; } & { c?: any; }' is not assignable to type '{ b: any; } & A'.
 *     Type '{ a: any; } & { c?: any; }' is not assignable to type '{ b: any; } & { a: any; } & { c?: any; }'.
 *       Property 'b' is missing in type '{ a: any; } & { c?: any; }' but required in type '{ b: any; }'.(2345)
 */

type B = ({ a: any } | { b: any }) & { c: any };

declare const s2: B;

const a2 = testA(s2); // Type of result is A but must be B

🙁 Actual behavior

The line const a1 = testA(s1); gives an error:

Argument of type 'A' is not assignable to parameter of type '{ b: any; } & A'.
  Type '{ a: any; } & { c?: any; }' is not assignable to type '{ b: any; } & A'.
    Type '{ a: any; } & { c?: any; }' is not assignable to type '{ b: any; } & { a: any; } & { c?: any; }'.
      Property 'b' is missing in type '{ a: any; } & { c?: any; }' but required in type '{ b: any; }'.(2345)

In the line const a2 = testA(s2); the typescript compiler infers the constanta a2 has type A

🙂 Expected behavior

The variable s1 has the type A that means function testA<Param extends A>(param: Param & A): Param accepts s1. The parameter type Param is instantiated to A.

The constant a2 has the type B since the parameter s2 has the type B.

It was a normal behavior up to version 3.6.0-dev.20190723. Since then it leads to errors.

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.

Direzione di ricerca

Inizia con il TypeScript Playground collegato e riproduci le due chiamate generiche usando le versioni 3.6.0-dev.20190723 e 3.6.0-dev.20190724. Traccia il comportamento del compilatore per i vincoli di unione e intersezione. Il lavoro è completato quando testA accetta s1 e inferisce a2 come B senza introdurre nuovi errori.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.