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

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