close

throw-new-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/throw-new-error': 'error',
    },
  },
]);

Rule Details

Require new when creating an error.

Calling an error constructor without new still produces an error object, but it is easy to mistake for an ordinary function call, and it breaks as soon as the class is subclassed or its prototype is relied upon.

Although the rule is named for throw, it applies to every call expression, so a bare call such as const error = Error() is reported too.

Examples of incorrect code for this rule:

throw Error();
throw TypeError('message');
throw lib.CustomError();

Examples of correct code for this rule:

throw new Error();
throw new TypeError('message');
throw new lib.CustomError();

Calls that new cannot be applied to are left alone — an optional-chained call such as Error?.() or lib?.Error(), a computed access such as lib["Error"](), and a decorator call such as @RegisterServiceError().

Original Documentation