Most example I found about how to correctly type useLoaderData
do it like this
export const loader = async () => {
return json({ messages: [...] })
}
// In component...
const { messages } = useLoaderData<typeof loader>
However, since V2, useLoaderData
is typed like this
export declare function useLoaderData(): unknown;
and we can't pass generics anymore. What's surprising is I can't find a single page mentioning this critical change. What is the best way to get type safety then ? How can I tell TypeScript that messages actually exits in the response, without having to hardcode the type itself and use an ugly cast with as
?
Thank you!
I just found the answer ! My IDE imported
useLoaderData
fromreact-router
instead of@remix-run/react
by default. It doesn't even suggest me the right package, that's why I did not realize it. I went through the changelog again and I noticed the difference !