Options
All
  • Public
  • Public/Protected
  • All
Menu

Module is/instance

Index

References

Interfaces

Functions

References

Renames and re-exports isInstance

Functions

  • isInstance<T>(declaration: Constructor<T>, target: unknown, loose?: boolean): target is T
  • isInstance<T>(declaration: Constructor<T>): (target: unknown, loose?: boolean) => target 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 { isInstance } from 'tily/is/instance';

    if (isInstance(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.

    isInstance<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.

    example
     isInstance(Foo, undefined);           //=> false
    isInstance(Foo, null); //=> false

    class Foo {}
    class Bar {}

    isInstance(Foo, new Foo()); //=> true
    isInstance(Foo, new Bar()); //=> false

    class Baz {}
    Object.defineProperty(Baz, 'name', { value: 'Foo' });

    isInstance(Foo, new Baz()); //=> true
    isInstance(Foo, new Baz(), false); //=> false

    const isFoo = isInstance(Foo);

    isFoo(new Baz()); //=> true
    isFoo(new Baz(), false); //=> false

    Type parameters

    • T = unknown

    Parameters

    • declaration: Constructor<T>

      The constructor

    • target: unknown

      The object to check

    • Optional loose: boolean

      Whether use loose checks. Default is true.

    Returns target 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 { isInstance } from 'tily/is/instance';

    if (isInstance(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.

    isInstance<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.

    example
     isInstance(Foo, undefined);           //=> false
    isInstance(Foo, null); //=> false

    class Foo {}
    class Bar {}

    isInstance(Foo, new Foo()); //=> true
    isInstance(Foo, new Bar()); //=> false

    class Baz {}
    Object.defineProperty(Baz, 'name', { value: 'Foo' });

    isInstance(Foo, new Baz()); //=> true
    isInstance(Foo, new Baz(), false); //=> false

    const isFoo = isInstance(Foo);

    isFoo(new Baz()); //=> true
    isFoo(new Baz(), false); //=> false

    Type parameters

    • T = unknown

    Parameters

    Returns (target: unknown, loose?: boolean) => target is T

      • (target: unknown, loose?: boolean): target is T
      • Parameters

        • target: unknown
        • Optional loose: boolean

        Returns target is T

Generated using TypeDoc