π Prefer Error.isError() when checking for errors.
π« This rule is disabled in the following configs: β
recommended, βοΈ unopinionated.
π§ This rule is automatically fixable by the --fix CLI option.
Error.isError() is a robust way to check whether a value is an error object.
instanceof Error is unreliable across realms, and Object.prototype.toString.call(error) === '[object Error]' can be spoofed with Symbol.toStringTag.
Unlike unicorn/no-instanceof-builtins, this rule also catches legacy Error brand checks using '[object Error]'.
// β
error instanceof Error;
// β
Error.isError(error);// β
Object.prototype.toString.call(error) === '[object Error]';
// β
Error.isError(error);// β
Object.prototype.toString.call(error) !== '[object Error]';
// β
!Error.isError(error);// β
error instanceof TypeError;