Skip to content

Commit b03cfe5

Browse files
committed
typesafety docs
1 parent a524852 commit b03cfe5

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

docs/discussion/type-safety.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: Type safety
3+
---
4+
5+
# Type safety
6+
7+
React Router generates types for each route in your app that you can use to get type safety for each route module export.
8+
9+
For example, let's say you have a `products/:id` route configured:
10+
11+
```ts filename=app/routes.ts
12+
import {
13+
type RouteConfig,
14+
route,
15+
} from "@react-router/dev/routes";
16+
17+
export const routes: RouteConfig = [
18+
route("products/:id", "./routes/product.tsx"),
19+
];
20+
```
21+
22+
Then, you can import route-specific types like so:
23+
24+
```tsx filename=app/routes/product.tsx
25+
import type * as Route from "./+types.product";
26+
// types generated for this route 👆
27+
28+
export function loader({ params }: Route.LoaderArgs) {
29+
// 👆 { id: string }
30+
return { planet: `world #${params.id}` };
31+
}
32+
33+
export default function Component({
34+
loaderData, // 👈 { planet: string }
35+
}: Route.ComponentProps) {
36+
return <h1>Hello, {loaderData.planet}!</h1>;
37+
}
38+
```
39+
40+
If you haven't done so already, check out our guide for [setting up type safety][setting-up-type-safety] in a new project.
41+
42+
## `typegen` command
43+
44+
You can manually generate types with the `typegen` command:
45+
46+
```sh
47+
react-router typegen
48+
```
49+
50+
You can also use `--watch` to automatically regenerate types as files change:
51+
52+
```sh
53+
react-router typegen --watch
54+
```
55+
56+
The following types are generated for each route:
57+
58+
- `LoaderArgs`
59+
- `ClientLoaderArgs`
60+
- `ActionArgs`
61+
- `ClientActionArgs`
62+
- `HydrateFallbackProps`
63+
- `ComponentProps` (for the `default` export)
64+
- `ErrorBoundaryProps`
65+
66+
## How it works
67+
68+
React Router's type generation executes your route config (`app/routes.ts` by default) to determine the routes for your app.
69+
It then generates a `+types.<route file>.d.ts` for each route within a special `.react-router/types/` directory.
70+
With [`rootDirs` configured][setting-up-type-safety], TypeScript can import these generated files as if they were right next to their corresponding route modules.
71+
72+
For a deeper dive into some of the design decisions, check out our [type inference decision doc](https://github.com/remix-run/react-router/blob/dev/decisions/0012-type-inference.md).
73+
74+
[setting-up-type-safety]: ../guides/setting-up-type-safety.md
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
title: Setting up type safety
3+
---
4+
5+
To know more about how type safety works in React Router, check out our [dedicated explanation](../discussion/type-safety.md).
6+
7+
# Setting up type safety
8+
9+
React Router generates types for into a `.react-router/` directory at the root of your app.
10+
This directory is fully managed by React Router and is derived from your route config (`app/routes.ts` by default), so it should be gitignore'd.
11+
12+
👉 **Add `.react-router/` to `.gitignore`**
13+
14+
```txt
15+
.react-router/
16+
```
17+
18+
You should also ensure that generated types are always present before running typechecking,
19+
especially for running typechecking in CI.
20+
21+
👉 **Add `react-router typegen` to your `typecheck` command in `package.json`**
22+
23+
```json
24+
{
25+
"scripts": {
26+
"typecheck": "react-router typegen && tsc"
27+
}
28+
}
29+
```
30+
31+
To get TypeScript to use those generated types, you'll need to add them to `include` in `tsconfig.json`.
32+
And to be able to import them as if they files next to your route modules, you'll also need to [configure `rootDirs`](https://www.typescriptlang.org/tsconfig/#rootDirs).
33+
34+
👉 **Configure `tsconfig.json` for generated types**
35+
36+
```json
37+
{
38+
"include": [".react-router/types/**/*"],
39+
"compilerOptions": {
40+
"rootDirs": [".", "./.react-router/types"]
41+
}
42+
}
43+
```
44+
45+
## Automatic typegen in VSCode (optional)
46+
47+
If you'd rather not need to remember to run `react-router typegen --watch` every time you start working on your app, you can use [VSCode Tasks](https://code.visualstudio.com/docs/editor/tasks) to automate this.
48+
49+
👉 **Add a `typegen:watch` script**
50+
51+
```json filename=package.json
52+
{
53+
"scripts": {
54+
"typegen:watch": "react-router typegen --watch"
55+
}
56+
}
57+
```
58+
59+
👉 **Configure a VSCode task for `typegen:watch`**
60+
61+
```json filename=.vscode/tasks.json
62+
{
63+
"version": "2.0.0",
64+
"tasks": [
65+
{
66+
"label": "React Router: Typegen",
67+
"type": "shell",
68+
"command": "npm run typegen:watch",
69+
"problemMatcher": [],
70+
"isBackground": true,
71+
"presentation": {
72+
"echo": true,
73+
"reveal": "always",
74+
"focus": false,
75+
"panel": "dedicated"
76+
},
77+
"runOptions": {
78+
"runOn": "folderOpen"
79+
}
80+
}
81+
]
82+
}
83+
```
84+
85+
Now, VSCode will automatically run the `typegen:watch` script in a dedicated terminal anytime you open your project.

0 commit comments

Comments
 (0)