unicorn/prefer-type-error
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-type-error": "error",
},
},
];Enforce throwing TypeError in type checking conditions.
if (Array.isArray(foo) === false) {
throw new Error('Array expected');
}if (Number.isNaN(foo) === false && Number.isInteger(foo) === false) {
throw new Error('Integer expected');
}if (isNaN(foo) === false) {
throw new Error('Number expected');
}if (Array.isArray(foo) === false) {
throw new TypeError('Array expected');
}if (Number.isNaN(foo) === false && Number.isInteger(foo) === false) {
throw new TypeError('Integer expected');
}if (isNaN(foo) === false) {
throw new TypeError('Number expected');
}Examples from the ESLint documentation.
unicorn/prefer-type-error
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-type-error": "error",
},
},
];unicorn/prefer-type-error
unicorn plugin · pedantic
{
"rules": {
"unicorn/prefer-type-error": "error"
}
}Biome does not have this rule.
Enforce the use of Buffer.from() and Buffer.alloc() instead of the deprecated new Buffer().
Prefer consistent types when spreading a ternary in an array literal.
Disallow instanceof with built-in objects
Require escape sequences to use uppercase or lowercase values.
Prefer .includes() over .indexOf(), .lastIndexOf(), and Array#some() when checking for existence or non-existence.
Require new when creating an error.