toolbox icon indicating copy to clipboard operation
toolbox copied to clipboard

isNull Guard on Nested Nullable Chain

Open ASafaeirad opened this issue 2 years ago • 1 comments

Describe the bug

The isNull guard is not working for the nested nullable chain.

Expected behavior

The following code should compile with the --strict flag.

function f(x: {
  foo: { bar: (string | undefined)[] | undefined } | undefined;
}) {
  if (isNull(x.foo?.bar?.[0])) return;
  x.foo.bar[0].toLowerCase();
}

Actual behavior

It throws the "Object is possibly 'undefined" error.

ASafaeirad avatar Aug 26 '22 08:08 ASafaeirad

Seems it's not possible for now: https://github.com/microsoft/TypeScript/issues/34974

ASafaeirad avatar Jun 29 '23 20:06 ASafaeirad

Fixed by https://github.com/microsoft/TypeScript/pull/55613

ASafaeirad avatar Jul 03 '24 10:07 ASafaeirad