microsoft / microsoft/TypeScript
Generic type not assignable if extends type with optional field and passed object does not include the optional field
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
TypeScript Version: 3.9.2
Search Terms:
optional generic "not assignable"
Code
const foo = <V extends { s?: string }>(v: V) => {};
// This works:
foo({ s: "1", v: 2 });
// This doesn't work:
foo({ v: 2 }); // Argument of type '{ v: number; }' is not assignable to parameter of type '{ s?: string | undefined; }'. Object literal may only specify known properties, and 'v' does not exist in type '{ s?: string | undefined; }'.
Compare with
const bar = <V extends { }>(v: V) => {};
// Both work:
bar({ s: "1", v: 2 });
bar({ v: 2 });
Expected behavior:
Both statements in the first snippet should compile.
Actual behavior:
foo({ v: 2 }); doesn't compile for const foo = <V extends { s?: string }>(v: V) => {};
Related Issues:
Possible remotely related... https://github.com/microsoft/TypeScript/issues/13747
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die TypeScript-3.9.2-Reproduktion im bereitgestellten Playground-Link auszuführen, und vergleiche die beiden generischen Aufrufe. Verfolge das Verhalten der Typprüfung für überschüssige Eigenschaften bei einer generischen Einschränkung mit einem optionalen Feld. Als erledigt gilt die Aufgabe, wenn beide Aufrufe im ersten Snippet kompiliert werden, ohne das für das zweite Snippet gezeigte Verhalten zu ändern.
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
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100