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

type inference not working for params using type validation with zod adapter #3100

Open
allan-cannon opened this issue Jan 2, 2025 · 3 comments

Comments

@allan-cannon
Copy link

Which project does this relate to?

Router

Describe the bug

When using the zodSearchValidator to validate route params, I would expect it to also coerce the typescript type, however it remains a string.

This is perhaps a regression bug because this behavior worked as expected as recently as 1.81.5 (version in repro is 1.93.0).

For example when attempting to constrain the id param to a number in this example:

export const Route = createFileRoute('my-route/$id')({
  component: MyComponent,
  params: zodSearchValidator(
    z.object({
      id: z.coerce.number().int().min(1),
    }),
  ),
});

Typescript infers the value as a string even though logging the type shows it's a number:

function MyComponent() {
  const { id } = Route.useParams(); // typescript says id is a string here
  console.log(typeof id); // this will print 'number'
}

Not sure if this is relevant, but if I log the output of the zodSearchValidator function I get this:

{
  parse: <parse function>,
  types: {
    input: undefined,
    output: undefined,
  }
}

Your Example Website or App

https://stackblitz.com/edit/github-4wp3konf?file=src%2Froutes%2Fposts.%24postId.tsx

Steps to Reproduce the Bug or Issue

  1. Open the stackblitz
  2. Click the Post 35 button to navigate to the posts/$postId route
  3. Notice that it prints the type of postId as number
  4. Hover over the postId variable in the component of the posts.$postId.tsx file and see that it infers the type as string

Expected behavior

I assume this is just a type issue since everything works as expected except the type inference. I would expect it to be type number instead of type string.

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 131.0.6778.241

Additional context

No response

@allan-cannon allan-cannon changed the title param type validation with zod adapter does not appear to be working type inference not working for params using type validation with zod adapter Jan 2, 2025
@nullberri
Copy link

nullberri commented Jan 5, 2025

https://tanstack.com/router/latest/docs/framework/react/guide/search-params#zod

At some recent update they changed what you should be doing for Zod validation adapting. If you just swap the adapter for the import in the example everything works.

@nullberri
Copy link

@allan-cannon
Copy link
Author

Ah yeah ok I see I didn't notice the package changed, thanks for pointing that out.

+1 to the comment regarding typing issues. I definitely agree that the change is an improvement but it does feel like an undocumented breaking change.

@TanStack TanStack locked and limited conversation to collaborators Jan 6, 2025
@TanStack TanStack unlocked this conversation Jan 6, 2025
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

No branches or pull requests

2 participants