Skip to content

Commit

Permalink
fix(types): add more overload to defineConfig (#18299)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Oct 8, 2024
1 parent 466f94a commit 94e34cf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
47 changes: 47 additions & 0 deletions packages/vite/src/node/__tests_dts__/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-unused-vars */
import type { Equal, ExpectTrue } from '@type-challenges/utils'
import {
type UserConfig,
type UserConfigExport,
type UserConfigFn,
type UserConfigFnObject,
type UserConfigFnPromise,
defineConfig,
} from '../config'
import { mergeConfig } from '../publicUtils'

const configObjectDefined = defineConfig({})
const configObjectPromiseDefined = defineConfig(Promise.resolve({}))
const configFnObjectDefined = defineConfig(() => ({}))
const configFnPromiseDefined = defineConfig(async () => ({}))
const configFnDefined = defineConfig(() =>
// TypeScript requires both non-promise config and
// promise config to have at least one property
Math.random() > 0.5 ? { base: '' } : Promise.resolve({ base: '/' }),
)
const configExportDefined = defineConfig({} as UserConfigExport)

export type cases1 = [
ExpectTrue<Equal<typeof configObjectDefined, UserConfig>>,
ExpectTrue<Equal<typeof configObjectPromiseDefined, Promise<UserConfig>>>,
ExpectTrue<Equal<typeof configFnObjectDefined, UserConfigFnObject>>,
ExpectTrue<Equal<typeof configFnPromiseDefined, UserConfigFnPromise>>,
ExpectTrue<Equal<typeof configFnDefined, UserConfigFn>>,
ExpectTrue<Equal<typeof configExportDefined, UserConfigExport>>,
]

defineConfig({
base: '',
// @ts-expect-error
unknownProperty: 1,
})

mergeConfig(defineConfig({}), defineConfig({}))
mergeConfig(
// @ts-expect-error
defineConfig(() => ({})),
defineConfig({}),
)

export {}
2 changes: 2 additions & 0 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export type UserConfigExport =
export function defineConfig(config: UserConfig): UserConfig
export function defineConfig(config: Promise<UserConfig>): Promise<UserConfig>
export function defineConfig(config: UserConfigFnObject): UserConfigFnObject
export function defineConfig(config: UserConfigFnPromise): UserConfigFnPromise
export function defineConfig(config: UserConfigFn): UserConfigFn
export function defineConfig(config: UserConfigExport): UserConfigExport
export function defineConfig(config: UserConfigExport): UserConfigExport {
return config
Expand Down

0 comments on commit 94e34cf

Please sign in to comment.