Skip to content

Commit

Permalink
fix(types): narrow down the return type of defineConfig
Browse files Browse the repository at this point in the history
Fixes compatibility with `mergeConfig`, as reported in vuejs/create-vue#313

Before the change, `defineConfig` would return `UserConfigExport`, which
is a union type. This would cause `mergeConfig` to fail, as it doesn't
accept the `UserConfigFn` type in the union. (vitejs#13135)

After the change, `defineConfig` returns the same type that it receives,
so it would exclude `UserConfigFn` from the return type whenever
possible.
  • Loading branch information
haoqunjiang committed Jul 11, 2023
1 parent ec9d2e7 commit 790cee2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions packages/vite/src/node/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { mergeConfig } from '../publicUtils'

describe('mergeConfig', () => {
test('handles configs with different alias schemas', () => {
const baseConfig: UserConfigExport = {
const baseConfig = defineConfig({
resolve: {
alias: [
{
Expand All @@ -16,16 +16,16 @@ describe('mergeConfig', () => {
},
],
},
}
})

const newConfig: UserConfigExport = {
const newConfig = defineConfig({
resolve: {
alias: {
bar: 'bar-value',
baz: 'baz-value',
},
},
}
})

const mergedConfig: UserConfigExport = {
resolve: {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export type UserConfigExport = UserConfig | Promise<UserConfig> | UserConfigFn
* The function receives a {@link ConfigEnv} object that exposes two properties:
* `command` (either `'build'` or `'serve'`), and `mode`.
*/
export function defineConfig(config: UserConfigExport): UserConfigExport {
export function defineConfig<T extends UserConfigExport>(config: T): T {
return config
}

Expand Down

0 comments on commit 790cee2

Please sign in to comment.