Microsoft/TypeScript
Ver no GitHubControl flow analysis for dependent parameters only works when checking against literals
Open
#55.766 aberto em 17 de set. de 2023
Domain: check: Control FlowHelp WantedPossible Improvement
Métricas do repositório
- Stars
- (48.455 stars)
- Métricas de merge de PR
- (Mesclagem média 6d 17h) (9 fundiu PRs em 30d)
Description
🔎 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