Fix isChildClass with correct is assertion (#546)

* Fix isChildClass with correct is assertion

* Remove stray any, allow never
This commit is contained in:
Jaco Greeff
2020-02-22 15:21:24 +01:00
committed by GitHub
parent ea7dd553a2
commit f837200a73
2 changed files with 12 additions and 3 deletions
+10
View File
@@ -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');
}
});
});
+2 -3
View File
@@ -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;
}