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
Union Type inference not working in class (ts version 1.4) #1706
Type guards only work on simple variables, not properties of an object, so you need to copy the property into a local variable.
typeNameOrNameArray=string|string[];classNameCreator{constructor(publicname: NameOrNameArray){}createName(): string{varname=this.name;// Copy into variableif(typeofname==="string"){returnname;}else{name.forEach(elem=>{console.log(elem);});returnname.join(" ");}}}
We have this restriction because we want to track (to the best of our abilities) that there are no assignments to the variable in the guarded section. This is pretty much impossible to do correctly for properties without extensive flow and alias analysis. For example:
This is just a simple example. You can construct increasingly impossible ones with little effort.
A possible alternative to our current design is to allow type guards to work on dotted names of the form x.y.z or this.x.y.z and to ignore the effects of assignments to such names (or parts of them). But it would mean that we wouldn't catch errors like the one in the example above.
Thanks, i get a much better understanding of type guard. The use of class properties or 'global variables' doesn't really make sense as it's pretty much impossible to make sure that the condition typeof this.name === "string" will still be true accross the type guarded scope. So i guess it'll still be confusing to allow dotted name in type guards.
We still need to work out the exact semantics of how this would work. It's less clear exactly what forms should be allowed since non-local effects are much easier on class members. Consider, for example:
classThing{x: string|number;ensureXisString(){this.x=this.x.toString();}someMethod(){if(typeofthis.x==='number'){this.ensureXisString();// Narrowed to this.x: number here, which is 100% wrong}}}
Activity
erichillah commentedon Jan 17, 2015
I am able to remove the error only by using explicit type assertion
ahejlsberg commentedon Jan 17, 2015
Type guards only work on simple variables, not properties of an object, so you need to copy the property into a local variable.
We have this restriction because we want to track (to the best of our abilities) that there are no assignments to the variable in the guarded section. This is pretty much impossible to do correctly for properties without extensive flow and alias analysis. For example:
This is just a simple example. You can construct increasingly impossible ones with little effort.
A possible alternative to our current design is to allow type guards to work on dotted names of the form
x.y.zorthis.x.y.zand to ignore the effects of assignments to such names (or parts of them). But it would mean that we wouldn't catch errors like the one in the example above.erichillah commentedon Jan 18, 2015
Thanks, i get a much better understanding of type guard. The use of class properties or 'global variables' doesn't really make sense as it's pretty much impossible to make sure that the condition
typeof this.name === "string"will still be true accross the type guarded scope. So i guess it'll still be confusing to allow dotted name in type guards.antoinerousseau commentedon Jan 22, 2015
Hi, is this issue going to be fixed?
RyanCavanaugh commentedon Jan 22, 2015
We still need to work out the exact semantics of how this would work. It's less clear exactly what forms should be allowed since non-local effects are much easier on class members. Consider, for example:
danieljsinclair commentedon Jan 26, 2015
It would be nice if type guards recognised
Array.isArray()this doesn't seem to work either (v1.4)
instanceof Arrayahejlsberg commentedon Jan 26, 2015
Daniel J Sinclair (@danieljsinclair) The
x instanceof Arrayissue was fixed in #1657.RyanCavanaugh commentedon Apr 27, 2015
Tentatively approved. Need to see how this impacts real-world code.
jakebailey commentedon Jul 7, 2023
Just looking at old issues; this one was fixed in TS 2.0 when control flow analysis was introduced.