setupHelpers.test-d.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import {
  2. defineProps,
  3. defineEmits,
  4. useAttrs,
  5. useSlots,
  6. withDefaults,
  7. Slots,
  8. defineSlots,
  9. VNode,
  10. Ref,
  11. defineModel
  12. } from 'vue'
  13. import { describe, expectType } from './utils'
  14. import { defineComponent } from 'vue'
  15. import { useModel } from 'vue'
  16. describe('defineProps w/ type declaration', () => {
  17. // type declaration
  18. const props = defineProps<{
  19. foo: string
  20. bool?: boolean
  21. boolAndUndefined: boolean | undefined
  22. }>()
  23. // explicitly declared type should be refined
  24. expectType<string>(props.foo)
  25. // @ts-expect-error
  26. props.bar
  27. expectType<boolean>(props.bool)
  28. expectType<boolean>(props.boolAndUndefined)
  29. })
  30. describe('defineProps w/ generics', () => {
  31. function test<T extends boolean>() {
  32. const props = defineProps<{ foo: T; bar: string; x?: boolean }>()
  33. expectType<T>(props.foo)
  34. expectType<string>(props.bar)
  35. expectType<boolean>(props.x)
  36. }
  37. test()
  38. })
  39. describe('defineProps w/ type declaration + withDefaults', () => {
  40. const res = withDefaults(
  41. defineProps<{
  42. number?: number
  43. arr?: string[]
  44. obj?: { x: number }
  45. fn?: (e: string) => void
  46. genStr?: string
  47. x?: string
  48. y?: string
  49. z?: string
  50. bool?: boolean
  51. boolAndUndefined: boolean | undefined
  52. }>(),
  53. {
  54. number: 123,
  55. arr: () => [],
  56. obj: () => ({ x: 123 }),
  57. fn: () => {},
  58. genStr: () => '',
  59. y: undefined,
  60. z: 'string'
  61. }
  62. )
  63. res.number + 1
  64. res.arr.push('hi')
  65. res.obj.x
  66. res.fn('hi')
  67. res.genStr.slice()
  68. // @ts-expect-error
  69. res.x.slice()
  70. // @ts-expect-error
  71. res.y.slice()
  72. expectType<string | undefined>(res.x)
  73. expectType<string | undefined>(res.y)
  74. expectType<string>(res.z)
  75. expectType<boolean>(res.bool)
  76. expectType<boolean>(res.boolAndUndefined)
  77. })
  78. describe('defineProps w/ union type declaration + withDefaults', () => {
  79. withDefaults(
  80. defineProps<{
  81. union1?: number | number[] | { x: number }
  82. union2?: number | number[] | { x: number }
  83. union3?: number | number[] | { x: number }
  84. union4?: number | number[] | { x: number }
  85. }>(),
  86. {
  87. union1: 123,
  88. union2: () => [123],
  89. union3: () => ({ x: 123 }),
  90. union4: () => 123
  91. }
  92. )
  93. })
  94. describe('defineProps w/ runtime declaration', () => {
  95. // runtime declaration
  96. const props = defineProps({
  97. foo: String,
  98. bar: {
  99. type: Number,
  100. default: 1
  101. },
  102. baz: {
  103. type: Array,
  104. required: true
  105. }
  106. })
  107. expectType<{
  108. foo?: string
  109. bar: number
  110. baz: unknown[]
  111. }>(props)
  112. props.foo && props.foo + 'bar'
  113. props.bar + 1
  114. // @ts-expect-error should be readonly
  115. props.bar++
  116. props.baz.push(1)
  117. const props2 = defineProps(['foo', 'bar'])
  118. props2.foo + props2.bar
  119. // @ts-expect-error
  120. props2.baz
  121. })
  122. describe('defineEmits w/ type declaration', () => {
  123. const emit = defineEmits<(e: 'change') => void>()
  124. emit('change')
  125. // @ts-expect-error
  126. emit()
  127. // @ts-expect-error
  128. emit('bar')
  129. type Emits = { (e: 'foo' | 'bar'): void; (e: 'baz', id: number): void }
  130. const emit2 = defineEmits<Emits>()
  131. emit2('foo')
  132. emit2('bar')
  133. emit2('baz', 123)
  134. // @ts-expect-error
  135. emit2('baz')
  136. })
  137. describe('defineEmits w/ alt type declaration', () => {
  138. const emit = defineEmits<{
  139. foo: [id: string]
  140. bar: any[]
  141. baz: []
  142. }>()
  143. emit('foo', 'hi')
  144. // @ts-expect-error
  145. emit('foo')
  146. emit('bar')
  147. emit('bar', 1, 2, 3)
  148. emit('baz')
  149. // @ts-expect-error
  150. emit('baz', 1)
  151. })
  152. describe('defineEmits w/ runtime declaration', () => {
  153. const emit = defineEmits({
  154. foo: () => {},
  155. bar: null
  156. })
  157. emit('foo')
  158. emit('bar', 123)
  159. // @ts-expect-error
  160. emit('baz')
  161. const emit2 = defineEmits(['foo', 'bar'])
  162. emit2('foo')
  163. emit2('bar', 123)
  164. // @ts-expect-error
  165. emit2('baz')
  166. })
  167. describe('defineSlots', () => {
  168. // short syntax
  169. const slots = defineSlots<{
  170. default: { foo: string; bar: number }
  171. optional?: string
  172. }>()
  173. expectType<(scope: { foo: string; bar: number }) => VNode[]>(slots.default)
  174. expectType<undefined | ((scope: string) => VNode[])>(slots.optional)
  175. // literal fn syntax (allow for specifying return type)
  176. const fnSlots = defineSlots<{
  177. default(props: { foo: string; bar: number }): any
  178. optional?(props: string): any
  179. }>()
  180. expectType<(scope: { foo: string; bar: number }) => VNode[]>(fnSlots.default)
  181. expectType<undefined | ((scope: string) => VNode[])>(fnSlots.optional)
  182. const slotsUntype = defineSlots()
  183. expectType<Slots>(slotsUntype)
  184. })
  185. describe('defineModel', () => {
  186. // overload 1
  187. const modelValueRequired = defineModel<boolean>({ required: true })
  188. expectType<Ref<boolean>>(modelValueRequired)
  189. // overload 2
  190. const modelValue = defineModel<string>()
  191. expectType<Ref<string | undefined>>(modelValue)
  192. modelValue.value = 'new value'
  193. const modelValueDefault = defineModel<boolean>({ default: true })
  194. expectType<Ref<boolean>>(modelValueDefault)
  195. // overload 3
  196. const countRequired = defineModel<number>('count', { required: false })
  197. expectType<Ref<number | undefined>>(countRequired)
  198. // overload 4
  199. const count = defineModel<number>('count')
  200. expectType<Ref<number | undefined>>(count)
  201. const countDefault = defineModel<number>('count', { default: 1 })
  202. expectType<Ref<number>>(countDefault)
  203. // infer type from default
  204. const inferred = defineModel({ default: 123 })
  205. expectType<Ref<number | undefined>>(inferred)
  206. const inferredRequired = defineModel({ default: 123, required: true })
  207. expectType<Ref<number>>(inferredRequired)
  208. // @ts-expect-error type / default mismatch
  209. defineModel<string>({ default: 123 })
  210. // @ts-expect-error unknown props option
  211. defineModel({ foo: 123 })
  212. // accept defineModel-only options
  213. defineModel({ local: true })
  214. defineModel('foo', { local: true })
  215. })
  216. describe('useModel', () => {
  217. defineComponent({
  218. props: ['foo'],
  219. setup(props) {
  220. const r = useModel(props, 'foo')
  221. expectType<Ref<any>>(r)
  222. // @ts-expect-error
  223. useModel(props, 'bar')
  224. }
  225. })
  226. defineComponent({
  227. props: {
  228. foo: String,
  229. bar: { type: Number, required: true },
  230. baz: { type: Boolean }
  231. },
  232. setup(props) {
  233. expectType<Ref<string | undefined>>(useModel(props, 'foo'))
  234. expectType<Ref<number>>(useModel(props, 'bar'))
  235. expectType<Ref<boolean>>(useModel(props, 'baz'))
  236. }
  237. })
  238. })
  239. describe('useAttrs', () => {
  240. const attrs = useAttrs()
  241. expectType<Record<string, unknown>>(attrs)
  242. })
  243. describe('useSlots', () => {
  244. const slots = useSlots()
  245. expectType<Slots>(slots)
  246. })