Skip to content

Commit cbee956

Browse files
committed
typegen: update changeset
1 parent 7a951c4 commit cbee956

File tree

1 file changed

+3
-115
lines changed

1 file changed

+3
-115
lines changed

.changeset/typesafety.md

Lines changed: 3 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,6 @@ This initial implementation targets type inference for:
2424
- `LoaderData` : Loader data from `loader` and/or `clientLoader` within your route module
2525
- `ActionData` : Action data from `action` and/or `clientAction` within your route module
2626

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-
3727
In the future, we plan to add types for the rest of the route module exports: `meta`, `links`, `headers`, `shouldRevalidate`, etc.
3828
We also plan to generate types for typesafe `Link`s:
3929

@@ -43,109 +33,7 @@ We also plan to generate types for typesafe `Link`s:
4333
// typesafe `to` and `params` based on the available routes in your app
4434
```
4535

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:
14837

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

Comments
 (0)