-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Open
Labels
Help WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
Description
π Search Terms
narrow, parameters, typeof
π Version & Regression Information
It never worked for typeof
/ asserts
checks since the feature introduction in #47190; it's been mentioned here:
β― 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
Ayc0 and danvk
Metadata
Metadata
Assignees
Labels
Help WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases