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 8 commits into
base: main
Choose a base branch
from

Conversation

Sheraff
Copy link

@Sheraff Sheraff commented Jan 5, 2025

Here's a demo of this PR, throwing type-safe notFound errors: https://stackblitz.com/edit/tanstack-router-fyrpy35d?file=src%2Froutes%2Findex.tsx


Currently the notFound() function accepts a params object with the shape of NotFoundError that is then passed as props to the notFoundComponent.

Arbitrary data can be send through this mechanism with the data?: any key of NotFoundError, but this any prevents us from creating type-safe "not found" errors.

This PR proposes that users should be able to define their own data shape through the global augmentation of a new interface NotFoundErrorData. For example:

declare module '@tanstack/react-router' {
  interface NotFoundErrorData {
    data: {
      a: string
      b: string
    }
  }
}
declare module '@tanstack/react-router' {
  interface NotFoundErrorData {
    data: 'foo' | 'bar'
  }
}
declare module '@tanstack/react-router' {
  interface NotFoundErrorData {
    data:
      | { type: 'foo', a: number }
      | { type: 'bar', b: string }
  }
}

This PR uses this new interface in a way that will preserve data?: any as the default if it is not globally augmented:

data?: NotFoundErrorData extends { data: infer TData } ? TData : any

Closes #3113

@Sheraff Sheraff changed the title feat'react-router): add NotFoundErrorData interface for improved type safety feat(react-router): add NotFoundErrorData interface for improved type safety Jan 5, 2025
@Sheraff Sheraff marked this pull request as ready for review January 5, 2025 09:42
Copy link

nx-cloud bot commented Jan 5, 2025

View your CI Pipeline Execution ↗ for commit 1eafefc.

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded 6m 9s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 1m 1s View ↗

☁️ Nx Cloud last updated this comment at 2025-01-05 10:49:01 UTC

Copy link

pkg-pr-new bot commented Jan 5, 2025

Open in Stackblitz

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/@tanstack/arktype-adapter@3112

@tanstack/create-router

npm i https://pkg.pr.new/@tanstack/create-router@3112

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/@tanstack/eslint-plugin-router@3112

@tanstack/history

npm i https://pkg.pr.new/@tanstack/history@3112

@tanstack/create-start

npm i https://pkg.pr.new/@tanstack/create-start@3112

@tanstack/react-cross-context

npm i https://pkg.pr.new/@tanstack/react-cross-context@3112

@tanstack/react-router

npm i https://pkg.pr.new/@tanstack/react-router@3112

@tanstack/react-router-with-query

npm i https://pkg.pr.new/@tanstack/react-router-with-query@3112

@tanstack/router-cli

npm i https://pkg.pr.new/@tanstack/router-cli@3112

@tanstack/router-devtools

npm i https://pkg.pr.new/@tanstack/router-devtools@3112

@tanstack/router-generator

npm i https://pkg.pr.new/@tanstack/router-generator@3112

@tanstack/router-plugin

npm i https://pkg.pr.new/@tanstack/router-plugin@3112

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/@tanstack/router-vite-plugin@3112

@tanstack/start

npm i https://pkg.pr.new/@tanstack/start@3112

@tanstack/start-vite-plugin

npm i https://pkg.pr.new/@tanstack/start-vite-plugin@3112

@tanstack/valibot-adapter

npm i https://pkg.pr.new/@tanstack/valibot-adapter@3112

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/@tanstack/virtual-file-routes@3112

@tanstack/zod-adapter

npm i https://pkg.pr.new/@tanstack/zod-adapter@3112

commit: 1eafefc

@EskiMojo14
Copy link
Contributor

might be worth extending the existing Register interface already used for registering the router type, rather than adding a new interface for the same purpose

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants