types(PropType): Allow undefined function to be used on PropType (#4405)

This commit is contained in:
Carlos Rodrigues 2021-08-24 00:04:03 +01:00 committed by GitHub
parent 98263821f8
commit afd49b3e88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 1 deletions

View File

@ -66,7 +66,9 @@ type PropConstructor<T = any> =
| { (): T } | { (): T }
| PropMethod<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 ? { new (): TConstructor; (): T; readonly prototype: TConstructor } // Create Function like constructor
: never : never

View File

@ -21,6 +21,7 @@ describe('with object props', () => {
b: string b: string
e?: Function e?: Function
h: boolean h: boolean
j: undefined | (() => string | undefined)
bb: string bb: string
bbb: string bbb: string
bbbb: string | undefined bbbb: string | undefined
@ -55,6 +56,7 @@ describe('with object props', () => {
}, },
e: Function, e: Function,
h: Boolean, h: Boolean,
j: Function as PropType<undefined | (() => string | undefined)>,
// default value should infer type and make it non-void // default value should infer type and make it non-void
bb: { bb: {
default: 'hello' default: 'hello'
@ -137,6 +139,7 @@ describe('with object props', () => {
expectType<ExpectedProps['b']>(props.b) expectType<ExpectedProps['b']>(props.b)
expectType<ExpectedProps['e']>(props.e) expectType<ExpectedProps['e']>(props.e)
expectType<ExpectedProps['h']>(props.h) expectType<ExpectedProps['h']>(props.h)
expectType<ExpectedProps['j']>(props.j)
expectType<ExpectedProps['bb']>(props.bb) expectType<ExpectedProps['bb']>(props.bb)
expectType<ExpectedProps['bbb']>(props.bbb) expectType<ExpectedProps['bbb']>(props.bbb)
expectType<ExpectedProps['bbbb']>(props.bbbb) expectType<ExpectedProps['bbbb']>(props.bbbb)