diff --git a/packages-private/dts-test/setupHelpers.test-d.ts b/packages-private/dts-test/setupHelpers.test-d.ts index 4074176ff98..dc21578ecd4 100644 --- a/packages-private/dts-test/setupHelpers.test-d.ts +++ b/packages-private/dts-test/setupHelpers.test-d.ts @@ -207,6 +207,77 @@ describe('defineProps w/ generic type declaration + withDefaults', (res.bool) }) +describe('defineProps w/union type', () => { + type PP = + | { + type: 'text' + mm: string + } + | { + type: 'number' + mm: number | null + } + + const res = defineProps() + expectType(res.mm) + + if (res.type === 'text') { + expectType(res.mm) + } + + if (res.type === 'number') { + expectType(res.mm) + } +}) + +describe('withDefaults w/ union type', () => { + type PP = + | { + type?: 'text' + mm: string + } + | { + type?: 'number' + mm: number | null + } + + const res = withDefaults(defineProps(), { + type: 'text', + }) + + if (res.type && res.type === 'text') { + expectType(res.mm) + } + + if (res.type === 'number') { + expectType(res.mm) + } +}) + +describe('withDefaults w/ generic union type', () => { + type PP = + | { + tt?: 'a' + mm: T + } + | { + tt?: 'b' + mm: T[] + } + + const res = withDefaults(defineProps(), { + tt: 'a', + }) + + if (res.tt === 'a') { + expectType(res.mm) + } else { + expectType(res.mm) + } +}) + describe('withDefaults w/ boolean type', () => { const res1 = withDefaults( defineProps<{ diff --git a/packages/runtime-core/src/apiSetupHelpers.ts b/packages/runtime-core/src/apiSetupHelpers.ts index 33817818a93..e54c69728e4 100644 --- a/packages/runtime-core/src/apiSetupHelpers.ts +++ b/packages/runtime-core/src/apiSetupHelpers.ts @@ -313,9 +313,6 @@ export function defineModel(): any { } type NotUndefined = T extends undefined ? never : T -type MappedOmit = { - [P in keyof T as P extends K ? never : P]: T[P] -} type InferDefaults = { [K in keyof T]?: InferDefault @@ -331,7 +328,7 @@ type PropsWithDefaults< T, Defaults extends InferDefaults, BKeys extends keyof T, -> = Readonly> & { +> = Readonly & { readonly [K in keyof Defaults as K extends keyof T ? K : never]-?: K extends keyof T