From 94e34cf1dfe6fdb331b6508e830b2cc446000aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Tue, 8 Oct 2024 16:52:50 +0900 Subject: [PATCH] fix(types): add more overload to `defineConfig` (#18299) --- .../vite/src/node/__tests_dts__/config.ts | 47 +++++++++++++++++++ packages/vite/src/node/config.ts | 2 + 2 files changed, 49 insertions(+) create mode 100644 packages/vite/src/node/__tests_dts__/config.ts diff --git a/packages/vite/src/node/__tests_dts__/config.ts b/packages/vite/src/node/__tests_dts__/config.ts new file mode 100644 index 00000000000000..0d8c88290faa83 --- /dev/null +++ b/packages/vite/src/node/__tests_dts__/config.ts @@ -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>, + ExpectTrue>>, + ExpectTrue>, + ExpectTrue>, + ExpectTrue>, + ExpectTrue>, +] + +defineConfig({ + base: '', + // @ts-expect-error + unknownProperty: 1, +}) + +mergeConfig(defineConfig({}), defineConfig({})) +mergeConfig( + // @ts-expect-error + defineConfig(() => ({})), + defineConfig({}), +) + +export {} diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 16d5e159a22756..5eb1045ef51c65 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -131,6 +131,8 @@ export type UserConfigExport = export function defineConfig(config: UserConfig): UserConfig export function defineConfig(config: Promise): Promise 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