jty
    Preparing search index...

    Function isOwnInstance

    • Checks if a value was directly constructed by the provided class (ignores inheritance)

      Unlike isInstance which uses instanceof and walks the prototype chain, isOwnInstance only returns true when the value's immediate prototype matches classConstructor.prototype. This means subclass instances return false when checked against a parent class.

      Type Parameters

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

      Parameters

      • x: unknown

        possibly a direct instance of a class

      • classConstructor: T

        a class constructor (usually starts with big letter!)

      Returns x is InstanceType<T>

      class Animal {}
      class Dog extends Animal {}
      const dog = new Dog()

      isOwnInstance(dog, Dog) => true
      isOwnInstance(dog, Animal) => false // dog's direct prototype is Dog.prototype
      isInstance(dog, Animal) => true // for comparison: instanceof walks the chain

      isOwnInstance({}, Object) => true
      isOwnInstance([], Object) => false // direct prototype is Array.prototype
      isOwnInstance([], Array) => true
      isOwnInstance(/hello/i, RegExp) => true
      isOwnInstance('plain str', String) => false
      isOwnInstance(22, Number) => false
      isOwnInstance(Object.create(null), Object) => false

      if classConstructor is not a function.