Skip to content

Latest commit

Β 

History

History
42 lines (28 loc) Β· 1.2 KB

throw-new-error.md

File metadata and controls

42 lines (28 loc) Β· 1.2 KB

throw-new-error

πŸ“ Require new when creating an error.

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ”§ This rule is automatically fixable by the --fix CLI option.

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

This rule identifies error constructors by names such as Error, TypeError, and CustomError. Calls whose results are immediately called are ignored because they may be factories.

When TypeScript type information is available, calls that return a function or class are also ignored, including factories whose result is stored for later use.

Examples

// ❌
const error = Error('unicorn');

// βœ…
const error = new Error('unicorn');
// ❌
throw TypeError('unicorn');

// βœ…
throw new TypeError('unicorn');
// ❌
throw lib.TypeError('unicorn');

// βœ…
throw new lib.TypeError('unicorn');