Fix isChildClass with correct is assertion (#546)
* Fix isChildClass with correct is assertion * Remove stray any, allow never
This commit is contained in:
@@ -24,4 +24,14 @@ describe('isChildClass', (): void => {
|
||||
isChildClass(BN)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('does TS magic, determining type', (): void => {
|
||||
const T = (class extends BN {}) as unknown;
|
||||
|
||||
if (isChildClass(BN, T)) {
|
||||
expect(new T(12345).toNumber()).toEqual(12345);
|
||||
} else {
|
||||
throw new Error('Not a child class');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -19,11 +19,10 @@ import { Constructor } from '../types';
|
||||
* console.log('isChildClass', isChildClass(BN, Uint8Array); // => false
|
||||
* ```
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export default function isChildClass <T = any> (Parent: Constructor<T>, Child?: Constructor | null): Child is Constructor<T> {
|
||||
export default function isChildClass <P extends Constructor> (Parent: P, Child?: Constructor | null | unknown | never): 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)
|
||||
? Parent === Child || Parent.isPrototypeOf(Child as Constructor)
|
||||
: false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user