]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(sfc/types): allow use default factory for primitive types in `withDefaults` ...
authorAlex Kozack <cawa-93@users.noreply.github.com>
Mon, 23 May 2022 00:28:39 +0000 (03:28 +0300)
committerGitHub <noreply@github.com>
Mon, 23 May 2022 00:28:39 +0000 (20:28 -0400)
fix #5938

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

index 155c8cd19cc8ab8349810e07616b7deea108e95d..a8b7fcdef31f8224b88dbfc28205ea0211489884 100644 (file)
@@ -139,7 +139,7 @@ type InferDefault<P, T> = T extends
   | boolean
   | symbol
   | Function
-  ? T
+  ? T | ((props: P) => T)
   : (props: P) => T
 
 type PropsWithDefaults<Base, Defaults> = Base & {
index 155ba337dcfa0a7c1f0267c3aa37c2ca089c7e7d..6b9c67b28978e6685a9ff310ca47e936910a6163 100644 (file)
@@ -28,12 +28,14 @@ describe('defineProps w/ type declaration + withDefaults', () => {
       obj?: { x: number }
       fn?: (e: string) => void
       x?: string
+      genStr?: string
     }>(),
     {
       number: 123,
       arr: () => [],
       obj: () => ({ x: 123 }),
-      fn: () => {}
+      fn: () => {},
+      genStr: () => ''
     }
   )
 
@@ -43,6 +45,7 @@ describe('defineProps w/ type declaration + withDefaults', () => {
   res.fn('hi')
   // @ts-expect-error
   res.x.slice()
+  res.genStr.slice()
 })
 
 describe('defineProps w/ union type declaration + withDefaults', () => {
@@ -51,11 +54,13 @@ describe('defineProps w/ union type declaration + withDefaults', () => {
       union1?: number | number[] | { x: number }
       union2?: number | number[] | { x: number }
       union3?: number | number[] | { x: number }
+      union4?: number | number[] | { x: number }
     }>(),
     {
       union1: 123,
       union2: () => [123],
-      union3: () => ({ x: 123 })
+      union3: () => ({ x: 123 }),
+      union4: () => 123,
     }
   )
 })