v3-setup-helpers.d.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { EmitFn, EmitsOptions } from './v3-setup-context'
  2. import {
  3. ComponentObjectPropsOptions,
  4. ExtractPropTypes
  5. } from './v3-component-props'
  6. /**
  7. * Vue `<script setup>` compiler macro for declaring component props. The
  8. * expected argument is the same as the component `props` option.
  9. *
  10. * Example runtime declaration:
  11. * ```js
  12. * // using Array syntax
  13. * const props = defineProps(['foo', 'bar'])
  14. * // using Object syntax
  15. * const props = defineProps({
  16. * foo: String,
  17. * bar: {
  18. * type: Number,
  19. * required: true
  20. * }
  21. * })
  22. * ```
  23. *
  24. * Equivalent type-based declaration:
  25. * ```ts
  26. * // will be compiled into equivalent runtime declarations
  27. * const props = defineProps<{
  28. * foo?: string
  29. * bar: number
  30. * }>()
  31. * ```
  32. *
  33. * This is only usable inside `<script setup>`, is compiled away in the
  34. * output and should **not** be actually called at runtime.
  35. */
  36. // overload 1: runtime props w/ array
  37. export function defineProps<PropNames extends string = string>(
  38. props: PropNames[]
  39. ): Readonly<{ [key in PropNames]?: any }>
  40. // overload 2: runtime props w/ object
  41. export function defineProps<
  42. PP extends ComponentObjectPropsOptions = ComponentObjectPropsOptions
  43. >(props: PP): Readonly<ExtractPropTypes<PP>>
  44. // overload 3: typed-based declaration
  45. export function defineProps<TypeProps>(): Readonly<TypeProps>
  46. /**
  47. * Vue `<script setup>` compiler macro for declaring a component's emitted
  48. * events. The expected argument is the same as the component `emits` option.
  49. *
  50. * Example runtime declaration:
  51. * ```js
  52. * const emit = defineEmits(['change', 'update'])
  53. * ```
  54. *
  55. * Example type-based declaration:
  56. * ```ts
  57. * const emit = defineEmits<{
  58. * (event: 'change'): void
  59. * (event: 'update', id: number): void
  60. * }>()
  61. *
  62. * emit('change')
  63. * emit('update', 1)
  64. * ```
  65. *
  66. * This is only usable inside `<script setup>`, is compiled away in the
  67. * output and should **not** be actually called at runtime.
  68. */
  69. // overload 1: runtime emits w/ array
  70. export function defineEmits<EE extends string = string>(
  71. emitOptions: EE[]
  72. ): EmitFn<EE[]>
  73. export function defineEmits<E extends EmitsOptions = EmitsOptions>(
  74. emitOptions: E
  75. ): EmitFn<E>
  76. export function defineEmits<TypeEmit>(): TypeEmit
  77. /**
  78. * Vue `<script setup>` compiler macro for declaring a component's exposed
  79. * instance properties when it is accessed by a parent component via template
  80. * refs.
  81. *
  82. * `<script setup>` components are closed by default - i.e. variables inside
  83. * the `<script setup>` scope is not exposed to parent unless explicitly exposed
  84. * via `defineExpose`.
  85. *
  86. * This is only usable inside `<script setup>`, is compiled away in the
  87. * output and should **not** be actually called at runtime.
  88. */
  89. export function defineExpose<
  90. Exposed extends Record<string, any> = Record<string, any>
  91. >(exposed?: Exposed): void
  92. type NotUndefined<T> = T extends undefined ? never : T
  93. type InferDefaults<T> = {
  94. [K in keyof T]?: InferDefault<T, NotUndefined<T[K]>>
  95. }
  96. type InferDefault<P, T> = T extends
  97. | null
  98. | number
  99. | string
  100. | boolean
  101. | symbol
  102. | Function
  103. ? T | ((props: P) => T)
  104. : (props: P) => T
  105. type PropsWithDefaults<Base, Defaults> = Base & {
  106. [K in keyof Defaults]: K extends keyof Base
  107. ? Defaults[K] extends undefined
  108. ? Base[K]
  109. : NotUndefined<Base[K]>
  110. : never
  111. }
  112. /**
  113. * Vue `<script setup>` compiler macro for providing props default values when
  114. * using type-based `defineProps` declaration.
  115. *
  116. * Example usage:
  117. * ```ts
  118. * withDefaults(defineProps<{
  119. * size?: number
  120. * labels?: string[]
  121. * }>(), {
  122. * size: 3,
  123. * labels: () => ['default label']
  124. * })
  125. * ```
  126. *
  127. * This is only usable inside `<script setup>`, is compiled away in the output
  128. * and should **not** be actually called at runtime.
  129. */
  130. export function withDefaults<Props, Defaults extends InferDefaults<Props>>(
  131. props: Props,
  132. defaults: Defaults
  133. ): PropsWithDefaults<Props, Defaults>
  134. // make them global
  135. type _defineProps = typeof defineProps
  136. type _defineEmits = typeof defineEmits
  137. type _defineExpose = typeof defineExpose
  138. type _withDefaults = typeof withDefaults
  139. declare global {
  140. const defineProps: _defineProps
  141. const defineEmits: _defineEmits
  142. const defineExpose: _defineExpose
  143. const withDefaults: _withDefaults
  144. }