Skip to content
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

Strongly type Router.events.on and Router.events.off #26456

Merged
merged 2 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class Container extends React.Component<{
}
}

export const emitter: MittEmitter = mitt()
export const emitter: MittEmitter<string> = mitt()
let CachedComponent: React.ComponentType

export default async (opts: { webpackHMR?: any } = {}) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/next/client/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ const routerEvents = [
'routeChangeError',
'hashChangeStart',
'hashChangeComplete',
]
] as const
export type RouterEvent = typeof routerEvents[number]

const coreMethodFields = [
'push',
'replace',
Expand Down Expand Up @@ -89,7 +91,7 @@ coreMethodFields.forEach((field: string) => {
}
})

routerEvents.forEach((event: string) => {
routerEvents.forEach((event) => {
singletonRouter.ready(() => {
Router.events.on(event, (...args) => {
const eventField = `on${event.charAt(0).toUpperCase()}${event.substring(
Expand Down
10 changes: 5 additions & 5 deletions packages/next/next-server/lib/mitt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI

type Handler = (...evts: any[]) => void

export type MittEmitter = {
on(type: string, handler: Handler): void
off(type: string, handler: Handler): void
emit(type: string, ...evts: any[]): void
export type MittEmitter<T> = {
on(type: T, handler: Handler): void
off(type: T, handler: Handler): void
emit(type: T, ...evts: any[]): void
}

export default function mitt(): MittEmitter {
export default function mitt(): MittEmitter<string> {
const all: { [s: string]: Handler[] } = Object.create(null)

return {
Expand Down
5 changes: 3 additions & 2 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
isAssetError,
markAssetError,
} from '../../../client/route-loader'
import { RouterEvent } from '../../../client/router'
import { DomainLocales } from '../../server/config'
import { denormalizePagePath } from '../../server/denormalize-page-path'
import { normalizeLocalePath } from '../i18n/normalize-locale-path'
Expand Down Expand Up @@ -522,7 +523,7 @@ export default class Router implements BaseRouter {
clc: ComponentLoadCancel
pageLoader: any
_bps: BeforePopStateCallback | undefined
events: MittEmitter
events: MittEmitter<RouterEvent>
_wrapApp: (App: AppComponent) => any
isSsr: boolean
isFallback: boolean
Expand All @@ -538,7 +539,7 @@ export default class Router implements BaseRouter {

private _idx: number = 0

static events: MittEmitter = mitt()
static events: MittEmitter<RouterEvent> = mitt()

constructor(
pathname: string,
Expand Down
4 changes: 4 additions & 0 deletions test/integration/typescript/components/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import Router, { withRouter } from 'next/router'

export default withRouter(({ router }) => {
React.useEffect(() => {
Router.events.on('routeChangeComplete', () => {})
//@ts-expect-error
Router.events.on('event', () => {})
Router.prefetch('/page')
Router.push
Router.back
Router.reload

router.events.on('routeChangeComplete', () => {})
//@ts-expect-error
router.events.on('event', () => {})
router.prefetch('/page')
router.push
Expand Down