microsoft / microsoft/TypeScript
Control flow analysis for dependent parameters only works when checking against literals
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
🔎 Search Terms
narrow, parameters, typeof
🕗 Version & Regression Information
It never worked for typeof / asserts checks since the feature introduction in https://github.com/microsoft/TypeScript/pull/47190; it's been mentioned here:
https://github.com/microsoft/TypeScript/pull/47190#issuecomment-1041216006
⏯ Playground Link
💻 Code
const fnLiteral: (...args: [`a`, number] | [`b`, string]) => void = (a, b) => {
if (a === "a") {
console.log(b);
// ^? number
}
}
const fnAssert: (...args: [`a`, number] | [`b`, string]) => void = (a, b) => {
if (isA(a)) {
console.log(b);
// ^? number | string
}
}
const fnTypeof: (...args: [number, number] | [string, string]) => void = (a, b) => {
if (typeof a === "number") {
console.log(b);
// ^? number | string
}
}
function isA(val: unknown): val is `a` {
return true;
}
🙁 Actual behavior
Both fnAssert and fnTypeof are unable to get TS to recognize b as its narrowed type (number). Only if a is checked against a literal does the narrowing works.
🙂 Expected behavior
The b parameter should be narrowed to number.
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
Beginnen Sie mit der bereitgestellten TypeScript Playground-Reproduktion und der Kontrollflussarbeit in PR #47190, wobei Sie Literalprüfungen mit den typeof- und Type-Predicate-Fällen vergleichen. Das Issue ist abgeschlossen, wenn der abhängige Parameter b sowohl in fnAssert als auch in fnTypeof auf number eingegrenzt wird und damit dem Verhalten der Literalprüfung entspricht.
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