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:
unknown, null, incorrect, conditional, type inference
Expected behavior:
There should not be an error when using a conditional to correctly narrow down an unknown variable to a variable of type object.
Actual behavior:
Given a variable foo of type unknown, it is correctly inferred to be of type object when the following are both true:
typeof foo === 'object'
foo !== null
However, when the order is swapped, the type of foo is incorrectly inferred to be of type object | null.
Related Issues:
Could not find related issues.
Code
constfoo={}asunknown;letbar: object;if(typeoffoo==='object'&&foo!==null){bar=foo;// type of 'foo' is correctly inferred to be 'object'}if(foo!==null&&typeoffoo==='object'){bar=foo;// type of 'foo' is incorrectly inferred to be 'object | null'}
Output
"use strict";constfoo={};letbar;if(typeoffoo==='object'&&foo!==null){bar=foo;// type of 'foo' is correctly inferred to be 'object'}if(foo!==null&&typeoffoo==='object'){bar=foo;// type of 'foo' is incorrectly inferred to be 'object | null'}
Uh oh!
There was an error while loading. Please reload this page.
TypeScript Version: 3.8.3
Search Terms:
unknown, null, incorrect, conditional, type inference
Expected behavior:
There should not be an error when using a conditional to correctly narrow down an
unknown
variable to a variable of typeobject
.Actual behavior:
Given a variable
foo
of typeunknown
, it is correctly inferred to be of typeobject
when the following are bothtrue
:typeof foo === 'object'
foo !== null
However, when the order is swapped, the type of
foo
is incorrectly inferred to be of typeobject | null
.Related Issues:
Could not find related issues.
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: