]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types(PropType): Allow `undefined` function to be used on `PropType` (#4405)
authorCarlos Rodrigues <david-181@hotmail.com>
Mon, 23 Aug 2021 23:04:03 +0000 (00:04 +0100)
committerGitHub <noreply@github.com>
Mon, 23 Aug 2021 23:04:03 +0000 (19:04 -0400)
packages/runtime-core/src/componentProps.ts
test-dts/defineComponent.test-d.tsx

index 7d7f3a38728c1e9e0e6b046cedddeeb4f78fa760..7218d22288b8f8939c4e9f81fea0c313ac7cdded 100644 (file)
@@ -66,7 +66,9 @@ type PropConstructor<T = any> =
   | { (): T }
   | PropMethod<T>
 
-type PropMethod<T, TConstructor = any> = [T] extends [(...args: any) => any] // if is function with args
+type PropMethod<T, TConstructor = any> = [T] extends [
+  ((...args: any) => any) | undefined
+] // if is function with args, allowing non-required functions
   ? { new (): TConstructor; (): T; readonly prototype: TConstructor } // Create Function like constructor
   : never
 
index 9569b585f501e49fcbd8103418595696018d965d..4766bddb4b3b43d89c0fef2f48c58fb76fc2237a 100644 (file)
@@ -21,6 +21,7 @@ describe('with object props', () => {
     b: string
     e?: Function
     h: boolean
+    j: undefined | (() => string | undefined)
     bb: string
     bbb: string
     bbbb: string | undefined
@@ -55,6 +56,7 @@ describe('with object props', () => {
       },
       e: Function,
       h: Boolean,
+      j: Function as PropType<undefined | (() => string | undefined)>,
       // default value should infer type and make it non-void
       bb: {
         default: 'hello'
@@ -137,6 +139,7 @@ describe('with object props', () => {
       expectType<ExpectedProps['b']>(props.b)
       expectType<ExpectedProps['e']>(props.e)
       expectType<ExpectedProps['h']>(props.h)
+      expectType<ExpectedProps['j']>(props.j)
       expectType<ExpectedProps['bb']>(props.bb)
       expectType<ExpectedProps['bbb']>(props.bbb)
       expectType<ExpectedProps['bbbb']>(props.bbbb)