jty
    Preparing search index...

    Function isInstance

    • Checks if a provided value is an instance of the provided class (considers inheritance)

      Uses the instanceof operator under the hood but does not throw for some cases where JavaScript chokes (e.g. 2 instanceof NaN throws, but isInstance(2, NaN) returns false).

      Type Parameters

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

      Parameters

      • x: unknown

        possibly an instance of a class

      • classConstructor: T

        a class constructor (usually starts with big letter!)

      Returns x is InstanceType<T>

      isInstance({}, Object) => true
      isInstance(Promise.resolve(), Promise) => true
      isInstance(/hello/i, RegExp) => true
      isInstance('plain str', String) => false
      isInstance(new String('str obj'), String) => true
      isInstance(22, Number) => false
      isInstance(new Number(33), Number) => true
      isInstance(2, NaN) => false // Note that `2 instanceof NaN` throws

      if classConstructor is not a function.