-
Notifications
You must be signed in to change notification settings - Fork 326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update createAll
types so they don't rely on component's defaults
#5496
Merged
+18
−13
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
govuk-design-system-ci
temporarily deployed
to
govuk-frontend-pr-5496
November 12, 2024 17:31
Inactive
📋 StatsFile sizes
Modules
View stats and visualisations on the review app Action run for 3c4cb8c |
Other changes to npm packagediff --git a/packages/govuk-frontend/dist/govuk/all.bundle.js b/packages/govuk-frontend/dist/govuk/all.bundle.js
index 956e54478..73172514b 100644
--- a/packages/govuk-frontend/dist/govuk/all.bundle.js
+++ b/packages/govuk-frontend/dist/govuk/all.bundle.js
@@ -2447,11 +2447,11 @@
*
* 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<T> | Element | Document | CreateAllOptions<T> } [createAllOptions] - options for createAll including scope of the document to search within and callback function if error throw by component on init
- * @returns {Array<InstanceType<T>>} - array of instantiated components
+ * @template {CompatibleClass} ComponentClass
+ * @param {ComponentClass} Component - class of the component to create
+ * @param {ComponentConfig<ComponentClass>} [config] - Config supplied to component
+ * @param {OnErrorCallback<ComponentClass> | Element | Document | CreateAllOptions<ComponentClass> } [createAllOptions] - options for createAll including scope of the document to search within and callback function if error throw by component on init
+ * @returns {Array<InstanceType<ComponentClass>>} - array of instantiated components
*/
function createAll(Component, config, createAllOptions) {
let $scope = document;
@@ -2498,7 +2498,7 @@
}).filter(Boolean);
}
/**
- * @typedef {{new (...args: any[]): any, defaults?: object, moduleName: string}} CompatibleClass
+ * @typedef {{new (...args: any[]): any, moduleName: string}} CompatibleClass
*/
/**
* Config for all components via `initAll()`
@@ -2532,23 +2532,27 @@
* @typedef {keyof Config} ConfigKey
*/
/**
- * @template {CompatibleClass} T
+ * @template {CompatibleClass} ComponentClass
+ * @typedef {ConstructorParameters<ComponentClass>[1]} ComponentConfig
+ */
+ /**
+ * @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 {ComponentConfig<ComponentClass>} config - Config supplied to component
*/
/**
- * @template {CompatibleClass} T
+ * @template {CompatibleClass} ComponentClass
* @callback OnErrorCallback
* @param {unknown} error - Thrown error
- * @param {ErrorContext<T>} context - Object containing the element, component class and configuration
+ * @param {ErrorContext<ComponentClass>} 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<T>} [onError] - callback function if error throw by component on init
+ * @property {OnErrorCallback<ComponentClass>} [onError] - callback function if error throw by component on init
*/
exports.Accordion = Accordion;
diff --git a/packages/govuk-frontend/dist/govuk/all.bundle.mjs b/packages/govuk-frontend/dist/govuk/all.bundle.mjs
index b56f12f83..188b6100c 100644
--- a/packages/govuk-frontend/dist/govuk/all.bundle.mjs
+++ b/packages/govuk-frontend/dist/govuk/all.bundle.mjs
@@ -2441,11 +2441,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<T> | Element | Document | CreateAllOptions<T> } [createAllOptions] - options for createAll including scope of the document to search within and callback function if error throw by component on init
- * @returns {Array<InstanceType<T>>} - array of instantiated components
+ * @template {CompatibleClass} ComponentClass
+ * @param {ComponentClass} Component - class of the component to create
+ * @param {ComponentConfig<ComponentClass>} [config] - Config supplied to component
+ * @param {OnErrorCallback<ComponentClass> | Element | Document | CreateAllOptions<ComponentClass> } [createAllOptions] - options for createAll including scope of the document to search within and callback function if error throw by component on init
+ * @returns {Array<InstanceType<ComponentClass>>} - array of instantiated components
*/
function createAll(Component, config, createAllOptions) {
let $scope = document;
@@ -2492,7 +2492,7 @@ function createAll(Component, config, createAllOptions) {
}).filter(Boolean);
}
/**
- * @typedef {{new (...args: any[]): any, defaults?: object, moduleName: string}} CompatibleClass
+ * @typedef {{new (...args: any[]): any, moduleName: string}} CompatibleClass
*/
/**
* Config for all components via `initAll()`
@@ -2526,23 +2526,27 @@ function createAll(Component, config, createAllOptions) {
* @typedef {keyof Config} ConfigKey
*/
/**
- * @template {CompatibleClass} T
+ * @template {CompatibleClass} ComponentClass
+ * @typedef {ConstructorParameters<ComponentClass>[1]} ComponentConfig
+ */
+/**
+ * @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 {ComponentConfig<ComponentClass>} config - Config supplied to component
*/
/**
- * @template {CompatibleClass} T
+ * @template {CompatibleClass} ComponentClass
* @callback OnErrorCallback
* @param {unknown} error - Thrown error
- * @param {ErrorContext<T>} context - Object containing the element, component class and configuration
+ * @param {ErrorContext<ComponentClass>} 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<T>} [onError] - callback function if error throw by component on init
+ * @property {OnErrorCallback<ComponentClass>} [onError] - callback function if error throw by component on init
*/
export { Accordion, Button, CharacterCount, Checkboxes, GOVUKFrontendComponent as Component, ErrorSummary, ExitThisPage, Header, NotificationBanner, PasswordInput, Radios, ServiceNavigation, SkipLink, Tabs, createAll, initAll, isSupported, version };
diff --git a/packages/govuk-frontend/dist/govuk/init.mjs b/packages/govuk-frontend/dist/govuk/init.mjs
index 9b087239b..7bc77bdab 100644
--- a/packages/govuk-frontend/dist/govuk/init.mjs
+++ b/packages/govuk-frontend/dist/govuk/init.mjs
@@ -54,11 +54,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<T> | Element | Document | CreateAllOptions<T> } [createAllOptions] - options for createAll including scope of the document to search within and callback function if error throw by component on init
- * @returns {Array<InstanceType<T>>} - array of instantiated components
+ * @template {CompatibleClass} ComponentClass
+ * @param {ComponentClass} Component - class of the component to create
+ * @param {ComponentConfig<ComponentClass>} [config] - Config supplied to component
+ * @param {OnErrorCallback<ComponentClass> | Element | Document | CreateAllOptions<ComponentClass> } [createAllOptions] - options for createAll including scope of the document to search within and callback function if error throw by component on init
+ * @returns {Array<InstanceType<ComponentClass>>} - array of instantiated components
*/
function createAll(Component, config, createAllOptions) {
let $scope = document;
@@ -105,7 +105,7 @@ function createAll(Component, config, createAllOptions) {
}).filter(Boolean);
}
/**
- * @typedef {{new (...args: any[]): any, defaults?: object, moduleName: string}} CompatibleClass
+ * @typedef {{new (...args: any[]): any, moduleName: string}} CompatibleClass
*/
/**
* Config for all components via `initAll()`
@@ -139,23 +139,27 @@ function createAll(Component, config, createAllOptions) {
* @typedef {keyof Config} ConfigKey
*/
/**
- * @template {CompatibleClass} T
+ * @template {CompatibleClass} ComponentClass
+ * @typedef {ConstructorParameters<ComponentClass>[1]} ComponentConfig
+ */
+/**
+ * @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 {ComponentConfig<ComponentClass>} config - Config supplied to component
*/
/**
- * @template {CompatibleClass} T
+ * @template {CompatibleClass} ComponentClass
* @callback OnErrorCallback
* @param {unknown} error - Thrown error
- * @param {ErrorContext<T>} context - Object containing the element, component class and configuration
+ * @param {ErrorContext<ComponentClass>} 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<T>} [onError] - callback function if error throw by component on init
+ * @property {OnErrorCallback<ComponentClass>} [onError] - callback function if error throw by component on init
*/
export { createAll, initAll };
Action run for 3c4cb8c |
romaricpascal
force-pushed
the
createall-type-no-defaults
branch
from
November 12, 2024 18:42
868cf6a
to
c36179e
Compare
govuk-design-system-ci
temporarily deployed
to
govuk-frontend-pr-5496
November 12, 2024 18:43
Inactive
1 task
romaricpascal
force-pushed
the
createall-type-no-defaults
branch
from
November 12, 2024 18:53
c36179e
to
3c4cb8c
Compare
govuk-design-system-ci
temporarily deployed
to
govuk-frontend-pr-5496
November 12, 2024 18:53
Inactive
domoscargin
approved these changes
Nov 14, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks grand to me!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Use TypeScript's
ConstructorParameters
to pick the type for the component's configuration from the arguments of its constructor, rather than itsdefaults
property.Thoughts
This is closer to the actual behaviour of the function, which passes the config as the second argument of the constructor.
Testing in the
init.jsdom.test.mjs
(as it imports bothcreateAll
and components):Completions work OK for components that accept a configuration:
For components that do not accept configuration, config is typed as
any
Future work
Because
createAll
no longer relies ondefaults
we can set thedefaults
static property of the components as@protected
to clarify the scope of our public API further. Given configuration passed to the constructor gets merged with those defaults, there's little need for them to be accessed outside of the component's class (or its descendents).