Skip to content

Bug: [only-throw-error] TypeOrValueSpecifier does not handle union #11452

@BrendanC23

Description

@BrendanC23

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.4.5&fileType=.ts&code=GYVwdgxgLglg9mABMAjAHgCoD4AUBKRAbwChFEAbAUykUoCc6AuRABTrgFsYBnSzLANylEUABbsA7rQZCAvsWKhIsBMgBMmFABpEGNbgIkyVGvSat2XXpqyIAPhc48%2BewcLGTpdOUA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6AeyfkNvwAto7AO71og6OjABtcNhyIx7CQBpZcrHOyQAhvHjDJMzZsgAFQQFtKyRJDWaAuvYgBfe09cgXQA&tsconfig=&tokens=false

Repro Code

function f1<T>() {
  let err: Promise<T>;
  throw err;
}

function f2<T1, T2>() {
  let err: Promise<T1> | Promise<T2>;
  throw err;
}

ESLint Config

{
  "rules": {
    "@typescript-eslint/only-throw-error": [
      "error",
      {
        "allow": [
          "Promise"
        ]
      }
    ]
  }
}

tsconfig

Expected Result

The only-throw-error warning should not be triggered by f1() or f2(). When the "allow" option is Promise, I should be able to throw an error of Promise<A> | Promise<B>.

Actual Result

The "only-throw-error" rule triggers when a union is thrown.

Expected an error object to be thrown. 8:9 - 8:12

Expected an error object to be thrown. 8:9 - 8:12

Additional Info

My actual use-case is with TanStack Router, which allows users to throw redirect(...) (this throws a Redirect type). I have a function that can return various redirects (which are later thrown):

function getRedirect(a, b) {
    if (a) return redirect(...); // returns Redirect<A>
    else return redirect(...); // returns Redirect<B>
}

This returns a union of Redirect<A> | Redirect<B>. The following code triggers and ESLint error because it is trying to throw a union type.

throw getRedirect(a, b);

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpackage: 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