microsoft / microsoft/TypeScript
Possible to narrow type of object literal property with strictNullChecks?
Offen
@sandersn arbeitet bereits daran.
Seit 30.1.2018.
Committed
Suggestion
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
TypeScript Version: Current playground (2.6?)
Code
type Foo = {
bar?: number[];
}
let f1: Foo = {};
f1.bar = [];
f1.bar.push(0);
let f2: Foo = { bar: [] };
f2.bar.push(0);
Expected behavior:
Both references to .bar compile successfully.
Actual behavior:
Second reference (f2.bar.push(0)) fails with "object is possibly undefined" -- the type of f2.bar is still treated as number[] | undefined even though it obviously can't be undefined.
This might not be an actual bug as such but whatever magic allows the compiler to narrow the type of f1.bar after assigning to it, should also be able to narrow the type when it was assigned in the literal.
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.
Bewertung
Dieses Issue wurde noch nicht bewertet.