Typescript expression can't follow value has been checked for undefined outside of it

See example below that even though I am checking if foundFoo is truthy, inside the function expression, it still sees it as possibly undefined

interface A {
name: string,
value: number
}

interface B {
fk: number
}

let fooArray: Array<A> = [{name: 'a', value: 1}, {name: 'b', value: 2}];
let bazArray: Array<B> = [{fk: 1}, {fk: 2}];
let foundFoo = fooArray.find((entry) => entry.name === 'a');
if (foundFoo) {
bazArray.filter((entry) => entry.fk === foundFoo.value); // foundFoo is marked as possibly undefined
bazArray.forEach((entry) => entry.fk === foundFoo.value); // foundFoo is marked as possibly undefined
}
0
5 comments

what errors can you see namely? For me, the IDE reports no errors/warnings for your code:

0
Avatar
Permanently deleted user

0
Avatar
Permanently deleted user

I am using webstorm 2019.1.2 and in a .ts file

0

The error comes from the Typescript compiler, not from the IDE. You can disable "strictNullChecks" in tsconfig.json to get rid of the issue.

Please see comments in https://github.com/Microsoft/TypeScript/issues/10642, https://github.com/microsoft/TypeScript/issues/29392 for some hints.

If you still believe this is a bug, please file an issue to Microsoft, https://github.com/microsoft/TypeScript/issues

0
Avatar
Permanently deleted user

I will look into that, thanks for the information

0

Please sign in to leave a comment.