|
| 1 | +import { Data } from './common' |
| 2 | +import { ComponentPropsOptions, ExtractPropTypes } from './componentProps' |
| 3 | +import { VNode } from 'vue' |
| 4 | +import { ComponentInstance, ComponentRenderProxy } from './componentProxy' |
| 5 | + |
| 6 | +import { ComponentOptions as Vue2ComponentOptions } from 'vue' |
| 7 | + |
| 8 | +export interface SetupContext { |
| 9 | + readonly attrs: Record<string, string> |
| 10 | + readonly slots: { [key: string]: (...args: any[]) => VNode[] } |
| 11 | + readonly parent: ComponentInstance | null |
| 12 | + readonly root: ComponentInstance |
| 13 | + readonly listeners: { [key: string]: Function } |
| 14 | + |
| 15 | + emit(event: string, ...args: any[]): void |
| 16 | +} |
| 17 | + |
| 18 | +export type ComputedGetter<T> = (ctx?: any) => T |
| 19 | +export type ComputedSetter<T> = (v: T) => void |
| 20 | + |
| 21 | +export interface WritableComputedOptions<T> { |
| 22 | + get: ComputedGetter<T> |
| 23 | + set: ComputedSetter<T> |
| 24 | +} |
| 25 | + |
| 26 | +export type ComputedOptions = Record< |
| 27 | + string, |
| 28 | + ComputedGetter<any> | WritableComputedOptions<any> |
| 29 | +> |
| 30 | + |
| 31 | +export interface MethodOptions { |
| 32 | + [key: string]: Function |
| 33 | +} |
| 34 | + |
| 35 | +export type SetupFunction<Props, RawBindings> = ( |
| 36 | + this: void, |
| 37 | + props: Props, |
| 38 | + ctx: SetupContext |
| 39 | +) => RawBindings | (() => VNode | null) |
| 40 | + |
| 41 | +interface ComponentOptionsBase< |
| 42 | + Props, |
| 43 | + D = Data, |
| 44 | + C extends ComputedOptions = {}, |
| 45 | + M extends MethodOptions = {} |
| 46 | +> |
| 47 | + extends Omit< |
| 48 | + Vue2ComponentOptions<Vue, D, M, C, Props>, |
| 49 | + 'data' | 'computed' | 'method' | 'setup' | 'props' |
| 50 | + > { |
| 51 | + data?: (this: Props, vm: Props) => D |
| 52 | + computed?: C |
| 53 | + methods?: M |
| 54 | +} |
| 55 | + |
| 56 | +export type ExtractComputedReturns<T extends any> = { |
| 57 | + [key in keyof T]: T[key] extends { get: (...args: any[]) => infer TReturn } |
| 58 | + ? TReturn |
| 59 | + : T[key] extends (...args: any[]) => infer TReturn |
| 60 | + ? TReturn |
| 61 | + : never |
| 62 | +} |
| 63 | + |
| 64 | +export type ComponentOptionsWithProps< |
| 65 | + PropsOptions = ComponentPropsOptions, |
| 66 | + RawBindings = Data, |
| 67 | + D = Data, |
| 68 | + C extends ComputedOptions = {}, |
| 69 | + M extends MethodOptions = {}, |
| 70 | + Props = ExtractPropTypes<PropsOptions> |
| 71 | +> = ComponentOptionsBase<Props, D, C, M> & { |
| 72 | + props?: PropsOptions |
| 73 | + setup?: SetupFunction<Props, RawBindings> |
| 74 | +} & ThisType<ComponentRenderProxy<Props, RawBindings, D, C, M>> |
| 75 | + |
| 76 | +export type ComponentOptionsWithArrayProps< |
| 77 | + PropNames extends string = string, |
| 78 | + RawBindings = Data, |
| 79 | + D = Data, |
| 80 | + C extends ComputedOptions = {}, |
| 81 | + M extends MethodOptions = {}, |
| 82 | + Props = Readonly<{ [key in PropNames]?: any }> |
| 83 | +> = ComponentOptionsBase<Props, D, C, M> & { |
| 84 | + props?: PropNames[] |
| 85 | + setup?: SetupFunction<Props, RawBindings> |
| 86 | +} & ThisType<ComponentRenderProxy<Props, RawBindings, D, C, M>> |
| 87 | + |
| 88 | +export type ComponentOptionsWithoutProps< |
| 89 | + Props = unknown, |
| 90 | + RawBindings = Data, |
| 91 | + D = Data, |
| 92 | + C extends ComputedOptions = {}, |
| 93 | + M extends MethodOptions = {} |
| 94 | +> = ComponentOptionsBase<Props, D, C, M> & { |
| 95 | + props?: undefined |
| 96 | + setup?: SetupFunction<Props, RawBindings> |
| 97 | +} & ThisType<ComponentRenderProxy<Props, RawBindings, D, C, M>> |
| 98 | + |
| 99 | +export type WithLegacyAPI<T, D, C, M, Props> = T & |
| 100 | + Omit<Vue2ComponentOptions<Vue, D, M, C, Props>, keyof T> |
0 commit comments