Skip to content

Commit c2913d5

Browse files
authored
fix(types): handling PropType<Function> with default value (#1896)
fix #1891
1 parent 02dcc68 commit c2913d5

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

packages/runtime-core/src/componentProps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type DefaultFactory<T> = (props: Data) => T | null | undefined
4747
interface PropOptions<T = any, D = T> {
4848
type?: PropType<T> | true | null
4949
required?: boolean
50-
default?: D | DefaultFactory<D> | null | undefined
50+
default?: D | DefaultFactory<D> | null | undefined | object
5151
validator?(value: unknown): boolean
5252
}
5353

test-dts/defineComponent.test-d.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('with object props', () => {
3030
fff: (a: number, b: string) => { a: boolean }
3131
hhh: boolean
3232
ggg: 'foo' | 'bar'
33+
ffff: (a: number, b: string) => { a: boolean }
3334
validated?: string
3435
}
3536

@@ -90,6 +91,11 @@ describe('with object props', () => {
9091
type: String as PropType<'foo' | 'bar'>,
9192
default: 'foo'
9293
},
94+
// default + function
95+
ffff: {
96+
type: Function as PropType<(a: number, b: string) => { a: boolean }>,
97+
default: (a: number, b: string) => ({ a: true })
98+
},
9399
validated: {
94100
type: String,
95101
// validator requires explicit annotation
@@ -113,6 +119,7 @@ describe('with object props', () => {
113119
expectType<ExpectedProps['fff']>(props.fff)
114120
expectType<ExpectedProps['hhh']>(props.hhh)
115121
expectType<ExpectedProps['ggg']>(props.ggg)
122+
expectType<ExpectedProps['ffff']>(props.ffff)
116123
expectType<ExpectedProps['validated']>(props.validated)
117124

118125
// @ts-expect-error props should be readonly

0 commit comments

Comments
 (0)