Skip to content

unicorn/throw-new-error Style

🛠️ An auto-fix is available for this rule.

What it does

Require new when throwing an error.`

Why is this bad?

While it's possible to create a new error without using the new keyword, it's better to be explicit.

Examples

Examples of incorrect code for this rule:

javascript
throw Error("🦄");
throw TypeError("unicorn");
throw lib.TypeError("unicorn");

Examples of correct code for this rule:

javascript
throw new Error("🦄");
throw new TypeError("unicorn");
throw new lib.TypeError("unicorn");

How to use

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

bash
oxlint --deny unicorn/throw-new-error
json
{
  "rules": {
    "unicorn/throw-new-error": "error"
  }
}

References

Released under the MIT License.