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

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