Skip to content

Commit c19a6c5

Browse files
authored
docs: prefer verbatimModuleSyntax when setting up type-only auto-imports (#12464)
that lets the TypeScript LSP handle `type` auto-imports, which means it works in any modern editor without any additional configuration
1 parent d7c16ed commit c19a6c5

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

docs/how-to/route-module-type-safety.md

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,38 +53,25 @@ When auto-importing the `Route` type helper, TypeScript will generate:
5353
import { Route } from "./+types/my-route";
5454
```
5555

56-
This will work, but you may want the `type` modifier for the import added automatically as well.
56+
But if you enable [verbatimModuleSyntax](https://www.typescriptlang.org/tsconfig/#verbatimModuleSyntax):
5757

58-
```ts filename=app/routes/my-route.tsx
59-
import type { Route } from "./+types/my-route";
60-
// ^^^^
61-
```
62-
63-
For example, this helps tools like bundlers to detect type-only module that can be safely excluded from the bundle.
64-
65-
### VSCode
66-
67-
In VSCode, you can get this behavior automatically by selecting `TypeScript › Preferences: Prefer Type Only Auto Imports` from the command palette or by manually setting `preferTypeOnlyAutoImports`:
68-
69-
```json filename=.vscode/settings.json
58+
```json filename=tsconfig.json
7059
{
71-
"typescript.preferences.preferTypeOnlyAutoImports": true
60+
"compilerOptions": {
61+
"verbatimModuleSyntax": true
62+
}
7263
}
7364
```
7465

75-
### eslint
76-
77-
In eslint, you can get this behavior by setting `prefer: "type-imports"` for the `consistent-type-imports` rule:
66+
Then, you will get the `type` modifier for the import automatically as well:
7867

79-
```json
80-
{
81-
"@typescript-eslint/consistent-type-imports": [
82-
"warn",
83-
{ "prefer": "type-imports" }
84-
]
85-
}
68+
```ts filename=app/routes/my-route.tsx
69+
import type { Route } from "./+types/my-route";
70+
// ^^^^
8671
```
8772

73+
This helps tools like bundlers to detect type-only module that can be safely excluded from the bundle.
74+
8875
## Conclusion
8976

9077
React Router's Vite plugin should be automatically generating types into `.react-router/types/` anytime you edit your route config (`routes.ts`).

0 commit comments

Comments
 (0)