Skip to content

Enhancement: [no-throw-literal] Specify allowed types #6226

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
4 tasks done
haines opened this issue Dec 16, 2022 · 5 comments
Closed
4 tasks done

Enhancement: [no-throw-literal] Specify allowed types #6226

haines opened this issue Dec 16, 2022 · 5 comments
Labels
accepting prs Go ahead, send a pull request that resolves this issue duplicate This issue or pull request already exists enhancement: plugin rule option New rule option for an existing eslint-plugin rule locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@haines
Copy link
Contributor

haines commented Dec 16, 2022

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

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

https://typescript-eslint.io/rules/no-throw-literal/

Description

I would like to add an additional option to this rule, allowThrowingTypes, that would allow users to override the current hard-coded behaviour of checking for the global Error class.

I'd find this useful in Remix projects, where you can throw a Response object. Currently, I have to disable this rule, when what I would prefer to do is only allow Error or Response objects to be thrown.

I think this could potentially also be useful in codebases with custom error types, where you want to enforce usage of a more specific subtype of Error.

I'm not sure what the syntax of the option should be - you would need be able to specify the import location and type, rather than just matching on the type name. Are there any other rules where it is possible to precisely specify types in this way?

Fail

throw "/login";

Pass

// with allowThrowingTypes: ["Error", "Response"]
// redirect returns Response & { json(): Promise<never> }
throw redirect("/login");

Additional Info

No response

@haines haines added enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Dec 16, 2022
@JoshuaKGoldberg
Copy link
Member

Seems reasonable to me. We have an ongoing discussion about making a unified format for specifying these types: #6017 -> eslint/eslint#16540.

I'm hopeful those will resolve soon. But we should wait to take action on this feature request until then.

@JoshuaKGoldberg JoshuaKGoldberg added blocked by another issue Issues which are not ready because another issue needs to be resolved first and removed triage Waiting for team members to take a look labels Dec 16, 2022
@raulmt
Copy link

raulmt commented Dec 6, 2023

Hi @JoshuaKGoldberg ! It seems a solution for specifying an allow list of types was implemented for another rule related to the mentioned discussion: #4436

Can that implementation unblock this issue now? (btw, I reached to this issue because of the same specific reason as @haines being able to throw Response :) )

@JoshuaKGoldberg
Copy link
Member

Oh, yes! Thanks for the ping 🙂 I'd missed unblocking this issue. Marking as accepting PRs to use that format.

@JoshuaKGoldberg JoshuaKGoldberg added accepting prs Go ahead, send a pull request that resolves this issue and removed blocked by another issue Issues which are not ready because another issue needs to be resolved first labels Dec 6, 2023
@kosiakMD
Copy link

In my case I need to allow throw custom Erros not extended from Error
In NestJS, there are Errors that are not extended from Error but are totally valid for the flow

const error = new ValidationError();
error.target = obj;
error.property = field || key;
error.value = fieldValue;
error.constraints = {...};
error.children = [];
throw error;

@kirkwaiblinger
Copy link
Member

This is duplicated by #9525. Closing in favor of that issue.

@kirkwaiblinger kirkwaiblinger closed this as not planned Won't fix, can't repro, duplicate, stale Oct 11, 2024
@kirkwaiblinger kirkwaiblinger added the duplicate This issue or pull request already exists label Oct 11, 2024
@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Oct 19, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue duplicate This issue or pull request already exists enhancement: plugin rule option New rule option for an existing eslint-plugin rule locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

5 participants