close

prefer-type-error

Unreleased

Configuration

PresetConfigured Value
✅ unicornPlugin.configs.recommended"error"
rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/prefer-type-error': 'error',
    },
  },
]);

Rule Details

Require TypeError instead of the generic Error when an if condition is exclusively checking a value's type and the branch contains only that throw.

The rule recognizes typeof, non-error instanceof checks, the global isNaN() and isFinite() functions, and common .isType()-style helpers used by JavaScript libraries. Existence checks such as typeof window !== 'undefined' are not treated as type checks.

Examples of incorrect code:

if (Array.isArray(value)) {
  throw new Error('Expected a non-array value.');
}

Examples of correct code:

if (Array.isArray(value)) {
  throw new TypeError('Expected a non-array value.');
}

Options

This rule has no options. It is enabled at error severity in unicornPlugin.configs.recommended.

Differences from upstream

Rslint resolves built-in names before offering the rewrite. A locally shadowed Error is not reported, and bare isNaN or isFinite calls are recognized only when they resolve to the environment globals. If TypeError is unavailable or locally shadowed, the diagnostic is still reported but the autofix is withheld rather than rewriting to a different binding.

These checks avoid binding-changing autofixes while preserving the upstream behavior for normal global built-ins.

Original Documentation