Skip to content

Function: isSubclassOf() ​

isSubclassOf(value, base): boolean

Tell whether a value is a class (or a constructor function) extending a base class, directly or not. The base class is not its own subclass, and an instance of the subclass is not a subclass. It gives the same result as DTO.isValid(value, DTO.Class(base)), without building a validation schema.

Parameters ​

value ​

unknown

The value to test, of any type; a value which is not a function, or a function without prototype (an arrow function), gives false.

base ​

Function

The base class.

Returns ​

boolean

true when value extends base, false otherwise.

Throws ​

When base is not callable or its prototype is not an object (instanceof rejects it), and value is a function with a prototype.

Example ​

typescript
import {isSubclassOf} from 'lakutata/utils'

class Animal {}
class Dog extends Animal {}

console.log(isSubclassOf(Dog, Animal), isSubclassOf(Animal, Animal), isSubclassOf(new Dog(), Animal))
//true false false

Released under the MIT License.