You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: .changeset/typesafety.md
+3-115Lines changed: 3 additions & 115 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,16 +24,6 @@ This initial implementation targets type inference for:
24
24
-`LoaderData` : Loader data from `loader` and/or `clientLoader` within your route module
25
25
-`ActionData` : Action data from `action` and/or `clientAction` within your route module
26
26
27
-
These types are then used to create types for route export args and props:
28
-
29
-
-`LoaderArgs`
30
-
-`ClientLoaderArgs`
31
-
-`ActionArgs`
32
-
-`ClientActionArgs`
33
-
-`HydrateFallbackProps`
34
-
-`ComponentProps` (for the `default` export)
35
-
-`ErrorBoundaryProps`
36
-
37
27
In the future, we plan to add types for the rest of the route module exports: `meta`, `links`, `headers`, `shouldRevalidate`, etc.
38
28
We also plan to generate types for typesafe `Link`s:
39
29
@@ -43,109 +33,7 @@ We also plan to generate types for typesafe `Link`s:
43
33
// typesafe `to` and `params` based on the available routes in your app
44
34
```
45
35
46
-
#### Setup
47
-
48
-
React Router will generate types into a `.react-router/` directory at the root of your app.
49
-
This directory is fully managed by React Router and is derived based on your route config (`routes.ts`).
50
-
51
-
👉 **Add `.react-router/` to `.gitignore`**
52
-
53
-
```txt
54
-
.react-router
55
-
```
56
-
57
-
You should also ensure that generated types for routes are always present before running typechecking,
58
-
especially for running typechecking in CI.
59
-
60
-
👉 **Add `react-router typegen` to your `typecheck` command in `package.json`**
61
-
62
-
```json
63
-
{
64
-
"scripts": {
65
-
"typecheck": "react-router typegen && tsc"
66
-
}
67
-
}
68
-
```
69
-
70
-
To get TypeScript to use those generated types, you'll need to add them to `include` in `tsconfig.json`.
71
-
And to be able to import them as if they files next to your route modules, you'll also need to configure `rootDirs`.
72
-
73
-
👉 **Configure `tsconfig.json` for generated types**
74
-
75
-
```json
76
-
{
77
-
"include": [".react-router/types/**/*"],
78
-
"compilerOptions": {
79
-
"rootDirs": [".", "./.react-router/types"]
80
-
}
81
-
}
82
-
```
83
-
84
-
#### `typegen` command
85
-
86
-
You can manually generate types with the new `typegen` command:
87
-
88
-
```sh
89
-
react-router typegen
90
-
```
91
-
92
-
However, manual type generation is tedious and types can get out of sync quickly if you ever forget to run `typegen`.
93
-
Instead, we recommend that you setup our new TypeScript plugin which will automatically generate fresh types whenever routes change.
94
-
That way, you'll always have up-to-date types.
95
-
96
-
#### TypeScript plugin
97
-
98
-
To get automatic type generation, you can use our new TypeScript plugin.
99
-
100
-
👉 **Add the TypeScript plugin to `tsconfig.json`**
101
-
102
-
```json
103
-
{
104
-
"compilerOptions": {
105
-
"plugins": [{ "name": "@react-router/dev" }]
106
-
}
107
-
}
108
-
```
109
-
110
-
We plan to add some other goodies to our TypeScript plugin soon, including:
111
-
112
-
- Automatic `jsdoc` for route exports that include links to official docs
113
-
- Autocomplete for route exports
114
-
- Warnings for non-HMR compliant exports
115
-
116
-
##### VSCode
117
-
118
-
TypeScript looks for plugins registered in `tsconfig.json` in the local `node_modules/`,
119
-
but VSCode ships with its own copy of TypeScript that is installed outside of your project.
120
-
For TypeScript plugins to work, you'll need to tell VSCode to use the local workspace version of TypeScript.
121
-
For security reasons, [VSCode won't use the workspace version of TypeScript](https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-the-workspace-version-of-typescript) until you manually opt-in.
122
-
123
-
Your project should have a `.vscode/settings.json` with the following settings:
124
-
125
-
```json
126
-
{
127
-
"typescript.tsdk": "node_modules/typescript/lib",
128
-
"typescript.enablePromptUseWorkspaceTsdk": true
129
-
}
130
-
```
131
-
132
-
That way [VSCode will ask you](https://code.visualstudio.com/updates/v1_45#_prompt-users-to-switch-to-the-workspace-version-of-typescript) if you want to use the workspace version of TypeScript the first time you open a TS file in that project.
133
-
134
-
> [!IMPORTANT]
135
-
> You'll need to install dependencies first so that the workspace version of TypeScript is available.
136
-
137
-
👉 **Select "Allow" when VSCode asks if you want to use the workspace version of TypeScript**
138
-
139
-
Otherwise, you can also manually opt-in to the workspace version:
140
-
141
-
1. Open up any TypeScript file in your project
142
-
2. Open up the VSCode Command Palette (<kbd>Cmd</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd>)
143
-
3. Search for `Select TypeScript Version`
144
-
4. Choose `Use Workspace Version`
145
-
5. Quit and reopen VSCode
146
-
147
-
##### Troubleshooting
36
+
Check out our docs for more:
148
37
149
-
In VSCode, open up any TypeScript file in your project and then use <kbd>CMD</kbd>+<kbd>SHIFT</kbd>+<kbd>P</kbd> to select `Open TS Server log`.
150
-
There should be a log for `[react-router] setup` that indicates that the plugin was resolved correctly.
151
-
Then look for any errors in the log.
38
+
-[_Explanations > Type Safety_](https://reactrouter.com/dev/guides/explanation/type-safety)
39
+
-[_How-To > Setting up type safety_](https://reactrouter.com/dev/guides/how-to/setting-up-type-safety)
0 commit comments