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

feat(react-router): add NotFoundErrorData interface for improved type safety #3112

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion packages/react-router/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export {
CatchNotFound,
DefaultGlobalNotFound,
} from './not-found'
export type { NotFoundError } from './not-found'
export type { NotFoundError, NotFoundErrorData } from './not-found'

export type { Manifest, RouterManagedTag } from './manifest'

Expand Down
16 changes: 15 additions & 1 deletion packages/react-router/src/not-found.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type { ErrorInfo } from 'react'
import type { RegisteredRouter } from './router'
import type { RouteIds } from './routeInfo'

export interface NotFoundErrorData {}

export type NotFoundError = {
/**
@deprecated
Expand All @@ -16,7 +18,19 @@ export type NotFoundError = {
Do not use this. It's used internally to indicate a path matching error
*/
_global?: boolean
data?: any
/**
This property can be typed globally using the `NotFoundErrorData` interface
```ts
declare module '@tanstack/react-router' {
interface NotFoundErrorData {
data: {
// your properties here
}
}
}
```
*/
data?: NotFoundErrorData extends { data: infer TData } ? TData : any
throw?: boolean
routeId?: RouteIds<RegisteredRouter['routeTree']>
headers?: HeadersInit
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,7 @@ export type ErrorComponentProps = {
}
export type NotFoundRouteProps = {
// TODO: Make sure this is `| null | undefined` (this is for global not-founds)
data: unknown
data: NotFoundError['data'] | null | undefined
}
//

Expand Down