From 7b8eaf9045dcae097efe3eb30e6358e2c31f78b7 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Sun, 12 Dec 2021 22:38:55 -0500 Subject: [PATCH] Fix typing errors with `app.modal.show` Unfortunately TypeScript doesn't support higher-kinded types, so we can't write this in a type-safe way. --- .../core/js/src/common/states/ModalManagerState.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/framework/core/js/src/common/states/ModalManagerState.ts b/framework/core/js/src/common/states/ModalManagerState.ts index 401d81b21..44c2be05d 100644 --- a/framework/core/js/src/common/states/ModalManagerState.ts +++ b/framework/core/js/src/common/states/ModalManagerState.ts @@ -1,5 +1,15 @@ +import Component from '../Component'; import Modal from '../components/Modal'; +/** + * Ideally, `show` would take a higher-kinded generic, ala: + * `show(componentClass: C, attrs: Attrs): void` + * Unfortunately, TypeScript does not support this: + * https://github.com/Microsoft/TypeScript/issues/1213 + * Therefore, we have to use this ugly, messy workaround. + */ +type UnsafeModalClass = ComponentClass & {isDismissible: boolean, component: typeof Component.component}; + /** * Class used to manage modal state. * @@ -10,7 +20,7 @@ export default class ModalManagerState { * @internal */ modal: null | { - componentClass: typeof Modal; + componentClass: UnsafeModalClass; attrs?: Record; } = null; @@ -28,7 +38,7 @@ export default class ModalManagerState { * // This "hack" is needed due to quirks with nested redraws in Mithril. * setTimeout(() => app.modal.show(MyCoolModal, { attr: 'value' }), 0); */ - show(componentClass: typeof Modal, attrs: Record = {}): void { + show(componentClass: UnsafeModalClass, attrs: Record = {}): void { if (!(componentClass.prototype instanceof Modal)) { // This is duplicated so that if the error is caught, an error message still shows up in the debug console. const invalidModalWarning = 'The ModalManager can only show Modals.';