Closed as not planned
Description
π Search Terms
nullable items nested typeguard type guards
π Version & Regression Information
- This changed between versions 0 and 5.6.3
β― Playground Link
π» Code
type FooBar = {
bar: string | null;
};
type NullableItem = {
foo: FooBar | null;
};
type NotNullableItem = {
foo: FooBar;
};
const itemHasFoo = (item: NullableItem): item is NotNullableItem => {
return item.foo !== null;
};
const items = [
{
foo: {
bar: 'hello',
},
},
{ foo: null },
];
const logArrayOfItems = (funcItems: NotNullableItem[]) => {
console.log(funcItems);
};
const filteredItems = items.filter(itemHasFoo);
logArrayOfItems(filteredItems);
π Actual behavior
Type guard itemHasFoo does not narrow type to NotNullableItem, but when FooBar
is changed to:
type FooBar = {
bar: string;
};
It works fine, something about the nullable property of FooBar seems to stop the inference of that type correctly.
π Expected behavior
I expect the typeguard to correctly narrow the type to NotNullableItem
and therefore not error.
Additional information about the issue
No response