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.
Activity
DanielRosenwasser commentedon Sep 14, 2018
It's kind of funny, but we were actually talking about #18196 right as you were opening this issue.
But filing this as a duplicate of #18196
ljharb commentedon Sep 18, 2018
This may be addressed by #27157; it can be closed or left open til the next release, as you like.
typescript-bot commentedon Dec 14, 2018
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.