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
Implicit type guard by other variables are not always workingΒ #46915
conststate={prop1: 'value1',prop2: 'value2',prop3: 'value3',}typeState=typeofstate;typeKeys=(keyofState)constmakeSetProp=(path: Keys|undefined)=>{// "setProp" is not null if "path" is not undefinedconstsetProp=path ?
(value: string)=>state[path]=value :
null;// "data" is not null is "path" is not undefinedconstdata=path ?
'nextValue' :
null;// "data" is always not null if "setProp" is not null, but the compiler does not recognize itif(setProp)setProp(data);// Argument of type 'string | null' is not assignable to parameter of type 'string'if(data)setProp(data);// Cannot invoke an object which is possibly 'null'.if(path)setProp(data);// Cannot invoke an object which is possibly 'null'. Argument of type 'string | null' is not assignable to parameter of type 'string'.if(setProp&&data)setProp(data);// works}
π Actual behavior
data does not narrow by checking the setProp
setProp does not narrow by checking the data
data and setProp both do not narrow by checking the path even though they were defined by it
π Expected behavior
Logically they all can be narrowed by each other:
data can be narrowed by checking the setProp or path
setProp can be narrowed by checking the data or path
The behavior in #44730 doesn't go as far as a full-blown counterfactual analysis, which is basically what'd be required here. The guarding expression has to be a boolean.
Activity
RyanCavanaugh commentedon Nov 24, 2021
The behavior in #44730 doesn't go as far as a full-blown counterfactual analysis, which is basically what'd be required here. The guarding expression has to be a boolean.