From 37ba93c213a81f99a68a99ef5d4065d61b150ba3 Mon Sep 17 00:00:00 2001 From: Thimo Sietsma Date: Mon, 15 Apr 2024 16:50:34 +0200 Subject: [PATCH] fix(types): avoid merging object union types when using withDefaults (#10596) close #10594 --- packages/dts-test/setupHelpers.test-d.ts | 35 ++++++++++++++++++++ packages/runtime-core/src/apiSetupHelpers.ts | 5 ++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/packages/dts-test/setupHelpers.test-d.ts b/packages/dts-test/setupHelpers.test-d.ts index c749e80a5c7..883ebe6b254 100644 --- a/packages/dts-test/setupHelpers.test-d.ts +++ b/packages/dts-test/setupHelpers.test-d.ts @@ -102,6 +102,41 @@ describe('defineProps w/ union type declaration + withDefaults', () => { ) }) +describe('defineProps w/ object union + withDefaults', () => { + const props = withDefaults( + defineProps< + { + foo: string + } & ( + | { + type: 'hello' + bar: string + } + | { + type: 'world' + bar: number + } + ) + >(), + { + foo: 'default value!', + }, + ) + + expectType< + | { + readonly type: 'hello' + readonly bar: string + readonly foo: string + } + | { + readonly type: 'world' + readonly bar: number + readonly foo: string + } + >(props) +}) + describe('defineProps w/ generic type declaration + withDefaults', = 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 @@ -299,7 +302,7 @@ type PropsWithDefaults< T, Defaults extends InferDefaults, BKeys extends keyof T, -> = Readonly> & { +> = Readonly> & { readonly [K in keyof Defaults]-?: K extends keyof T ? Defaults[K] extends undefined ? T[K]