]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): avoid merging object union types when using withDefaults (#10596)
authorThimo Sietsma <thimosietsma@gmail.com>
Mon, 15 Apr 2024 14:50:34 +0000 (16:50 +0200)
committerGitHub <noreply@github.com>
Mon, 15 Apr 2024 14:50:34 +0000 (22:50 +0800)
close #10594

packages/dts-test/setupHelpers.test-d.ts
packages/runtime-core/src/apiSetupHelpers.ts

index c749e80a5c7164c19b8f28acd34ea95304f30c32..883ebe6b254ff86a4176b5ce3cc093b829aa6f66 100644 (file)
@@ -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
   number, TA extends {
   a: string
index 0765a6ee88540846a462251e020a04c2054ff775..382bb30b3650aad287905c0001717e32f747a725 100644 (file)
@@ -284,6 +284,9 @@ export function defineModel(): any {
 }
 
 type NotUndefined<T> = T extends undefined ? never : T
+type MappedOmit<T, K extends keyof any> = {
+  [P in keyof T as P extends K ? never : P]: T[P]
+}
 
 type InferDefaults<T> = {
   [K in keyof T]?: InferDefault<T, T[K]>
@@ -299,7 +302,7 @@ type PropsWithDefaults<
   T,
   Defaults extends InferDefaults<T>,
   BKeys extends keyof T,
-> = Readonly<Omit<T, keyof Defaults>> & {
+> = Readonly<MappedOmit<T, keyof Defaults>> & {
   readonly [K in keyof Defaults]-?: K extends keyof T
     ? Defaults[K] extends undefined
       ? T[K]