Function isError

  • Checks if a value is an instance of Error or a specific error type.

    Type Parameters

    • T extends Error

      A specific error type that extends the Error class.

    Parameters

    • value: unknown

      The value to check. It can be of any type.

    • OptionalerrorType: new (...args: any[]) => T

      (Optional) A specific error type to validate against.

    Returns value is T

    Returns true if the value is an instance of Error or the specified error type, otherwise false.

    const error = new Error('An error occurred');
    console.log(isError(error)); // true

    const typeError = new TypeError('A type error occurred');
    console.log(isError(typeError, TypeError)); // true