From 4255771f5c68921ebfa9478f7b1236911c8a8841 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 19 Jul 2020 17:58:02 -0400 Subject: [PATCH] Have isFormT match on derived form types by walking the prototype chain. --- src/core/Form.test.ts | 19 +++++++++++++++++++ src/core/Form.ts | 6 +++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/core/Form.test.ts b/src/core/Form.test.ts index 4178d19..5b00536 100644 --- a/src/core/Form.test.ts +++ b/src/core/Form.test.ts @@ -92,3 +92,22 @@ test('form types', () => { expect(isFormT(new FormA(), FormB)).toBe(false); expect(isFormT(new FormB(), FormB)).toBe(true); }); + +test('derived form types', () => { + class Base extends Form { + readonly name = 'Base'; + readonly lines = {}; + }; + class Derived extends Base {}; + class SecondDerived extends Derived {}; + + expect(isFormT(new Base(), Base)).toBe(true); + expect(isFormT(new Derived(), Derived)).toBe(true); + + expect(isFormT(new Derived(), Base)).toBe(true); + expect(isFormT(new Base(), Derived)).toBe(false); + + expect(isFormT(new SecondDerived(), SecondDerived)).toBe(true); + expect(isFormT(new SecondDerived(), Derived)).toBe(true); + expect(isFormT(new SecondDerived(), Base)).toBe(true); +}); diff --git a/src/core/Form.ts b/src/core/Form.ts index dc7e70a..6aaece8 100644 --- a/src/core/Form.ts +++ b/src/core/Form.ts @@ -66,5 +66,9 @@ export type FormClass = new (...args: any[]) => T; export function isFormT(form: Form, formClass: FormClass): form is T { - return form.constructor === formClass; + for (let proto = form; proto !== null; proto = Object.getPrototypeOf(proto)) { + if (proto.constructor === formClass) + return true; + } + return false; } -- 2.43.5