isError()
isError(
value):value is Error
Checks if a value is an Error instance.
note
Also returns true for Error subclasses (TypeError, RangeError, custom errors).
Parametersโ
value: unknownโ
The value to check.
Returns: value is Errorโ
true if the value is an Error, false otherwise.
Sinceโ
2.0.0
Exampleโ
isError(new Error('oops')); // => true
isError(new TypeError('bad')); // => true
isError({ message: 'fake' }); // => false
isError('error string'); // => false
How it works?โ
Type guard that checks if a value is an Error instance.
Type Narrowingโ
Common Checksโ
| Value | Result |
|---|---|
new Error('msg') | true |
new TypeError('msg') | true |
new RangeError('msg') | true |
new SyntaxError('msg') | true |
{ message: 'error' } | false |
'Error' | false |
Use Casesโ
Catch exceptions correctly ๐โ
Distinguish between standard Error objects and other thrown values. Critical for robust error handling and logging.
try {
dangerousCall();
} catch (err) {
if (isError(err)) {
logError(err.message, err.stack);
}
}
true
false