Skip to content

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

Closed
@iRealNirmal

Description

@iRealNirmal

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions