From d6c8ed8693b22be8026dd30f473e035f53daaed7 Mon Sep 17 00:00:00 2001 From: Romaric Pascal Date: Tue, 12 Nov 2024 18:36:54 +0000 Subject: [PATCH 1/3] Rename generic `T` to the more explicit `ComponentClass` --- packages/govuk-frontend/src/govuk/init.mjs | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/govuk-frontend/src/govuk/init.mjs b/packages/govuk-frontend/src/govuk/init.mjs index 380cdcd233..a8e486441d 100644 --- a/packages/govuk-frontend/src/govuk/init.mjs +++ b/packages/govuk-frontend/src/govuk/init.mjs @@ -76,11 +76,11 @@ function initAll(config) { * * Any component errors will be caught and logged to the console. * - * @template {CompatibleClass} T - * @param {T} Component - class of the component to create - * @param {T["defaults"]} [config] - Config supplied to component - * @param {OnErrorCallback | Element | Document | CreateAllOptions } [createAllOptions] - options for createAll including scope of the document to search within and callback function if error throw by component on init - * @returns {Array>} - array of instantiated components + * @template {CompatibleClass} ComponentClass + * @param {ComponentClass} Component - class of the component to create + * @param {ComponentClass["defaults"]} [config] - Config supplied to component + * @param {OnErrorCallback | Element | Document | CreateAllOptions } [createAllOptions] - options for createAll including scope of the document to search within and callback function if error throw by component on init + * @returns {Array>} - array of instantiated components */ function createAll(Component, config, createAllOptions) { let /** @type {Element | Document} */ $scope = document @@ -202,23 +202,23 @@ export { initAll, createAll } */ /** - * @template {CompatibleClass} T + * @template {CompatibleClass} ComponentClass * @typedef {object} ErrorContext * @property {Element} [element] - Element used for component module initialisation - * @property {T} [component] - Class of component - * @property {T["defaults"]} config - Config supplied to component + * @property {ComponentClass} [component] - Class of component + * @property {ComponentClass["defaults"]} config - Config supplied to component */ /** - * @template {CompatibleClass} T + * @template {CompatibleClass} ComponentClass * @callback OnErrorCallback * @param {unknown} error - Thrown error - * @param {ErrorContext} context - Object containing the element, component class and configuration + * @param {ErrorContext} context - Object containing the element, component class and configuration */ /** - * @template {CompatibleClass} T + * @template {CompatibleClass} ComponentClass * @typedef {object} CreateAllOptions * @property {Element | Document} [scope] - scope of the document to search within - * @property {OnErrorCallback} [onError] - callback function if error throw by component on init + * @property {OnErrorCallback} [onError] - callback function if error throw by component on init */ From fef3a86ff73cb1c41e973742b6704598cf6c7d8a Mon Sep 17 00:00:00 2001 From: Romaric Pascal Date: Tue, 12 Nov 2024 18:42:18 +0000 Subject: [PATCH 2/3] Refactor access to component's configuration in its own type --- packages/govuk-frontend/src/govuk/init.mjs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/govuk-frontend/src/govuk/init.mjs b/packages/govuk-frontend/src/govuk/init.mjs index a8e486441d..618a1b4311 100644 --- a/packages/govuk-frontend/src/govuk/init.mjs +++ b/packages/govuk-frontend/src/govuk/init.mjs @@ -78,7 +78,7 @@ function initAll(config) { * * @template {CompatibleClass} ComponentClass * @param {ComponentClass} Component - class of the component to create - * @param {ComponentClass["defaults"]} [config] - Config supplied to component + * @param {ComponentConfig} [config] - Config supplied to component * @param {OnErrorCallback | Element | Document | CreateAllOptions } [createAllOptions] - options for createAll including scope of the document to search within and callback function if error throw by component on init * @returns {Array>} - array of instantiated components */ @@ -201,12 +201,17 @@ export { initAll, createAll } * @typedef {keyof Config} ConfigKey */ +/** + * @template {CompatibleClass} ComponentClass + * @typedef {ComponentClass["defaults"]} ComponentConfig + */ + /** * @template {CompatibleClass} ComponentClass * @typedef {object} ErrorContext * @property {Element} [element] - Element used for component module initialisation * @property {ComponentClass} [component] - Class of component - * @property {ComponentClass["defaults"]} config - Config supplied to component + * @property {ComponentConfig} config - Config supplied to component */ /** From 3c4cb8cb32984450320aea0ca934dedf7bbe0e7a Mon Sep 17 00:00:00 2001 From: Romaric Pascal Date: Tue, 12 Nov 2024 18:42:48 +0000 Subject: [PATCH 3/3] Use ConstructorParameters to get the type of the components config --- packages/govuk-frontend/src/govuk/init.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/govuk-frontend/src/govuk/init.mjs b/packages/govuk-frontend/src/govuk/init.mjs index 618a1b4311..e8ab325bbe 100644 --- a/packages/govuk-frontend/src/govuk/init.mjs +++ b/packages/govuk-frontend/src/govuk/init.mjs @@ -162,7 +162,7 @@ export { initAll, createAll } **/ /** - * @typedef {{new (...args: any[]): any, defaults?: object, moduleName: string}} CompatibleClass + * @typedef {{new (...args: any[]): any, moduleName: string}} CompatibleClass */ /* eslint-enable jsdoc/valid-types */ @@ -203,7 +203,7 @@ export { initAll, createAll } /** * @template {CompatibleClass} ComponentClass - * @typedef {ComponentClass["defaults"]} ComponentConfig + * @typedef {ConstructorParameters[1]} ComponentConfig */ /**