v3-component-proxy.d.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { ExtractDefaultPropTypes, ExtractPropTypes } from './v3-component-props'
  2. import {
  3. nextTick,
  4. ShallowUnwrapRef,
  5. UnwrapNestedRefs,
  6. WatchOptions,
  7. WatchStopHandle
  8. } from './v3-generated'
  9. import { Data } from './common'
  10. import { Vue, VueConstructor } from './vue'
  11. import { ComponentOptions as Vue2ComponentOptions } from './options'
  12. import {
  13. ComputedOptions,
  14. MethodOptions,
  15. ExtractComputedReturns
  16. } from './v3-component-options'
  17. import {
  18. ComponentRenderEmitFn,
  19. EmitFn,
  20. EmitsOptions,
  21. ObjectEmitsOptions,
  22. Slots
  23. } from './v3-setup-context'
  24. type EmitsToProps<T extends EmitsOptions> = T extends string[]
  25. ? {
  26. [K in string & `on${Capitalize<T[number]>}`]?: (...args: any[]) => any
  27. }
  28. : T extends ObjectEmitsOptions
  29. ? {
  30. [K in string &
  31. `on${Capitalize<string & keyof T>}`]?: K extends `on${infer C}`
  32. ? T[Uncapitalize<C>] extends null
  33. ? (...args: any[]) => any
  34. : (
  35. ...args: T[Uncapitalize<C>] extends (...args: infer P) => any
  36. ? P
  37. : never
  38. ) => any
  39. : never
  40. }
  41. : {}
  42. export type ComponentInstance = InstanceType<VueConstructor>
  43. // public properties exposed on the proxy, which is used as the render context
  44. // in templates (as `this` in the render option)
  45. export type ComponentRenderProxy<
  46. P = {}, // props type extracted from props option
  47. B = {}, // raw bindings returned from setup()
  48. D = {}, // return from data()
  49. C extends ComputedOptions = {},
  50. M extends MethodOptions = {},
  51. Mixin = {},
  52. Extends = {},
  53. Emits extends EmitsOptions = {},
  54. PublicProps = P,
  55. Defaults = {},
  56. MakeDefaultsOptional extends boolean = false
  57. > = {
  58. $data: D
  59. $props: Readonly<
  60. MakeDefaultsOptional extends true
  61. ? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults>
  62. : P & PublicProps
  63. >
  64. $attrs: Record<string, string>
  65. $emit: ComponentRenderEmitFn<
  66. Emits,
  67. keyof Emits,
  68. ComponentRenderProxy<
  69. P,
  70. B,
  71. D,
  72. C,
  73. M,
  74. Mixin,
  75. Extends,
  76. Emits,
  77. PublicProps,
  78. Defaults,
  79. MakeDefaultsOptional
  80. >
  81. >
  82. } & Readonly<P> &
  83. ShallowUnwrapRef<B> &
  84. D &
  85. M &
  86. ExtractComputedReturns<C> &
  87. Omit<Vue, '$data' | '$props' | '$attrs' | '$emit'>
  88. // for Vetur and TSX support
  89. type VueConstructorProxy<
  90. PropsOptions,
  91. RawBindings,
  92. Data,
  93. Computed extends ComputedOptions,
  94. Methods extends MethodOptions,
  95. Mixin = {},
  96. Extends = {},
  97. Emits extends EmitsOptions = {},
  98. Props = ExtractPropTypes<PropsOptions> &
  99. ({} extends Emits ? {} : EmitsToProps<Emits>)
  100. > = Omit<VueConstructor, never> & {
  101. new (...args: any[]): ComponentRenderProxy<
  102. Props,
  103. ShallowUnwrapRef<RawBindings>,
  104. Data,
  105. Computed,
  106. Methods,
  107. Mixin,
  108. Extends,
  109. Emits,
  110. Props,
  111. ExtractDefaultPropTypes<PropsOptions>,
  112. true
  113. >
  114. }
  115. type DefaultData<V> = object | ((this: V) => object)
  116. type DefaultMethods<V> = { [key: string]: (this: V, ...args: any[]) => any }
  117. type DefaultComputed = { [key: string]: any }
  118. export type VueProxy<
  119. PropsOptions,
  120. RawBindings,
  121. Data = DefaultData<Vue>,
  122. Computed extends ComputedOptions = DefaultComputed,
  123. Methods extends MethodOptions = DefaultMethods<Vue>,
  124. Mixin = {},
  125. Extends = {},
  126. Emits extends EmitsOptions = {}
  127. > = Vue2ComponentOptions<
  128. Vue,
  129. ShallowUnwrapRef<RawBindings> & Data,
  130. Methods,
  131. Computed,
  132. PropsOptions,
  133. ExtractPropTypes<PropsOptions>
  134. > &
  135. VueConstructorProxy<
  136. PropsOptions,
  137. RawBindings,
  138. Data,
  139. Computed,
  140. Methods,
  141. Mixin,
  142. Extends,
  143. Emits
  144. >
  145. // public properties exposed on the proxy, which is used as the render context
  146. // in templates (as `this` in the render option)
  147. export type ComponentPublicInstance<
  148. P = {}, // props type extracted from props option
  149. B = {}, // raw bindings returned from setup()
  150. D = {}, // return from data()
  151. C extends ComputedOptions = {},
  152. M extends MethodOptions = {},
  153. E extends EmitsOptions = {},
  154. PublicProps = P,
  155. Defaults = {},
  156. MakeDefaultsOptional extends boolean = false
  157. > = {
  158. $data: D
  159. $props: MakeDefaultsOptional extends true
  160. ? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults>
  161. : P & PublicProps
  162. $attrs: Data
  163. $refs: Data
  164. $slots: Slots
  165. $root: ComponentPublicInstance | null
  166. $parent: ComponentPublicInstance | null
  167. $emit: EmitFn<E>
  168. $el: any
  169. // $options: Options & MergedComponentOptionsOverride
  170. $forceUpdate: () => void
  171. $nextTick: typeof nextTick
  172. $watch(
  173. source: string | Function,
  174. cb: Function,
  175. options?: WatchOptions
  176. ): WatchStopHandle
  177. } & P &
  178. ShallowUnwrapRef<B> &
  179. UnwrapNestedRefs<D> &
  180. ExtractComputedReturns<C> &
  181. M