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
interfaceFoo{a: string|null;}functionbar(foo: Foo|null){constisNullish=foo?.a==null;if(isNullish){return;}// report "Object is possibly 'null'.(2531)" for "foo.a"console.log(foo.a.toUpperCase());}
π Actual behavior
Report an error Object is possibly 'null'.(2531) for foo.a
π Expected behavior
No error is reported
Additional information about the issue
I don't know this behavior is a bug or intended, but based on PR #44730 , I chose 'bug' for this issue.
The text was updated successfully, but these errors were encountered:
Narrowing through indirect references occurs only when the conditional expression or discriminant property access is declared in a const variable declaration with no type annotation, and the reference being narrowed is a const variable, a readonly property, or a parameter for which there are no assignments in the function body.
So just mark the property a as readonly and it works.
π Search Terms
"optional chaining operator", null check
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.5.0-dev.20240523#code/JYOwLgpgTgZghgYwgAgGIHt3IN4ChkHJwBcyAzmFKAObIA+yIArgDYsDcuAvrjEyAjDB0IZACM4UABQxMpDFgbM2AShz5CCERWTAyAOVYs9AC2QBeZLPQB+AHRwLl5Rw0FgMZFL2G2ptXiEQchQEGBMUCCcQTxBAPRxIRAADuhQYMgARADyYgBWEIK6ZMipZGTAYiwAnsgA5C51dlIATACsAMwAjCqZVmlZ1g6ZbshaIGToLBB2LOjUMpgOdmDoAKrJydAAwnBkEFIqKpw8QA
π» Code
π Actual behavior
Report an error
Object is possibly 'null'.(2531)
forfoo.a
π Expected behavior
No error is reported
Additional information about the issue
I don't know this behavior is a bug or intended, but based on PR #44730 , I chose 'bug' for this issue.
The text was updated successfully, but these errors were encountered: