Closed
Description
π Search Terms
parameter symbol assignment binding pattern dependent narrowing variables
π Version & Regression Information
- This is the behavior in every version I tried
β― Playground Link
π» Code
function test([x, y]: [1, 2] | [3, 4]) {
if (Math.random()) {
y = 2;
}
if (y === 2) {
x
// ^? (parameter) x: 1
}
}
// an example valid call that might lead to a runtime error in the called function
test([3, 4]);
π Actual behavior
Assignment to y
keeps narrowing x
as if y
was never mutated. Effectively the binding pattern in this parameter is treated as const
variable
π Expected behavior
I'd expect this assignment to prevent dependent narrowing altogether.
Additional information about the issue
Assignment to x
actually prevents dependent narrowing in a scenario like this: TS playground. It's just that those assignments are tracked per symbol and not per the root declaration. I believe that the simplest fix would be to just mark the whole root declaration as reassigned and treat all contained variables as non-const based on that.