| { (): 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] // if is function with args
? { new (): TConstructor; (): T; readonly prototype: TConstructor } // Create Function like constructor
: never
: never
}[keyof T]
-type InferPropType<T> = T extends null
+type InferPropType<T> = [T] extends [null]
? any // null & true would fail to infer
- : T extends { type: null | true }
+ : [T] extends [{ type: null | true }]
? any // As TS issue https://github.com/Microsoft/TypeScript/issues/14829 // somehow `ObjectConstructor` when inferred from { (): T } becomes `any` // `BooleanConstructor` when inferred from PropConstructor(with PropMethod) becomes `Boolean`
- : T extends ObjectConstructor | { type: ObjectConstructor }
+ : [T] extends [ObjectConstructor | { type: ObjectConstructor }]
? Record<string, any>
- : T extends BooleanConstructor | { type: BooleanConstructor }
+ : [T] extends [BooleanConstructor | { type: BooleanConstructor }]
? boolean
- : T extends DateConstructor | { type: DateConstructor }
+ : [T] extends [DateConstructor | { type: DateConstructor }]
? Date
- : T extends Prop<infer V, infer D> ? (unknown extends V ? D : V) : T
+ : [T] extends [Prop<infer V, infer D>]
+ ? (unknown extends V ? D : V)
+ : T
export type ExtractPropTypes<O> = O extends object
? { [K in RequiredKeys<O>]: InferPropType<O[K]> } &
ComponentPublicInstance,
ComponentOptions,
SetupContext,
+ IsUnion,
h
} from './index'
hhh: boolean
ggg: 'foo' | 'bar'
ffff: (a: number, b: string) => { a: boolean }
+ iii?: (() => string) | (() => number)
+ jjj: ((arg1: string) => string) | ((arg1: string, arg2: string) => string)
+ kkk?: any
validated?: string
date?: Date
}
type: Function as PropType<(a: number, b: string) => { a: boolean }>,
default: (a: number, b: string) => ({ a: a > +b })
},
+ // union + function with different return types
+ iii: Function as PropType<(() => string) | (() => number)>,
+ // union + function with different args & same return type
+ jjj: {
+ type: Function as PropType<
+ ((arg1: string) => string) | ((arg1: string, arg2: string) => string)
+ >,
+ required: true
+ },
+ kkk: null,
validated: {
type: String,
// validator requires explicit annotation
expectType<ExpectedProps['hhh']>(props.hhh)
expectType<ExpectedProps['ggg']>(props.ggg)
expectType<ExpectedProps['ffff']>(props.ffff)
+ if (typeof props.iii !== 'function') {
+ expectType<undefined>(props.iii)
+ }
+ expectType<ExpectedProps['iii']>(props.iii)
+ expectType<IsUnion<typeof props.jjj>>(true)
+ expectType<ExpectedProps['jjj']>(props.jjj)
+ expectType<ExpectedProps['kkk']>(props.kkk)
expectType<ExpectedProps['validated']>(props.validated)
expectType<ExpectedProps['date']>(props.date)
expectType<ExpectedProps['fff']>(props.fff)
expectType<ExpectedProps['hhh']>(props.hhh)
expectType<ExpectedProps['ggg']>(props.ggg)
+ if (typeof props.iii !== 'function') {
+ expectType<undefined>(props.iii)
+ }
+ expectType<ExpectedProps['iii']>(props.iii)
+ expectType<IsUnion<typeof props.jjj>>(true)
+ expectType<ExpectedProps['jjj']>(props.jjj)
+ expectType<ExpectedProps['kkk']>(props.kkk)
// @ts-expect-error props should be readonly
expectError((props.a = 1))
expectType<ExpectedProps['fff']>(this.fff)
expectType<ExpectedProps['hhh']>(this.hhh)
expectType<ExpectedProps['ggg']>(this.ggg)
+ if (typeof this.iii !== 'function') {
+ expectType<undefined>(this.iii)
+ }
+ expectType<ExpectedProps['iii']>(this.iii)
+ const { jjj } = this
+ expectType<IsUnion<typeof jjj>>(true)
+ expectType<ExpectedProps['jjj']>(this.jjj)
+ expectType<ExpectedProps['kkk']>(this.kkk)
// @ts-expect-error props on `this` should be readonly
expectError((this.a = 1))
fff={(a, b) => ({ a: a > +b })}
hhh={false}
ggg="foo"
+ jjj={() => ''}
// should allow class/style as attrs
class="bar"
style={{ color: 'red' }}
eee={() => ({ a: 'eee' })}
fff={(a, b) => ({ a: a > +b })}
hhh={false}
+ jjj={() => ''}
/>
)