You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TypeScript shows strictNullChecks errors for "possible 'null'" and "possible 'undefined'" values along a property access chain, even though the code is protected by a type predicate using optional chaining that guarantees the property access chain is valid.
🔎 Search Terms
NonNullable type predicate optional chaining
🕗 Version & Regression Information
This is the behavior in every version I tried (from 3.7.5. to 4.9.0 nightly), and I reviewed the FAQ for entries about type system behaviour, type guards and save navigation operator.
typeSomeType={prop?: {nestedProp?: string}}declarefunctionsomeRandomFunc(...args: any[]): SomeType|null;constisDefinedAndNotNull=<T,>(val: T|null|undefined): val is NonNullable<T>=>val!=null;constinstance=someRandomFunc();if(isDefinedAndNotNull(instance?.prop?.nestedProp)){// Unexpected strictNullChecks errors on the line below:// - instance: Object is possibly 'null'// - instance: Object is possibly 'undefined'// - prop: Object is possibly 'undefined'someRandomFunc(instance.prop.nestedProp);}if(isDefinedAndNotNull(instance)&&isDefinedAndNotNull(instance?.prop)&&isDefinedAndNotNull(instance?.prop?.nestedProp)){// No strictNullChecks errors here, but too much unnecessary code abovesomeRandomFunc(instance.prop.nestedProp);}
This seems like an expected limitation, given that checking the value of a property doesn't narrow the containing object in general (except for discriminated unions). I note that there are potentially multiple steps of inference required:
* instance?.prop?.nestedProp is known to not be undefined, therefore * instance?.prop must also be non-nullish, therefore * instance must also be non-nullish
It would indeed be nice if this pattern worked, but I don't know if the control-flow analysis machinery as it exists today is capable of making these inferences.
edit: I'll leave this here for posterity but this analysis of the situation was wrong - see below.
Thanks for the comments. @RyanCavanaugh's comments on the original issue gives me hope this might be fixed in the future, and there's no need for me to leave this duplicate issue open.
Bug Report
TypeScript shows strictNullChecks errors for "possible 'null'" and "possible 'undefined'" values along a property access chain, even though the code is protected by a type predicate using optional chaining that guarantees the property access chain is valid.
🔎 Search Terms
NonNullable type predicate optional chaining
🕗 Version & Regression Information
⏯ Playground Link
Playground Link: Provided
💻 Code
Compiler Options
🙁 Actual behavior
TypeScript emits errors about possible null / undefined values along a property access chain.
This is unexpected as the code is guarded by a type predicate.
🙂 Expected behavior
No strictNullChecks errors.
The text was updated successfully, but these errors were encountered: