]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): strip non-prop default values from return type of withDefaults (#9998)
authoryangxiuxiu <79584569+yangxiuxiu1115@users.noreply.github.com>
Mon, 19 Aug 2024 08:29:43 +0000 (16:29 +0800)
committerGitHub <noreply@github.com>
Mon, 19 Aug 2024 08:29:43 +0000 (16:29 +0800)
close #9899

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

index 28b3c01cd0ff7856fbc117c0e75383077e5e9248..64c944e0be22d8609e91bc7aff542ffb6b9aaaf5 100644 (file)
@@ -227,6 +227,19 @@ describe('withDefaults w/ boolean type', () => {
   expectType<boolean | undefined>(res2.bool)
 })
 
+describe('withDefaults w/ defineProp type is different from the defaults type', () => {
+  const res1 = withDefaults(
+    defineProps<{
+      bool?: boolean
+    }>(),
+    { bool: false, value: false },
+  )
+  expectType<boolean>(res1.bool)
+
+  // @ts-expect-error
+  res1.value
+})
+
 describe('defineProps w/ runtime declaration', () => {
   // runtime declaration
   const props = defineProps({
index 7915fa3fd49cc5215ebfbadcd3c6282d959fb7b7..90bde3aebd25bc5525098df4573102be86ffecde 100644 (file)
@@ -328,7 +328,9 @@ type PropsWithDefaults<
   Defaults extends InferDefaults<T>,
   BKeys extends keyof T,
 > = Readonly<MappedOmit<T, keyof Defaults>> & {
-  readonly [K in keyof Defaults]-?: K extends keyof T
+  readonly [K in keyof Defaults as K extends keyof T
+    ? K
+    : never]-?: K extends keyof T
     ? Defaults[K] extends undefined
       ? IfAny<Defaults[K], NotUndefined<T[K]>, T[K]>
       : NotUndefined<T[K]>