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
type checking after instainceOf condition in else block is working incorrectly #5523
#5442 only has changes typeof type guards and composite type guards.
But this behavior is actually by spec - with instanceof type guards, we only affirmatively narrow (so in the true branch) - the logic being that since TS uses structural typing, even if instanceof catches something, instanceofnot catching something doesn't mean that the thing you're checking is not a given type. (In effect, val instanceof Array does not imply that val does not have the type of an array, since it could still have the shape of an array but not the same constructor.) User-defined type guards are more strict, as we assume when you make a type predicate that it's capable of being exhaustive, and typeof is more strict because primitives are effectively nominal.
Activity
sandersn commentedon Nov 4, 2015
#5442 should fix this, I think.
weswigham commentedon Nov 4, 2015
#5442 only has changes
typeoftype guards and composite type guards.But this behavior is actually by spec - with
instanceoftype guards, we only affirmatively narrow (so in thetruebranch) - the logic being that since TS uses structural typing, even ifinstanceofcatches something,instanceofnot catching something doesn't mean that the thing you're checking is not a given type. (In effect,val instanceof Arraydoes not imply that val does not have the type of an array, since it could still have the shape of an array but not the same constructor.) User-defined type guards are more strict, as we assume when you make a type predicate that it's capable of being exhaustive, andtypeofis more strict because primitives are effectively nominal.In the example above, if you do this:
it will narrow as you desire. Prefer
typeoftoinstanceofwhere possible because of this.RyanCavanaugh commentedon Nov 4, 2015
#1719 is the issue for this