From f837200a731c548a545654ebf7091554a8dc95b3 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sat, 22 Feb 2020 15:21:24 +0100 Subject: [PATCH] Fix isChildClass with correct is assertion (#546) * Fix isChildClass with correct is assertion * Remove stray any, allow never --- packages/util/src/is/childClass.spec.ts | 10 ++++++++++ packages/util/src/is/childClass.ts | 5 ++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/util/src/is/childClass.spec.ts b/packages/util/src/is/childClass.spec.ts index c62bcbb28..131f1143a 100644 --- a/packages/util/src/is/childClass.spec.ts +++ b/packages/util/src/is/childClass.spec.ts @@ -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'); + } + }); }); diff --git a/packages/util/src/is/childClass.ts b/packages/util/src/is/childClass.ts index b947c69c8..733a3b231 100644 --- a/packages/util/src/is/childClass.ts +++ b/packages/util/src/is/childClass.ts @@ -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 (Parent: Constructor, Child?: Constructor | null): Child is Constructor { +export default function isChildClass

(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; }