Skip to content

Commit

Permalink
fix: correct invalid TS types for router-dependant hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Oct 21, 2022
1 parent 1f0f420 commit 499ab59
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/next/client/components/hooks-client-context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createContext } from 'react'

export const SearchParamsContext = createContext<URLSearchParams | null>(null)
export const PathnameContext = createContext<string>(null as any)
export const PathnameContext = createContext<string | null>(null)
export const ParamsContext = createContext(null as any)
export const LayoutSegmentsContext = createContext(null as any)

Expand Down
8 changes: 6 additions & 2 deletions packages/next/client/components/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ export function useSearchParams() {
/**
* Get the router methods. For example router.push('/dashboard')
*/
export function useRouter(): import('../../shared/lib/app-router-context').AppRouterInstance {
export function useRouter():
| import('../../shared/lib/app-router-context').AppRouterInstance
| null {
// TODO-APP: consider throwing an error or adapting this to support pages router
return useContext(AppRouterContext)
}

Expand All @@ -102,7 +105,8 @@ export function useRouter(): import('../../shared/lib/app-router-context').AppRo
/**
* Get the current pathname. For example usePathname() on /dashboard?foo=bar would return "/dashboard"
*/
export function usePathname(): string {
export function usePathname(): string | null {
// TODO-APP: consider throwing an error or adapting this to support pages router
return useContext(PathnameContext)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/next/client/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default singletonRouter as SingletonRouter
// Reexport the withRoute HOC
export { default as withRouter } from './with-router'

export function useRouter(): NextRouter {
export function useRouter(): NextRouter | null {
return React.useContext(RouterContext)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/next/shared/lib/app-router-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export interface AppRouterInstance {
prefetch(href: string): void
}

export const AppRouterContext = React.createContext<AppRouterInstance>(
null as any
export const AppRouterContext = React.createContext<AppRouterInstance | null>(
null
)
export const LayoutRouterContext = React.createContext<{
childNodes: CacheNode['parallelRoutes']
Expand Down
2 changes: 1 addition & 1 deletion packages/next/shared/lib/router-context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import type { NextRouter } from './router/router'

export const RouterContext = React.createContext<NextRouter>(null as any)
export const RouterContext = React.createContext<NextRouter | null>(null)

if (process.env.NODE_ENV !== 'production') {
RouterContext.displayName = 'RouterContext'
Expand Down

0 comments on commit 499ab59

Please sign in to comment.