We can take advantage of TypeScript by setting our loader
function to the LoaderFunction
type from @remix-run/node
Then we can ensure that our loader is returning the type that we want by creating a custom type called LoaderData
with a property of posts
.
We’ll also do a cool trick where we get the awaited return type of our getPosts
async function. This is done like this posts: Awaited<ReturnType<typeof getPosts>>
Where is the video for this lesson?
Feels odd to have to type cast useLoaderData
as LoaderData
when in v1 it was possible to do useLoaderData<typeof loader>()
? Will this be changed again in the future to not have to cast types and infer them instead?