Skip to content

Rule proposal: no-unnecessary-type-predicate #10398

@elliotleelewis

Description

@elliotleelewis

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

I think there would be value in having a rule to avoid extra type-predicate checks when they're not necessary. This can help clean up type-predicates around a repo as types are changed, and also avoid unnecessary CPU cycles at runtime.

This rule would work similarly to no-unnecessary-type-assertion.

Fail Cases

const test: "foo" = "foo";

const isFoo = (text: string): text is "foo" => {
    return text === "foo";
}

// Should error here: at compile time we know the else branch will never get hit
if (isFoo(test)) {
    console.log("FOO!");
} else {
    console.log("BAR!");
}

Pass Cases

const test: string = "bar";

const isFoo = (text: string): text is "foo" => {
    return text === "foo";
}

// Shouldn't error here: at compile time we cannot know if the else branch will be hit, and so the predicate usage makes sense.
if (isFoo(test)) {
    console.log("FOO!");
} else {
    console.log("BAR!");
}

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already existsenhancement: new plugin ruleNew rule request for eslint-pluginlocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugintriageWaiting for team members to take a look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy