diff --git a/packages/docusaurus-module-type-aliases/src/index.d.ts b/packages/docusaurus-module-type-aliases/src/index.d.ts index 6c56613196af..7292518fc3b9 100644 --- a/packages/docusaurus-module-type-aliases/src/index.d.ts +++ b/packages/docusaurus-module-type-aliases/src/index.d.ts @@ -6,8 +6,9 @@ */ declare module '@generated/client-modules' { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const clientModules: readonly any[]; + import type {ClientModule} from '@docusaurus/types'; + + const clientModules: readonly (ClientModule & {default: ClientModule})[]; export default clientModules; } diff --git a/packages/docusaurus-types/src/index.d.ts b/packages/docusaurus-types/src/index.d.ts index cd98b9b9eb11..5e9e043ef0ec 100644 --- a/packages/docusaurus-types/src/index.d.ts +++ b/packages/docusaurus-types/src/index.d.ts @@ -11,6 +11,7 @@ import type {CommanderStatic} from 'commander'; import type {ParsedUrlQueryInput} from 'querystring'; import type Joi from 'joi'; import type {Overwrite, DeepPartial} from 'utility-types'; +import type {Location} from 'history'; export type ReportingSeverity = 'ignore' | 'log' | 'warn' | 'error' | 'throw'; @@ -448,3 +449,11 @@ export interface TOCItem { } export type RouteChunksTree = {[x: string | number]: string | RouteChunksTree}; + +export type ClientModule = { + onRouteUpdate?: (args: { + previousLocation: Location | null; + location: Location; + }) => void; + onRouteUpdateDelayed?: (args: {location: Location}) => void; +}; diff --git a/packages/docusaurus/src/client/PendingNavigation.tsx b/packages/docusaurus/src/client/PendingNavigation.tsx index 7e89df324c8e..050dd1a9000a 100644 --- a/packages/docusaurus/src/client/PendingNavigation.tsx +++ b/packages/docusaurus/src/client/PendingNavigation.tsx @@ -69,12 +69,7 @@ class PendingNavigation extends React.Component { }); // Route has loaded, we can reset previousLocation. this.previousLocation = null; - this.setState( - { - nextRouteHasLoaded: true, - }, - this.stopProgressBar, - ); + this.setState({nextRouteHasLoaded: true}, this.stopProgressBar); const {hash} = nextLocation; if (!hash) { window.scrollTo(0, 0); diff --git a/packages/docusaurus/src/client/client-lifecycles-dispatcher.ts b/packages/docusaurus/src/client/client-lifecycles-dispatcher.ts index 98a4a206401d..57982c394313 100644 --- a/packages/docusaurus/src/client/client-lifecycles-dispatcher.ts +++ b/packages/docusaurus/src/client/client-lifecycles-dispatcher.ts @@ -6,32 +6,28 @@ */ import clientModules from '@generated/client-modules'; +import type {ClientModule} from '@docusaurus/types'; -interface Dispatchers { - onRouteUpdate: (...args: unknown[]) => void; - onRouteUpdateDelayed: (...args: unknown[]) => void; -} - -function dispatchLifecycleAction( - lifecycleAction: keyof Dispatchers, - ...args: unknown[] +function dispatchLifecycleAction( + lifecycleAction: K, + args: Parameters>, ) { clientModules.forEach((clientModule) => { - const lifecycleFunction = - clientModule?.default?.[lifecycleAction] ?? clientModule[lifecycleAction]; + const lifecycleFunction = (clientModule?.default?.[lifecycleAction] ?? + clientModule[lifecycleAction]) as + | ((...a: Parameters>) => void) + | undefined; - if (lifecycleFunction) { - lifecycleFunction(...args); - } + lifecycleFunction?.(...args); }); } -const clientLifecyclesDispatchers: Dispatchers = { +const clientLifecyclesDispatchers: Required = { onRouteUpdate(...args) { - dispatchLifecycleAction('onRouteUpdate', ...args); + dispatchLifecycleAction('onRouteUpdate', args); }, onRouteUpdateDelayed(...args) { - dispatchLifecycleAction('onRouteUpdateDelayed', ...args); + dispatchLifecycleAction('onRouteUpdateDelayed', args); }, };