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
No type safety on useSearch({ strict: false }) #1217
Comments
This is incorrect, this is type safe. You get back the union of all possible search parameters. see https://stackblitz.com/edit/tanstack-router-kws5zj?file=src%2Froutes%2Fdashboard.users.tsx const search: {} | {} | {} | {
showNotes?: boolean | undefined;
notes?: string | undefined;
} | {
usersView?: {
sortBy?: "id" | "name" | "email" | undefined;
filterBy?: string | undefined;
} | undefined;
} | {
...;
} | {
...;
} | {
...;
}
Indeed, on stackblitz it does not work. |
@schiller-manuel I have tested in a "proper IDE". It was my thought that StackBlitz used VS Code on the web? So I figured providing StackBlitz examples would be easiest to reproduce. Here is what I am seeing on VS Code on my local machine...
|
Since one of the union members is the empty object Can you please provide a reproducer where the |
@schiller-manuel Ok, that makes sense, so only by using the I was able to reproduce the Line 435 has the error |
Oh my bad, forgot to fork it. Try this link |
thanks for the reproducer, the fix is released with https://github.com/TanStack/router/releases/tag/v1.17.4 well... at least for some cases :( |
Hm yes, seems like it worked for my smaller example, but once I updated that to have a few more routes it unfortunately broke again. |
Do you know what could be some possible usecases where useSearch would return an empty object as a type? I too am facing this issue and from as far as I can tell when using zod schemas atleast, it will give me the correct union of all the types but adds on that empty object. |
you probably have at least one route without a |
@schiller-manuel Gotcha, yes I have multiple. Workaround? Should we add a validateSearch to all routes and somehow return undefined? |
why is having the empty object a problem? |
Because I am getting a type safety issue where the key does not exist within the empty object. const { page } = useSearch({
strict: false
}); Property 'page' does not exist on type '{} | { redirect?: string | undefined; } | { sort?: string | undefined; search?: string | undefined; page?: number | undefined; limit?: number | undefined; } | { sort?: string | undefined; search?: string | undefined; page?: number | undefined; limit?: number | undefined; } | { ...; }'. Edit: Specifically this: Property 'page' does not exist on type {}. I have plenty of pages that are validating the search params including a The use case I have is a component that updates a single param in the url across multiple tables/pages. |
you also have a route with search If const search = useSearch({
strict: false
});
if ('page' in search) {
console.log(page.search);
} |
Got it.
|
"Any idea when it will no longer be experimental?" for other questions please ask in our discord community at https://discord.com/channels/719702312431386674/1023930177224462388 |
Describe the bug
The type safety is not working as expected for
useSearch
oruseParams
when usingstrict: false
in the options.Your Example Website or App
https://stackblitz.com/edit/tanstack-router-nan1d9?file=src%2Froutes%2Fdashboard.users.tsx
Steps to Reproduce the Bug or Issue
route.useSearch
touseSearch({ strict: false })
useParams({ strict: false })
Expected behavior
With
strict: false
the type safety should be loosened to show all available types.Screenshots or Videos
useSearch({ strict: false })
no type safetyuseSearch({ strict: false, experimental_returnIntersection: true }) does not work. This does fix the same issue I see with
useParams` though.useParams({ strict: false })
no type safetyuseParams({ strict: false, experimental_returnIntersection: true })
fixes the issue foruseParams
Platform
tanstack router version: 1.16.6
typescript: 5.3.3 (on my local, not sure what stackblitz is on)
Additional context
No response
The text was updated successfully, but these errors were encountered: