Skip to content

Commit

Permalink
try to extend the base class type
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickpatrickpatrick committed Sep 12, 2024
1 parent 98ed672 commit a35cab2
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions packages/govuk-frontend/src/govuk/govuk-frontend-component.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import { InitError, SupportError } from './errors/index.mjs'
* @virtual
*/
export class GOVUKFrontendComponent {
/**
* @type {string|undefined}
*/
static moduleName
/**
* Constructs a new component, validating that GOV.UK Frontend is supported
*
Expand All @@ -24,9 +20,8 @@ export class GOVUKFrontendComponent {
this.checkSupport()
this.checkInitialised($module)

const moduleName = /** @type {typeof GOVUKFrontendComponent} */ (
this.constructor
).moduleName
const moduleName = /** @type {ChildClassConstructor} */ (this.constructor)
.moduleName

if (typeof moduleName === 'string') {
moduleName && $module?.setAttribute(`data-${moduleName}-init`, 'true')
Expand All @@ -43,9 +38,8 @@ export class GOVUKFrontendComponent {
* @throws {InitError} when component is already initialised
*/
checkInitialised($module) {
const moduleName = /** @type {typeof GOVUKFrontendComponent} */ (
this.constructor
).moduleName
const moduleName = /** @type {ChildClassConstructor} */ (this.constructor)
.moduleName

if ($module && moduleName && isInitialised($module, moduleName)) {
throw new InitError(moduleName)
Expand All @@ -64,3 +58,12 @@ export class GOVUKFrontendComponent {
}
}
}

/**
* @typedef ChildClass
* @property {string} [moduleName] - The module name that'll be looked for in the DOM when initialising the component
*/

/**
* @typedef {typeof GOVUKFrontendComponent & ChildClass} ChildClassConstructor
*/

0 comments on commit a35cab2

Please sign in to comment.