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
functionf(a?: {}){if(!a){// filters out falsy values:// - `null`// - `undefined`// - `false`// - `NaN`// - `0` / `-0`// - `''`return{};}// here, the type of `a` has been narrowed from "optional {}"// to just "{} minus `object`" (object-coercible, ie, anything but null/undefined, but not `object`)// effectively, this is `string | number | symbol | boolean | Function`if(typeofa!=='object'){// `a` here could be the following values:// - a function// - `true``// - a non-zero non-`NaN`` number// - a non-empty string// - a symbolreturna;// however, the type inferred here is `never`}// here, `a` has a verifiably certain type of `object`.// because of the type guard above.returna;// however, the type is reported as `{}` }
Expected behavior: The type of a inside the if block will be either {}, or better, string | number | symbol | boolean | Function.
Actual behavior: The type of a inside the if block is never, which is flat out incorrect.
TypeScript Version: latest
Search Terms: n/a
Code
Expected behavior: The type of
a
inside theif
block will be either{}
, or better,string | number | symbol | boolean | Function
.Actual behavior: The type of
a
inside theif
block isnever
, which is flat out incorrect.Playground Link: link
Related Issues: not sure how to search for it
The text was updated successfully, but these errors were encountered: