v3-component-public-instance.d.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import { ExtractDefaultPropTypes, ExtractPropTypes } from './v3-component-props'
  2. import {
  3. DebuggerEvent,
  4. nextTick,
  5. ShallowUnwrapRef,
  6. UnwrapNestedRefs,
  7. WatchOptions,
  8. WatchStopHandle
  9. } from './v3-generated'
  10. import { Data, UnionToIntersection } from './common'
  11. import { VueConstructor } from './vue'
  12. import {
  13. ComputedOptions,
  14. MethodOptions,
  15. ExtractComputedReturns,
  16. ComponentOptionsMixin,
  17. ComponentOptionsBase
  18. } from './v3-component-options'
  19. import { EmitFn, EmitsOptions, Slots } from './v3-setup-context'
  20. import { VNode } from './vnode'
  21. /**
  22. * Custom properties added to component instances in any way and can be accessed through `this`
  23. *
  24. * @example
  25. * ```ts
  26. * import { Router } from 'vue-router'
  27. *
  28. * declare module 'vue' {
  29. * interface ComponentCustomProperties {
  30. * $router: Router
  31. * }
  32. * }
  33. * ```
  34. */
  35. export interface ComponentCustomProperties {}
  36. export type ComponentInstance = InstanceType<VueConstructor>
  37. export type OptionTypesKeys = 'P' | 'B' | 'D' | 'C' | 'M' | 'Defaults'
  38. export type OptionTypesType<
  39. P = {},
  40. B = {},
  41. D = {},
  42. C extends ComputedOptions = {},
  43. M extends MethodOptions = {},
  44. Defaults = {}
  45. > = {
  46. P: P
  47. B: B
  48. D: D
  49. C: C
  50. M: M
  51. Defaults: Defaults
  52. }
  53. type IsDefaultMixinComponent<T> = T extends ComponentOptionsMixin
  54. ? ComponentOptionsMixin extends T
  55. ? true
  56. : false
  57. : false
  58. type MixinToOptionTypes<T> = T extends ComponentOptionsBase<
  59. infer P,
  60. infer B,
  61. infer D,
  62. infer C,
  63. infer M,
  64. infer Mixin,
  65. infer Extends,
  66. any,
  67. any,
  68. infer Defaults
  69. >
  70. ? OptionTypesType<P & {}, B & {}, D & {}, C & {}, M & {}, Defaults & {}> &
  71. IntersectionMixin<Mixin> &
  72. IntersectionMixin<Extends>
  73. : never
  74. // ExtractMixin(map type) is used to resolve circularly references
  75. type ExtractMixin<T> = {
  76. Mixin: MixinToOptionTypes<T>
  77. }[T extends ComponentOptionsMixin ? 'Mixin' : never]
  78. type IntersectionMixin<T> = IsDefaultMixinComponent<T> extends true
  79. ? OptionTypesType<{}, {}, {}, {}, {}, {}>
  80. : UnionToIntersection<ExtractMixin<T>>
  81. type UnwrapMixinsType<
  82. T,
  83. Type extends OptionTypesKeys
  84. > = T extends OptionTypesType ? T[Type] : never
  85. type EnsureNonVoid<T> = T extends void ? {} : T
  86. export type CreateComponentPublicInstance<
  87. P = {},
  88. B = {},
  89. D = {},
  90. C extends ComputedOptions = {},
  91. M extends MethodOptions = {},
  92. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  93. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  94. E extends EmitsOptions = {},
  95. PublicProps = P,
  96. Defaults = {},
  97. MakeDefaultsOptional extends boolean = false,
  98. PublicMixin = IntersectionMixin<Mixin> & IntersectionMixin<Extends>,
  99. PublicP = UnwrapMixinsType<PublicMixin, 'P'> & EnsureNonVoid<P>,
  100. PublicB = UnwrapMixinsType<PublicMixin, 'B'> & EnsureNonVoid<B>,
  101. PublicD = UnwrapMixinsType<PublicMixin, 'D'> & EnsureNonVoid<D>,
  102. PublicC extends ComputedOptions = UnwrapMixinsType<PublicMixin, 'C'> &
  103. EnsureNonVoid<C>,
  104. PublicM extends MethodOptions = UnwrapMixinsType<PublicMixin, 'M'> &
  105. EnsureNonVoid<M>,
  106. PublicDefaults = UnwrapMixinsType<PublicMixin, 'Defaults'> &
  107. EnsureNonVoid<Defaults>
  108. > = ComponentPublicInstance<
  109. PublicP,
  110. PublicB,
  111. PublicD,
  112. PublicC,
  113. PublicM,
  114. E,
  115. PublicProps,
  116. PublicDefaults,
  117. MakeDefaultsOptional
  118. >
  119. // public properties exposed on the proxy, which is used as the render context
  120. // in templates (as `this` in the render option)
  121. export type ComponentPublicInstance<
  122. P = {}, // props type extracted from props option
  123. B = {}, // raw bindings returned from setup()
  124. D = {}, // return from data()
  125. C extends ComputedOptions = {},
  126. M extends MethodOptions = {},
  127. E extends EmitsOptions = {},
  128. PublicProps = P,
  129. Defaults = {},
  130. MakeDefaultsOptional extends boolean = false,
  131. Options = ComponentOptionsBase<
  132. any,
  133. any,
  134. any,
  135. any,
  136. any,
  137. any,
  138. any,
  139. any,
  140. any,
  141. any
  142. >
  143. > = {
  144. // $: ComponentInternalInstance
  145. $data: D
  146. $props: Readonly<
  147. MakeDefaultsOptional extends true
  148. ? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults>
  149. : P & PublicProps
  150. >
  151. $attrs: Data
  152. $refs: Data
  153. $slots: Record<string, VNode[] | undefined>
  154. $scopedSlots: Slots
  155. $root: ComponentPublicInstance | null
  156. $parent: ComponentPublicInstance | null
  157. $emit: EmitFn<E>
  158. $el: any
  159. $options: Options & MergedComponentOptionsOverride
  160. $forceUpdate: () => void
  161. $nextTick: typeof nextTick
  162. $watch(
  163. source: string | Function,
  164. cb: Function,
  165. options?: WatchOptions
  166. ): WatchStopHandle
  167. } & Readonly<P> &
  168. ShallowUnwrapRef<B> &
  169. UnwrapNestedRefs<D> &
  170. ExtractComputedReturns<C> &
  171. M &
  172. ComponentCustomProperties
  173. type MergedHook<T = () => void> = T | T[]
  174. export type MergedComponentOptionsOverride = {
  175. beforeCreate?: MergedHook
  176. created?: MergedHook
  177. beforeMount?: MergedHook
  178. mounted?: MergedHook
  179. beforeUpdate?: MergedHook
  180. updated?: MergedHook
  181. activated?: MergedHook
  182. deactivated?: MergedHook
  183. /** @deprecated use `beforeUnmount` instead */
  184. beforeDestroy?: MergedHook
  185. beforeUnmount?: MergedHook
  186. /** @deprecated use `unmounted` instead */
  187. destroyed?: MergedHook
  188. unmounted?: MergedHook
  189. renderTracked?: MergedHook<DebuggerHook>
  190. renderTriggered?: MergedHook<DebuggerHook>
  191. errorCaptured?: MergedHook<ErrorCapturedHook>
  192. }
  193. export type DebuggerHook = (e: DebuggerEvent) => void
  194. export type ErrorCapturedHook<TError = unknown> = (
  195. err: TError,
  196. instance: ComponentPublicInstance | null,
  197. info: string
  198. ) => boolean | void
  199. export type ComponentPublicInstanceConstructor<
  200. T extends ComponentPublicInstance<
  201. Props,
  202. RawBindings,
  203. D,
  204. C,
  205. M
  206. > = ComponentPublicInstance<any, any, any>,
  207. Props = any,
  208. RawBindings = any,
  209. D = any,
  210. C extends ComputedOptions = ComputedOptions,
  211. M extends MethodOptions = MethodOptions
  212. > = {
  213. new (...args: any[]): T
  214. }