Skip to content

Commit b546282

Browse files
authored
fix(sfc/types): allow use default factory for primitive types in withDefaults (#5939)
fix #5938
1 parent dddbd96 commit b546282

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

packages/runtime-core/src/apiSetupHelpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ type InferDefault<P, T> = T extends
139139
| boolean
140140
| symbol
141141
| Function
142-
? T
142+
? T | ((props: P) => T)
143143
: (props: P) => T
144144

145145
type PropsWithDefaults<Base, Defaults> = Base & {

test-dts/setupHelpers.test-d.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ describe('defineProps w/ type declaration + withDefaults', () => {
2828
obj?: { x: number }
2929
fn?: (e: string) => void
3030
x?: string
31+
genStr?: string
3132
}>(),
3233
{
3334
number: 123,
3435
arr: () => [],
3536
obj: () => ({ x: 123 }),
36-
fn: () => {}
37+
fn: () => {},
38+
genStr: () => ''
3739
}
3840
)
3941

@@ -43,6 +45,7 @@ describe('defineProps w/ type declaration + withDefaults', () => {
4345
res.fn('hi')
4446
// @ts-expect-error
4547
res.x.slice()
48+
res.genStr.slice()
4649
})
4750

4851
describe('defineProps w/ union type declaration + withDefaults', () => {
@@ -51,11 +54,13 @@ describe('defineProps w/ union type declaration + withDefaults', () => {
5154
union1?: number | number[] | { x: number }
5255
union2?: number | number[] | { x: number }
5356
union3?: number | number[] | { x: number }
57+
union4?: number | number[] | { x: number }
5458
}>(),
5559
{
5660
union1: 123,
5761
union2: () => [123],
58-
union3: () => ({ x: 123 })
62+
union3: () => ({ x: 123 }),
63+
union4: () => 123,
5964
}
6065
)
6166
})

0 commit comments

Comments
 (0)