Skip to content

Commit

Permalink
Merge pull request #4207 from alphagov/govuk-frontend-typescript-defi…
Browse files Browse the repository at this point in the history
…nition

Add shorthand ambient module definition for 'govuk-frontend'
  • Loading branch information
romaricpascal authored Oct 11, 2024
2 parents 9484b8f + a830e84 commit 984f448
Show file tree
Hide file tree
Showing 12 changed files with 10 additions and 121 deletions.
9 changes: 9 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* As govuk-frontend provides no types, TypeScript will type its exports as `any`,
* but be unable to acknowledge fields inherited from parent classes
* leading to errors when trying to assign or use them.
*
* TypeScript's shorthand ambient modules seem to also make inherited fields typed as `any`.
* https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#shorthand-ambient-module-declarations
*/
declare module "govuk-frontend";
12 changes: 0 additions & 12 deletions src/javascripts/components/back-to-top.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@ import { Component } from 'govuk-frontend'
* Website back to top link
*/
class BackToTop extends Component {
/**
* Returns the root element of the component
*
* @returns {any} - the root element of component
*/
get $root() {
// Unfortunately, govuk-frontend does not provide type definitions
// so TypeScript does not know of `this._$root`
// @ts-expect-error
return this._$root
}

static moduleName = 'app-back-to-top'

/**
Expand Down
12 changes: 0 additions & 12 deletions src/javascripts/components/cookie-banner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ const cookieConfirmationRejectSelector = '.js-cookie-banner-confirmation-reject'
* Website cookie banner
*/
class CookieBanner extends Component {
/**
* Returns the root element of the component
*
* @returns {any} - the root element of component
*/
get $root() {
// Unfortunately, govuk-frontend does not provide type definitions
// so TypeScript does not know of `this._$root`
// @ts-expect-error
return this._$root
}

static moduleName = 'govuk-cookie-banner'
/**
* @param {Element} $module - HTML element
Expand Down
12 changes: 0 additions & 12 deletions src/javascripts/components/cookies-page.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ import { getConsentCookie, setConsentCookie } from './cookie-functions.mjs'
* Website cookies page
*/
class CookiesPage extends Component {
/**
* Returns the root element of the component
*
* @returns {any} - the root element of component
*/
get $root() {
// Unfortunately, govuk-frontend does not provide type definitions
// so TypeScript does not know of `this._$root`
// @ts-expect-error
return this._$root
}

static moduleName = 'app-cookies-page'
/**
* @param {Element} $module - HTML element
Expand Down
12 changes: 0 additions & 12 deletions src/javascripts/components/copy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@ import { Component } from 'govuk-frontend'
* Copy button for code examples
*/
class Copy extends Component {
/**
* Returns the root element of the component
*
* @returns {any} - the root element of component
*/
get $root() {
// Unfortunately, govuk-frontend does not provide type definitions
// so TypeScript does not know of `this._$root`
// @ts-expect-error
return this._$root
}

static moduleName = 'app-copy'

/**
Expand Down
12 changes: 0 additions & 12 deletions src/javascripts/components/embed-card.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ class EmbedCard extends Component {
}
}

/**
* Returns the root element of the component
*
* @returns {any} - the root element of component
*/
get $root() {
// Unfortunately, govuk-frontend does not provide type definitions
// so TypeScript does not know of `this._$root`
// @ts-expect-error
return this._$root
}

static moduleName = 'app-embed-card'

/**
Expand Down
12 changes: 0 additions & 12 deletions src/javascripts/components/example-frame.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@ import iFrameResize from 'iframe-resizer/js/iframeResizer.js'
* @augments Component<HTMLIFrameElement>
*/
class ExampleFrame extends Component {
/**
* Returns the root element of the component
*
* @returns {any} - the root element of component
*/
get $root() {
// Unfortunately, govuk-frontend does not provide type definitions
// so TypeScript does not know of `this._$root`
// @ts-expect-error
return this._$root
}

static moduleName = 'app-example-frame'
/**
* @param {Element} $module - HTML element
Expand Down
12 changes: 0 additions & 12 deletions src/javascripts/components/navigation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ const subNavJSClass = '.js-app-navigation__subnav'
* Website navigation
*/
class Navigation extends Component {
/**
* Returns the root element of the component
*
* @returns {any} - the root element of component
*/
get $root() {
// Unfortunately, govuk-frontend does not provide type definitions
// so TypeScript does not know of `this._$root`
// @ts-expect-error
return this._$root
}

/**
* Name for the component used when initialising using data-module attributes.
*/
Expand Down
12 changes: 0 additions & 12 deletions src/javascripts/components/scroll-container.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ const scrollContainerResizeObserver = new window.ResizeObserver((entries) => {
*
*/
class ScrollContainer extends Component {
/**
* Returns the root element of the component
*
* @returns {any} - the root element of component
*/
get $root() {
// Unfortunately, govuk-frontend does not provide type definitions
// so TypeScript does not know of `this._$root`
// @ts-expect-error
return this._$root
}

static moduleName = 'app-scroll-container'

/**
Expand Down
12 changes: 0 additions & 12 deletions src/javascripts/components/search.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ const DEBOUNCE_TIME_TO_WAIT = () => {
* Website search
*/
class Search extends Component {
/**
* Returns the root element of the component
*
* @returns {any} - the root element of component
*/
get $root() {
// Unfortunately, govuk-frontend does not provide type definitions
// so TypeScript does not know of `this._$root`
// @ts-expect-error
return this._$root
}

static moduleName = 'app-search'
/**
* @param {Element} $module - HTML element
Expand Down
12 changes: 0 additions & 12 deletions src/javascripts/components/tabs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@ import { Component } from 'govuk-frontend'
* - panels - the content that is shown/hidden/switched; same across all breakpoints
*/
class AppTabs extends Component {
/**
* Returns the root element of the component
*
* @returns {any} - the root element of component
*/
get $root() {
// Unfortunately, govuk-frontend does not provide type definitions
// so TypeScript does not know of `this._$root`
// @ts-expect-error
return this._$root
}

static moduleName = 'app-tabs'

/**
Expand Down
2 changes: 1 addition & 1 deletion src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "../tsconfig.base.json",
"include": ["**/*.js", "**/*.mjs"],
"include": ["**/*.js", "**/*.mjs", "**/*.ts"],
"compilerOptions": {
"lib": ["ESNext", "DOM"],
"target": "ES2015"
Expand Down

0 comments on commit 984f448

Please sign in to comment.