Files
common/packages/util/src/is/childClass.ts
T
2021-01-03 11:20:22 +01:00

28 lines
923 B
TypeScript

// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Constructor } from '../types';
/**
* @name isChildClass
* @summary Tests if the child extends the parent Class
* @description
* Checks to see if the child Class extends the parent Class
* @example
* <BR>
*
* ```javascript
* import { isChildClass } from '@polkadot/util';
*
* console.log('isChildClass', isChildClass(BN, BN); // => true
* console.log('isChildClass', isChildClass(BN, Uint8Array); // => false
* ```
*/
export function isChildClass <P extends Constructor> (Parent: P, Child?: Constructor | null | unknown): Child is P {
// https://stackoverflow.com/questions/30993434/check-if-a-constructor-inherits-another-in-es6/30993664
return Child
// eslint-disable-next-line no-prototype-builtins
? Parent === Child || Parent.isPrototypeOf(Child as Constructor)
: false;
}