TanStackT
TanStack15mo ago
4 replies
ordinary-sapphire

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
  })}
>

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 '{}'

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!,
    };
  }}
>

But that solution is very ugly, and it feels really wrong.
How do I handle this in a better way?
Was this page helpful?