Skip to content

TypeScript is not able to infer the type narrowing from method #44591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
iRealNirmal opened this issue Jun 15, 2021 · 5 comments
Closed

TypeScript is not able to infer the type narrowing from method #44591

iRealNirmal opened this issue Jun 15, 2021 · 5 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@iRealNirmal
Copy link

iRealNirmal commented Jun 15, 2021

Bug Report

🔎 Search Terms

Type narrowing

🕗 Version & Regression Information

TS - 4.3.2
(Regression not aware)

💻 Code

  private _enabled(): boolean {
    return this.maxlength ? true : false;
  }

  private _createValidator(): void {
    const maxlength = this.maxlength;
    this._validator = (this._enabled()) ? validator(toInteger(maxlength)) : null;
  }

🙁 Actual behavior

When using method getting error.

Argument of type 'string | number | null' is not assignable to parameter of type 'string | number'. while same thing is working when using variable directly.

🙂 Expected behavior

Narrowing should work whether we use method or property.

@MartinJohns
Copy link
Contributor

This just sounds like an issue with the library you use. Without a reproducible example we can't tell you what's wrong, but most likely TypeScript works as it should.

@wangweixuan
Copy link

To make this narrowing, the compiler has to look into _enabled method and perform interprocedural analysis. That might be too costly and hard to implement. Currently type narrowing applies mostly to local variables.
For this problem I would recommend you to use non-null assertion on maxlength.

@MartinJohns
Copy link
Contributor

Performing analysis for every method would be way too costly. I think the most close would be something like #38390, which aims to automatically infer type guards for simple cases.

@MartinJohns
Copy link
Contributor

If it's costly process at least can we suggest to either use non-null assertion ?

I don't understand the question. You're free to use the non-null assertion operator whenever in your code.

@RyanCavanaugh RyanCavanaugh added Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript labels Jun 15, 2021
@RyanCavanaugh
Copy link
Member

We don't inline method bodies for CFA purposes, and even if we did, we don't retroactively apply narrowings on the basis of assuming non-mutation of the initializing reference.

We don't automatically suggest ! because we want you to actually think about whether something might be null or not 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants