Skip to content

unicorn/prefer-type-error Pedantic

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

What it does

Enforce throwing a TypeError instead of a generic Error after a type checking if-statement.

Why is this bad?

Throwing a TypeError instead of a generic Error after a type checking if-statement is more specific and helps to catch bugs.

Example

Examples of incorrect code for this rule:

javascript
if (Array.isArray(foo)) {
  throw new Error("Expected foo to be an array");
}

Examples of correct code for this rule:

javascript
if (Array.isArray(foo)) {
  throw new TypeError("Expected foo to be an array");
}

How to use

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

bash
oxlint --deny unicorn/prefer-type-error
json
{
  "rules": {
    "unicorn/prefer-type-error": "error"
  }
}

References

Released under the MIT License.