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
Search Terms: type guard unknown null typeof object
Code
declareconstsomething: unknown;letobj: object;if(typeofsomething==="object"&&something!==null){// Works fine ✓obj=something;}if(something!==null&&typeofsomething==="object"){// 💥 Type 'object | null' is not assignable to type 'object'.obj=something;}
Expected behavior:
The order in which the type not null guard and a typeof guard is applied, should not matter since none of them have side-effects and the and operator is commutative.
Actual behavior:
When applying a "strict not null" (!== null) type guard before a typeof object guard (typeof .. === "object") the not null guard is forgotten.
TypeScript Version: 4.1.0-dev.20200920
Search Terms: type guard unknown null typeof object
Code
Expected behavior:
The order in which the type not null guard and a typeof guard is applied, should not matter since none of them have side-effects and the and operator is commutative.
Actual behavior:
When applying a "strict not null" (
!== null
) type guard before a typeof object guard (typeof .. === "object"
) the not null guard is forgotten.Playground Link: Link to playground.
Related Issues: This is the closest I got #32798, but it seems to be caused specifically by a non-strict equal operator.
The text was updated successfully, but these errors were encountered: