useThrowNewError
Summary
Section titled Summary- Rule available since:
v1.8.0 - Diagnostic Category:
lint/style/useThrowNewError - This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Same as
unicorn/throw-new-error
- Same as
Description
Section titled DescriptionRequire new when throwing an error.
While it’s possible to instantiate Error without using the new keyword, it’s better to be consistent: modern builtins require new to be instantiated.
Rule matches errors when their name ends with the word “Error” and the first character is uppercase.
Examples
Section titled ExamplesInvalid
Section titled Invalidthrow Error();code-block.js:1:7 lint/style/useThrowNewError FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use new Error() instead of Error() when throwing an error.
> 1 │ throw Error();
│ ^^^^^^^
2 │
ℹ Instantiate Error with new keyword for consistency with modern builtins.
ℹ Unsafe fix: Add new keyword.
1 │ throw·new·Error();
│ ++++
throw TypeError('biome');code-block.js:1:7 lint/style/useThrowNewError FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use new TypeError() instead of TypeError() when throwing an error.
> 1 │ throw TypeError(‘biome’);
│ ^^^^^^^^^^^^^^^^^^
2 │
ℹ Instantiate Error with new keyword for consistency with modern builtins.
ℹ Unsafe fix: Add new keyword.
1 │ throw·new·TypeError(‘biome’);
│ ++++
throw lib.TypeError();code-block.js:1:7 lint/style/useThrowNewError FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use new TypeError() instead of TypeError() when throwing an error.
> 1 │ throw lib.TypeError();
│ ^^^^^^^^^^^^^^^
2 │
ℹ Instantiate Error with new keyword for consistency with modern builtins.
ℹ Unsafe fix: Add new keyword.
1 │ throw·new·lib.TypeError();
│ ++++
Valid
Section titled Validthrow new Error();throw new TypeError('biome');throw new lib.TypeError();How to configure
Section titled How to configure{ "linter": { "rules": { "style": { "useThrowNewError": "error" } } }}