T
TanStack11mo ago
provincial-silver

Handling types of "prev" value

I have a link in my code that looks like this:
<Link
to="/organization/$organizationSlug/"
params={(prev) => ({
organizationSlug: prev.organizationSlug
})}
>
<Link
to="/organization/$organizationSlug/"
params={(prev) => ({
organizationSlug: prev.organizationSlug
})}
>
At this point in my code I am 100% sure that organizaionSlug is valid and defined, since I handle that validation in beforeLoad, in the router. But prev.organizaionSlug is not a valid input for my params, and I get this type error:
Property 'organizationSlug' does not exist on type '{} | {} | ({} & { organizationSlug: string; }) .......
Property 'organizationSlug' does not exist on type '{}'
Property 'organizationSlug' does not exist on type '{} | {} | ({} & { organizationSlug: string; }) .......
Property 'organizationSlug' does not exist on type '{}'
I am able to get the error to disappear by doing this:
<Link
to="/organization/$organizationSlug/"
params={(prev) => {
const prevTyped = prev as { organizationSlug?: string };
return {
organizationSlug: prevTyped.organizationSlug!,
};
}}
>
<Link
to="/organization/$organizationSlug/"
params={(prev) => {
const prevTyped = prev as { organizationSlug?: string };
return {
organizationSlug: prevTyped.organizationSlug!,
};
}}
>
But that solution is very ugly, and it feels really wrong. How do I handle this in a better way?
2 Replies
metropolitan-bronze
metropolitan-bronze11mo ago
you must set the from property then all is handled automatically in fact, if your from already has the path param, you don't need to specify it anymore
provincial-silver
provincial-silverOP11mo ago
Thank you!

Did you find this page helpful?