microsoft / microsoft/TypeScript
`unknown` isn’t narrowed by assignment
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
Bug Report
🔎 Search Terms
try block
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about try-catch blocks
⏯ Playground Link
Playground link with relevant code
💻 Code
function f(request: {params: {[param: string]: unknown;}}) {
let x = request.params.x;
try {
x = validate(x);
} catch (e) {}
try {
takesString(x); // Fails to compile, x is of type unknown.
} catch (e) {}
}
function validate(x: unknown): string { return ""; }
function takesString(s: string) {}
🙁 Actual behavior
In this example, the code fails to compile because tsc has deduced x in the second block to be of type unknown
🙂 Expected behavior
Since we have successfully exited the first try-catch block, x's type should be narrowed to string in the second.
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
Führe zuerst das verlinkte TypeScript Playground-Beispiel aus, um den Fehler rund um die beiden try-catch-Blöcke zu reproduzieren. Verfolge dann das Control-Flow-Narrowing des Compilers für Zuweisungen über try-catch-Grenzen hinweg und füge einen Regressionstest für den gezeigten Fall hinzu; fertig ist es, wenn tsc takesString(x) nach dem ersten Block akzeptiert, ohne das Narrowing an anderer Stelle abzuschwächen.
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