Skip to content

Commit c971f26

Browse files
authored
fix(types): narrow down the return type of defineConfig (#13792)
1 parent 975a631 commit c971f26

File tree

6 files changed

+16
-13
lines changed

6 files changed

+16
-13
lines changed

packages/vite/src/node/__tests__/config.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { mergeConfig } from '../publicUtils'
77

88
describe('mergeConfig', () => {
99
test('handles configs with different alias schemas', () => {
10-
const baseConfig: UserConfigExport = {
10+
const baseConfig = defineConfig({
1111
resolve: {
1212
alias: [
1313
{
@@ -16,16 +16,16 @@ describe('mergeConfig', () => {
1616
},
1717
],
1818
},
19-
}
19+
})
2020

21-
const newConfig: UserConfigExport = {
21+
const newConfig = defineConfig({
2222
resolve: {
2323
alias: {
2424
bar: 'bar-value',
2525
baz: 'baz-value',
2626
},
2727
},
28-
}
28+
})
2929

3030
const mergedConfig: UserConfigExport = {
3131
resolve: {

packages/vite/src/node/config.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,26 @@ export interface ConfigEnv {
100100
*/
101101
export type AppType = 'spa' | 'mpa' | 'custom'
102102

103+
export type UserConfigFnObject = (env: ConfigEnv) => UserConfig
104+
export type UserConfigFnPromise = (env: ConfigEnv) => Promise<UserConfig>
103105
export type UserConfigFn = (env: ConfigEnv) => UserConfig | Promise<UserConfig>
104-
export type UserConfigExport = UserConfig | Promise<UserConfig> | UserConfigFn
106+
107+
export type UserConfigExport =
108+
| UserConfig
109+
| Promise<UserConfig>
110+
| UserConfigFnObject
111+
| UserConfigFnPromise
112+
| UserConfigFn
105113

106114
/**
107115
* Type helper to make it easier to use vite.config.ts
108116
* accepts a direct {@link UserConfig} object, or a function that returns it.
109117
* The function receives a {@link ConfigEnv} object that exposes two properties:
110118
* `command` (either `'build'` or `'serve'`), and `mode`.
111119
*/
120+
export function defineConfig(config: UserConfig): UserConfig
121+
export function defineConfig(config: Promise<UserConfig>): Promise<UserConfig>
122+
export function defineConfig(config: UserConfigExport): UserConfigExport
112123
export function defineConfig(config: UserConfigExport): UserConfigExport {
113124
return config
114125
}

playground/assets/vite.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import path from 'node:path'
22
import { defineConfig } from 'vite'
33

4-
/** @type {import('vite').UserConfig} */
5-
// @ts-expect-error typecast
64
export default defineConfig({
75
base: '/foo',
86
publicDir: 'static',

playground/css/vite.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ globalThis.window = {}
99
// @ts-expect-error refer to https://github.com/vitejs/vite/pull/11079
1010
globalThis.location = new URL('http://localhost/')
1111

12-
/** @type {import('vite').UserConfig} */
13-
// @ts-expect-error typecast
1412
export default defineConfig({
1513
build: {
1614
cssTarget: 'chrome61',

playground/define/vite.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { defineConfig } from 'vite'
22

3-
/** @type {import('vite').UserConfig} */
4-
// @ts-expect-error typecast
53
export default defineConfig({
64
define: {
75
__EXP__: 'false',

playground/lib/vite.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import fs from 'node:fs'
22
import path from 'node:path'
33
import { defineConfig } from 'vite'
44

5-
/** @type {import('vite').UserConfig} */
6-
// @ts-expect-error typecast
75
export default defineConfig({
86
esbuild: {
97
supported: {

0 commit comments

Comments
 (0)