microsoft / microsoft/TypeScript

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

Offen
#44,315 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Needs Investigation
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verknüpften TypeScript Playground und reproduziere die beiden generischen Aufrufe mit den Versionen 3.6.0-dev.20190723 und 3.6.0-dev.20190724. Verfolge das Compiler-Verhalten für Union- und Intersection-Constraints. Als abgeschlossen gilt die Aufgabe, wenn testA s1 akzeptiert und a2 als B inferiert wird, ohne neue Fehler einzuführen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.