Closed
Description
Bug Report
π Search Terms
typeof boolean narrow optional chaining
π Version & Regression Information
- This changed between versions 4.8.4 and 4.9.3
β― Playground Link
Playground link with relevant code
π» Code
type WrappedStringOr<T> = {value?: string} | {value?: T};
function numberOk(wrapped: WrappedStringOr<number> | null) {
if (typeof wrapped?.value !== 'string') {
return null;
}
return wrapped.value;
}
function booleanBad(wrapped: WrappedStringOr<boolean> | null) {
if (typeof wrapped?.value !== 'string') {
return null;
}
// @ts-expect-error Unexpected type error here
return wrapped.value;
}
function booleanFixed(wrapped: WrappedStringOr<boolean> | null) {
// This is semantically equivalent to above format (and gets auto-fixed to the above by formatters like Prettier).
if (typeof (wrapped?.value) !== 'string') {
return null;
}
// No type error here
return wrapped.value;
}
π Actual behavior
The type of wrapped
is narrowed to exclude the {value?: boolean}
case, but doesn't exclude the null
case.
π Expected behavior
The type of of wrapped
is narrowed to both exclude the {value?: boolean}
case and also exclude the null
case.