Skip to content

Commit 37ba93c

Browse files
authored
fix(types): avoid merging object union types when using withDefaults (#10596)
close #10594
1 parent fc99e4d commit 37ba93c

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

packages/dts-test/setupHelpers.test-d.ts

+35
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,41 @@ describe('defineProps w/ union type declaration + withDefaults', () => {
102102
)
103103
})
104104

105+
describe('defineProps w/ object union + withDefaults', () => {
106+
const props = withDefaults(
107+
defineProps<
108+
{
109+
foo: string
110+
} & (
111+
| {
112+
type: 'hello'
113+
bar: string
114+
}
115+
| {
116+
type: 'world'
117+
bar: number
118+
}
119+
)
120+
>(),
121+
{
122+
foo: 'default value!',
123+
},
124+
)
125+
126+
expectType<
127+
| {
128+
readonly type: 'hello'
129+
readonly bar: string
130+
readonly foo: string
131+
}
132+
| {
133+
readonly type: 'world'
134+
readonly bar: number
135+
readonly foo: string
136+
}
137+
>(props)
138+
})
139+
105140
describe('defineProps w/ generic type declaration + withDefaults', <T extends
106141
number, TA extends {
107142
a: string

packages/runtime-core/src/apiSetupHelpers.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ export function defineModel(): any {
284284
}
285285

286286
type NotUndefined<T> = T extends undefined ? never : T
287+
type MappedOmit<T, K extends keyof any> = {
288+
[P in keyof T as P extends K ? never : P]: T[P]
289+
}
287290

288291
type InferDefaults<T> = {
289292
[K in keyof T]?: InferDefault<T, T[K]>
@@ -299,7 +302,7 @@ type PropsWithDefaults<
299302
T,
300303
Defaults extends InferDefaults<T>,
301304
BKeys extends keyof T,
302-
> = Readonly<Omit<T, keyof Defaults>> & {
305+
> = Readonly<MappedOmit<T, keyof Defaults>> & {
303306
readonly [K in keyof Defaults]-?: K extends keyof T
304307
? Defaults[K] extends undefined
305308
? T[K]

0 commit comments

Comments
 (0)