Skip to content

Commit 52469c3

Browse files
feat(create-vite): set types compiler option in tsconfigs (#20132)
Co-authored-by: 翠 <green@sapphi.red>
1 parent b178f90 commit 52469c3

29 files changed

Lines changed: 41 additions & 34 deletions

File tree

docs/guide/api-hmr.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,14 @@ if (import.meta.hot) {
6262
6363
## IntelliSense for TypeScript
6464
65-
Vite provides type definitions for `import.meta.hot` in [`vite/client.d.ts`](https://github.com/vitejs/vite/blob/main/packages/vite/client.d.ts). You can create an `vite-env.d.ts` in the `src` directory so TypeScript picks up the type definitions:
65+
Vite provides type definitions for `import.meta.hot` in [`vite/client.d.ts`](https://github.com/vitejs/vite/blob/main/packages/vite/client.d.ts). You can add "vite/client" in the `tsconfig.json` so TypeScript picks up the type definitions:
6666
67-
```ts [vite-env.d.ts]
68-
/// <reference types="vite/client" />
67+
```json [tsconfig.json]
68+
{
69+
"compilerOptions": {
70+
"types": ["vite/client"]
71+
}
72+
}
6973
```
7074
7175
## `hot.accept(cb)`

docs/guide/env-and-mode.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ By default, Vite provides type definitions for `import.meta.env` in [`vite/clien
117117
To achieve this, you can create an `vite-env.d.ts` in `src` directory, then augment `ImportMetaEnv` like this:
118118
119119
```typescript [vite-env.d.ts]
120-
/// <reference types="vite/client" />
121-
122120
interface ViteTypeOptions {
123121
// By adding this line, you can make the type of ImportMetaEnv strict
124122
// to disallow unknown keys.

docs/guide/features.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,7 @@ Vite starter templates have `"skipLibCheck": "true"` by default to avoid typeche
113113

114114
### Client Types
115115

116-
Vite's default types are for its Node.js API. To shim the environment of client-side code in a Vite application, add a `d.ts` declaration file:
117-
118-
```typescript
119-
/// <reference types="vite/client" />
120-
```
121-
122-
::: details Using `compilerOptions.types`
123-
124-
Alternatively, you can add `vite/client` to `compilerOptions.types` inside `tsconfig.json`:
116+
Vite's default types are for its Node.js API. To shim the environment of client-side code in a Vite application, you can add `vite/client` to `compilerOptions.types` inside `tsconfig.json`:
125117

126118
```json [tsconfig.json]
127119
{
@@ -131,7 +123,15 @@ Alternatively, you can add `vite/client` to `compilerOptions.types` inside `tsco
131123
}
132124
```
133125

134-
Note that if [`compilerOptions.types`](https://www.typescriptlang.org/tsconfig#types) is specified, only these packages will be included in the global scope (instead of all visible ”@types” packages).
126+
Note that if [`compilerOptions.types`](https://www.typescriptlang.org/tsconfig#types) is specified, only these packages will be included in the global scope (instead of all visible ”@types” packages). This is recommended since TS 5.9.
127+
128+
::: details Using triple-slash directive
129+
130+
Alternatively, you can add a `d.ts` declaration file:
131+
132+
```typescript [vite-env.d.ts]
133+
/// <reference types="vite/client" />
134+
```
135135

136136
:::
137137

@@ -153,7 +153,13 @@ For example, to make the default import of `*.svg` a React component:
153153
export default content
154154
}
155155
```
156-
- The file containing the reference to `vite/client` (normally `vite-env.d.ts`):
156+
- If you are using `compilerOptions.types`, ensure the file is included in `tsconfig.json`:
157+
```json [tsconfig.json]
158+
{
159+
"include": ["src", "./vite-env-override.d.ts"]
160+
}
161+
```
162+
- If you are using triple-slash directives, update the file containing the reference to `vite/client` (normally `vite-env.d.ts`):
157163
```ts
158164
/// <reference types="./vite-env-override.d.ts" />
159165
/// <reference types="vite/client" />

packages/create-vite/template-lit-ts/src/vite-env.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/create-vite/template-lit-ts/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"useDefineForClassFields": false,
66
"module": "ESNext",
77
"lib": ["ES2022", "DOM", "DOM.Iterable"],
8+
"types": ["vite/client"],
89
"skipLibCheck": true,
910

1011
/* Bundler mode */

packages/create-vite/template-preact-ts/src/vite-env.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/create-vite/template-preact-ts/tsconfig.app.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"useDefineForClassFields": true,
66
"module": "ESNext",
77
"lib": ["ES2022", "DOM", "DOM.Iterable"],
8+
"types": ["vite/client"],
89
"skipLibCheck": true,
910
"paths": {
1011
"react": ["./node_modules/preact/compat/"],

packages/create-vite/template-preact-ts/tsconfig.node.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"target": "ES2023",
55
"lib": ["ES2023"],
66
"module": "ESNext",
7+
"types": [],
78
"skipLibCheck": true,
89

910
/* Bundler mode */

packages/create-vite/template-qwik-ts/src/vite-env.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/create-vite/template-qwik-ts/tsconfig.app.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"useDefineForClassFields": true,
66
"module": "ESNext",
77
"lib": ["ES2022", "DOM", "DOM.Iterable"],
8+
"types": ["vite/client"],
89
"skipLibCheck": true,
910

1011
/* Bundler mode */

0 commit comments

Comments
 (0)