microsoft / microsoft/TypeScript
TypeScript doesn't narrow out `undefined` after constructor call when callee is `any`
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
TypeScript doesn't seem to narrow the type of a variable correctly in cases where the constructor being called is typed as any. If the type of a variable includes undefined in its type signature, it's not narrowed out after a constructor call even though by definition (I think--correct me if I'm wrong) new fn() must always evaluate to an object if it doesn't throw first.
TypeScript Version: 3.1.3
Search Terms: constructor narrow
Code
let x: { [x: string]: any } | undefined;
x = new (Object as any)();
// AFAIK `x` can't possibly be undefined after this point
console.log(x.foo); // error: Object is possibly 'undefined'.
Expected behavior:
No error.
Actual behavior:
Compile-time error: Object is possibly 'undefined'.
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
Beginnen Sie mit dem TypeScript Playground-Repro und der im Issue gezeigten Zuweisung new (Object as any)(). Verfolgen Sie, wie Control-Flow-Narrowing Konstruktoraufrufe behandelt, deren callee any ist, und überprüfen Sie anschließend, dass das Beispiel nicht mehr Object is possibly 'undefined' meldet, ohne das Verhalten des davon unabhängigen Narrowings 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
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100