Closed
Description
TypeScript Version: latest
Search Terms: n/a
Code
function f(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 (typeof a !== 'object') {
// `a` here could be the following values:
// - a function
// - `true``
// - a non-zero non-`NaN`` number
// - a non-empty string
// - a symbol
return a; // however, the type inferred here is `never`
}
// here, `a` has a verifiably certain type of `object`.
// because of the type guard above.
return a; // 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.
Playground Link: link
Related Issues: not sure how to search for it