]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): correct withDefaults return type for boolean prop with undefined default...
authorzqran <215244947@qq.com>
Tue, 11 Jul 2023 10:35:22 +0000 (18:35 +0800)
committerGitHub <noreply@github.com>
Tue, 11 Jul 2023 10:35:22 +0000 (18:35 +0800)
packages/dts-test/setupHelpers.test-d.ts
packages/runtime-core/src/apiSetupHelpers.ts

index 77342590dc6a53fb13d13d88697658cac1841fa8..934e6056d2d2539b564cf0a48b89dca9cdb38d15 100644 (file)
@@ -134,6 +134,26 @@ describe('defineProps w/ generic type declaration + withDefaults', <T extends nu
   expectType<boolean>(res.bool)
 })
 
+describe('withDefaults w/ boolean type', () => {
+  const res1 = withDefaults(
+    defineProps<{
+      bool?: boolean
+    }>(),
+    { bool: false }
+  )
+  expectType<boolean>(res1.bool)
+
+  const res2 = withDefaults(
+    defineProps<{
+      bool?: boolean
+    }>(),
+    {
+      bool: undefined
+    }
+  )
+  expectType<boolean | undefined>(res2.bool)
+})
+
 describe('defineProps w/ runtime declaration', () => {
   // runtime declaration
   const props = defineProps({
index c00937981d915003c0c9e7e7e02c1d1a5cd0c402..93200667081b1925af0fb7fe1246f3b38e5835f5 100644 (file)
@@ -303,7 +303,13 @@ type PropsWithDefaults<
       ? T[K]
       : NotUndefined<T[K]>
     : never
-} & { readonly [K in BKeys]-?: boolean }
+} & {
+  readonly [K in BKeys]-?: K extends keyof Defaults
+    ? Defaults[K] extends undefined
+      ? boolean | undefined
+      : boolean
+    : boolean
+}
 
 /**
  * Vue `<script setup>` compiler macro for providing props default values when