microsoft / microsoft/TypeScript
Type 'never' incorrectly inferred for mixed primitive type properties assignment with strictNullChecks disabled
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 Search Terms
strictNullChecks false assign never
🕗 Version & Regression Information
- typescript 3.6.0-dev.20190612, 3.6.0, and every version on TS Playground between 3.6.1 and nightly (5.7.0-dev.20240914)
- This changed between versions 3.6.0-dev.20190611 and 3.6.0-dev.20190612
- This changed in commit or PR 74c6bc1f85c2ab36fdb789aa0ef6ee5dbde5afdd (#31708)
- No error in da2aa9781e582bebd531750a4b8a81d9f8be2906
⏯ Playground Link
💻 Code
type Obj = {
prop1?: string;
prop2?: string;
prop3?: number;
prop4?: Date;
};
// Case 1: All properties have the same type (string)
const f1 = (obj: Obj, key: "prop1" | "prop2") => {
obj[key] = undefined; // Valid
obj[key] = null; // Valid when strictNullChecks is false
};
// Case 2: Properties have different primitive types (string | number)
const f2 = (obj: Obj, key: "prop1" | "prop3") => {
obj[key] = undefined; // Error when strictNullChecks is false. Expected to be valid
obj[key] = null; // Error. Expected to be valid when strictNullChecks is false
const val = obj[key]; // Valid (type is string | number)
// Workaround
const obj2 = obj as {prop1?: undefined, prop3?: undefined};
obj2[key] = undefined; // Valid
obj2[key] = null; // Valid when strictNullChecks is false
// Valid even with `exactOptionalPropertyTypes: true`, though behavior differs
delete obj[key];
};
// Case 3: Properties include both primitive and object types (string | Date)
const f3 = (obj: Obj, key: "prop1" | "prop4") => {
obj[key] = undefined; // Valid
obj[key] = null; // Valid when strictNullChecks is false
};
🙁 Actual behavior
-
In Case 2, when
strictNullChecksis false:- Assigning
undefinedresults in an error: "Type 'undefined' is not assignable to type 'never'.(TS2322)" - Assigning
nullalways results in an error: "Type 'null' is not assignable to type 'never'.(TS2322)"
- Assigning
-
The behavior is inconsistent across different combinations of property types.
🙂 Expected behavior
Since all properties in Obj are optional, it should be possible to assign undefined to obj[key] regardless of the strictNullChecks setting, unless exactOptionalPropertyTypes is true (which cannot be used with strictNullChecks: false).
Additionally, when strictNullChecks is false, assigning null should be allowed for all cases.
Additional information about the issue
No response
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 mit der TypeScript Playground-Reproduktion und vergleiche das Verhalten der Fälle 1–3 bei deaktiviertem strictNullChecks. Verfolge, wie Typen für indizierte Zuweisungen bei Vereinigungen optionaler Eigenschaften inferiert werden, wobei der gemeldete Regressionsbereich und Commit #31708 als Einstiegspunkt dienen. Als erledigt gilt die Aufgabe, wenn Zuweisungen von undefined und null den genannten Erwartungen folgen, ohne das Verhalten von exactOptionalPropertyTypes zu beeinträchtigen.
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
- 35/100