You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Control flow analysis for dependent parameters only works when checking against literalsΒ #55766
constfnLiteral: (...args: [`a`,number]|[`b`,string])=>void=(a,b)=>{if(a==="a"){console.log(b);// ^? number}}constfnAssert: (...args: [`a`,number]|[`b`,string])=>void=(a,b)=>{if(isA(a)){console.log(b);// ^? number | string}}constfnTypeof: (...args: [number,number]|[string,string])=>void=(a,b)=>{if(typeofa==="number"){console.log(b);// ^? number | string}}functionisA(val: unknown): val is `a` {returntrue;}
π 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.
Currently only discriminated unions are supported and it only works if you check against the discriminant. This isn't unique to dependent parameters - it works exactly in the same way with object types: TS playground
Activity
Andarist commentedon Sep 18, 2023
Currently only discriminated unions are supported and it only works if you check against the discriminant. This isn't unique to dependent parameters - it works exactly in the same way with object types: TS playground
danvk commentedon Jan 24, 2024
Dupe of #32399?