Skip to content

typescript/only-throw-error Pedantic

🚧 An auto-fix is still under development.

What it does

This rule disallows throwing non-Error values.

Why is this bad?

It's considered good practice to only throw Error objects (or subclasses of Error). This is because Error objects automatically capture a stack trace, which is useful for debugging. Additionally, some tools and environments expect thrown values to be Error objects.

Examples

Examples of incorrect code for this rule:

ts
throw "error"; // throwing string

throw 42; // throwing number

throw true; // throwing boolean

throw { message: "error" }; // throwing plain object

throw null; // throwing null

throw undefined; // throwing undefined

const error = "Something went wrong";
throw error; // throwing non-Error variable

Examples of correct code for this rule:

ts
throw new Error("Something went wrong");

throw new TypeError("Invalid type");

throw new RangeError("Value out of range");

// Custom Error subclasses
class CustomError extends Error {
  constructor(message: string) {
    super(message);
    this.name = "CustomError";
  }
}
throw new CustomError("Custom error occurred");

// Variables that are Error objects
const error = new Error("Error message");
throw error;

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny typescript/only-throw-error
json
{
  "rules": {
    "typescript/only-throw-error": "error"
  }
}

References

Released under the MIT License.