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
I work in a codebase that recommends adding explicit types. In this case, adding a type that is the same as the inferred type (as claimed by the hover tooltips) causes narrowing to fail for a previous maybe-null type that depends on the result that has the explicit type.
🔎 Search Terms
explicit type, narrowing
🕗 Version & Regression Information
Prior to 4.4, both explicit & inferred types would complain that obj could be null.
After 4.4 (and up to 5.0-nightly), the inferred type behaves correctly, but the explicit type still does not see that obj can no longer be null.
interfaceDeeplyNestedObject{deeply: {nested: {object: string;}}}functionexample(): DeeplyNestedObject|null{returnMath.random()>0.5 ? {deeply: {nested: {object: 'hi'}}} : null;}functionexplicit(): DeeplyNestedObject|undefined{constobj=example();// Explicit type which is the same as the inferred typeconstresult: string|undefined=obj?.deeply?.nested?.object;if(!result){returnundefined;}// obj can no longer be null, as we've validated that result is truthy which is only possible if obj != null.// ts2322: Type 'DeeplyNestedObject | null' is not assignable to type 'DeeplyNestedObject | undefined'.// Type 'null' is not assignable to type 'DeeplyNestedObject | undefined'.returnobj;}functioninferred(): DeeplyNestedObject|undefined{constobj=example();// Inferred typeconstresult=obj?.deeply?.nested?.object;if(!result){returnundefined;}returnobj;}
🙁 Actual behavior
TypeScript claims that obj can still be null, even though we've eliminated that possibility by performing some optional chaining with obj and then checking to make sure that the result was truthy.
🙂 Expected behavior
The same as the inferred example - no compiler errors.
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, [..]
Bug Report
I work in a codebase that recommends adding explicit types. In this case, adding a type that is the same as the inferred type (as claimed by the hover tooltips) causes narrowing to fail for a previous maybe-null type that depends on the result that has the explicit type.
🔎 Search Terms
explicit type, narrowing
🕗 Version & Regression Information
Prior to 4.4, both explicit & inferred types would complain that
obj
could be null.After 4.4 (and up to 5.0-nightly), the inferred type behaves correctly, but the explicit type still does not see that
obj
can no longer be null.⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
TypeScript claims that
obj
can still be null, even though we've eliminated that possibility by performing some optional chaining withobj
and then checking to make sure that the result was truthy.🙂 Expected behavior
The same as the
inferred
example - no compiler errors.The text was updated successfully, but these errors were encountered: