Skip to content

Commit afd49b3

Browse files
authored
types(PropType): Allow undefined function to be used on PropType (#4405)
1 parent 9826382 commit afd49b3

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

packages/runtime-core/src/componentProps.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ type PropConstructor<T = any> =
6666
| { (): T }
6767
| PropMethod<T>
6868

69-
type PropMethod<T, TConstructor = any> = [T] extends [(...args: any) => any] // if is function with args
69+
type PropMethod<T, TConstructor = any> = [T] extends [
70+
((...args: any) => any) | undefined
71+
] // if is function with args, allowing non-required functions
7072
? { new (): TConstructor; (): T; readonly prototype: TConstructor } // Create Function like constructor
7173
: never
7274

test-dts/defineComponent.test-d.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('with object props', () => {
2121
b: string
2222
e?: Function
2323
h: boolean
24+
j: undefined | (() => string | undefined)
2425
bb: string
2526
bbb: string
2627
bbbb: string | undefined
@@ -55,6 +56,7 @@ describe('with object props', () => {
5556
},
5657
e: Function,
5758
h: Boolean,
59+
j: Function as PropType<undefined | (() => string | undefined)>,
5860
// default value should infer type and make it non-void
5961
bb: {
6062
default: 'hello'
@@ -137,6 +139,7 @@ describe('with object props', () => {
137139
expectType<ExpectedProps['b']>(props.b)
138140
expectType<ExpectedProps['e']>(props.e)
139141
expectType<ExpectedProps['h']>(props.h)
142+
expectType<ExpectedProps['j']>(props.j)
140143
expectType<ExpectedProps['bb']>(props.bb)
141144
expectType<ExpectedProps['bbb']>(props.bbb)
142145
expectType<ExpectedProps['bbbb']>(props.bbbb)

0 commit comments

Comments
 (0)