Sub topic 8
Type-safe route modules with generated types
This chapter assumes you know TypeScript basics, have read the earlier chapters on route configuration and loaders, and have a framework mode project running.
Why generated types exist
A route module exports a loader, an action, and a default component. The component receives whatever the loader returned, but TypeScript cannot see that link by reading the file alone. Different routes also have different URL params.
React Router solves this by generating a types file for every route in app/routes.ts. You import from it instead of writing types by hand.
Where the types come from
For a route file routes/contacts/detail.tsx, React Router creates a sibling module named +types/detail. The files live in a .react-router/types directory, and the rootDirs setting in tsconfig.json makes them look like they sit next to your route files.
The Route namespace you import from it contains types for each export:
| Type | Used for |
|---|---|
| `Route.LoaderArgs` | The argument to `loader`, including typed `params` |
| `Route.ActionArgs` | The argument to `action` |
| `Route.ComponentProps` | Props of the default export: `loaderData`, `actionData`, `params`, `matches` |
| `Route.ErrorBoundaryProps` | Props of `ErrorBoundary` |
Using the types
import { data } from "react-router";
import type { Route } from "./+types/detail";
import { getContact } from "~/contacts.server";
export async function loader({ params }: Route.LoaderArgs) {
const contact = await getContact(params.contactId);
if (!contact) {
throw data("Contact not found", { status: 404 });
}
return { contact };
}
export default function ContactDetail({ loaderData }: Route.ComponentProps) {
return <h1>{loaderData.contact.name}</h1>;
}Two things are inferred here:
params.contactIdis astring, because the route path iscontacts/:contactId. A typo such asparams.contactIDis a compile error.loaderDatahas the exact return type ofloader. If you renamecontacttopersonin the loader, the component fails to compile until you update it.
Running the type generator
Types are produced by the dev server and by the typegen command. In a clean checkout or CI job, nothing exists until you generate them, so run it before the type check:
npx react-router typegen && npx tscMost project templates wrap this in a typecheck script in package.json.
The flow
Common errors
- Cannot find module
./+types/detail. The types have not been generated yet, orrootDirsintsconfig.jsondoes not include.react-router/types. Start the dev server or runreact-router typegen, and check the template'stsconfig.json. - Stale types after renaming a route file. The generated file name follows the route file name. Rerun typegen, and update the import path.
- Types import from the wrong route. Each route module must import from its own
+types/<filename>. Copying an import from another route gives you the wrong params and data. - Loader data is not what you expect. Values that cannot be serialized, such as class instances, may not arrive in the component as the same type. Return plain objects.
Exercise
A route is registered as route("posts/:postId/comments/:commentId", "routes/comment.tsx"). Which import gives you typed access to params.postId and params.commentId in its loader, and what must you do if the import cannot be found?
Show answer
Use import type { Route } from "./+types/comment"; and type the loader argument as Route.LoaderArgs. If the module cannot be found, generate the types by starting the dev server or running npx react-router typegen, and confirm tsconfig.json has .react-router/types in rootDirs.