Skip to main content

instanceOf

Callable

  • instanceOf<T>(object: unknown, declaration: Constructor<T>, loose?: boolean): object is T

  • Performs a loose instance check by comparing class names up the prototype chain if instanceof initially fails. To disable this loose check, pass false as the 3rd argument.

    import { instanceOf } from '@boost/common';

    if (instanceOf(error, Error)) {
    console.log(error.stack);
    }

    Generics can be used to type the object being checked. This will default to the declaration passed to the 2nd argument.

    instanceOf<ParseError>(error, Error);

    Loose checks can be useful if multiple copies of the same class declaration exists in the module tree. For example, multiple versions of the same package are imported.


    Type parameters

    • T = unknown