Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Rule proposal: prevent an explicit: boolean return type annotation if a predicate return type could be inferred #9764

Open
Labels
accepting prsGo ahead, send a pull request that resolves this issueenhancement: new plugin ruleNew rule request for eslint-pluginpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin
@bradzacher

Description

@bradzacher

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
  • My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Description

TS 5.5 introducedInferred Type Predicates.
TS will try to infer a predicate return type if:

  • The function does not have an explicit return type or type predicate annotation.
  • The function has a single return statement and no implicit returns.
  • The function does not mutate its parameter.
  • The function returns a boolean expression that’s tied to a refinement on the parameter.

The first point is the kicker -- a lot of people (us included) annotate return types explicitly.
Or they'll annotate their own predicates.

We should introduce a rule that warns if:

  • the function matches the other 3 conditions
  • the function has an explicit return type

This would allow people to lean on TS's inference for much safer code -- explicit type predicates are horridly unsound! TS only enforces that the predicate is assignable to the variable's original type -- not that the predicate is correct.

For example this code is TS valid but it is WILDLY incorrect and will crash at runtime:

constx=[1,2,null].filter((x):x isnumber=>x==null);

Fail Cases

constx=[1,2,null].filter((x):boolean=>x!=null);

Pass Cases

constx=[1,2,null].filter(x=>x!=null);// perhaps configurable to allow this?// by default inferring the predicate would probably be better as it's more type safeconstx=[1,2,null].filter((x):x isNonNullable<typeofx>=>x!=null);

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: new plugin ruleNew rule request for eslint-pluginpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp