setupHelpers.test-d.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/ generic type declaration + withDefaults', <T extends number, TA extends {
  95. a: string
  96. }, TString extends string>() => {
  97. const res = withDefaults(
  98. defineProps<{
  99. n?: number
  100. bool?: boolean
  101. generic1?: T[] | { x: T }
  102. generic2?: { x: T }
  103. generic3?: TString
  104. generic4?: TA
  105. }>(),
  106. {
  107. n: 123,
  108. generic1: () => [123, 33] as T[],
  109. generic2: () => ({ x: 123 } as { x: T }),
  110. generic3: () => 'test' as TString,
  111. generic4: () => ({ a: 'test' } as TA)
  112. }
  113. )
  114. res.n + 1
  115. expectType<T[] | { x: T }>(res.generic1)
  116. expectType<{ x: T }>(res.generic2)
  117. expectType<TString>(res.generic3)
  118. expectType<TA>(res.generic4)
  119. expectType<boolean>(res.bool)
  120. })
  121. describe('defineProps w/ runtime declaration', () => {
  122. // runtime declaration
  123. const props = defineProps({
  124. foo: String,
  125. bar: {
  126. type: Number,
  127. default: 1
  128. },
  129. baz: {
  130. type: Array,
  131. required: true
  132. }
  133. })
  134. expectType<{
  135. foo?: string
  136. bar: number
  137. baz: unknown[]
  138. }>(props)
  139. props.foo && props.foo + 'bar'
  140. props.bar + 1
  141. // @ts-expect-error should be readonly
  142. props.bar++
  143. props.baz.push(1)
  144. const props2 = defineProps(['foo', 'bar'])
  145. props2.foo + props2.bar
  146. // @ts-expect-error
  147. props2.baz
  148. })
  149. describe('defineEmits w/ type declaration', () => {
  150. const emit = defineEmits<(e: 'change') => void>()
  151. emit('change')
  152. // @ts-expect-error
  153. emit()
  154. // @ts-expect-error
  155. emit('bar')
  156. type Emits = { (e: 'foo' | 'bar'): void; (e: 'baz', id: number): void }
  157. const emit2 = defineEmits<Emits>()
  158. emit2('foo')
  159. emit2('bar')
  160. emit2('baz', 123)
  161. // @ts-expect-error
  162. emit2('baz')
  163. })
  164. describe('defineEmits w/ alt type declaration', () => {
  165. const emit = defineEmits<{
  166. foo: [id: string]
  167. bar: any[]
  168. baz: []
  169. }>()
  170. emit('foo', 'hi')
  171. // @ts-expect-error
  172. emit('foo')
  173. emit('bar')
  174. emit('bar', 1, 2, 3)
  175. emit('baz')
  176. // @ts-expect-error
  177. emit('baz', 1)
  178. })
  179. describe('defineEmits w/ runtime declaration', () => {
  180. const emit = defineEmits({
  181. foo: () => {},
  182. bar: null
  183. })
  184. emit('foo')
  185. emit('bar', 123)
  186. // @ts-expect-error
  187. emit('baz')
  188. const emit2 = defineEmits(['foo', 'bar'])
  189. emit2('foo')
  190. emit2('bar', 123)
  191. // @ts-expect-error
  192. emit2('baz')
  193. })
  194. describe('defineSlots', () => {
  195. // literal fn syntax (allow for specifying return type)
  196. const fnSlots = defineSlots<{
  197. default(props: { foo: string; bar: number }): any
  198. optional?(props: string): any
  199. }>()
  200. expectType<(scope: { foo: string; bar: number }) => VNode[]>(fnSlots.default)
  201. expectType<undefined | ((scope: string) => VNode[])>(fnSlots.optional)
  202. const slotsUntype = defineSlots()
  203. expectType<Slots>(slotsUntype)
  204. })
  205. describe('defineModel', () => {
  206. // overload 1
  207. const modelValueRequired = defineModel<boolean>({ required: true })
  208. expectType<Ref<boolean>>(modelValueRequired)
  209. // overload 2
  210. const modelValue = defineModel<string>()
  211. expectType<Ref<string | undefined>>(modelValue)
  212. modelValue.value = 'new value'
  213. const modelValueDefault = defineModel<boolean>({ default: true })
  214. expectType<Ref<boolean>>(modelValueDefault)
  215. // overload 3
  216. const countRequired = defineModel<number>('count', { required: false })
  217. expectType<Ref<number | undefined>>(countRequired)
  218. // overload 4
  219. const count = defineModel<number>('count')
  220. expectType<Ref<number | undefined>>(count)
  221. const countDefault = defineModel<number>('count', { default: 1 })
  222. expectType<Ref<number>>(countDefault)
  223. // infer type from default
  224. const inferred = defineModel({ default: 123 })
  225. expectType<Ref<number | undefined>>(inferred)
  226. const inferredRequired = defineModel({ default: 123, required: true })
  227. expectType<Ref<number>>(inferredRequired)
  228. // @ts-expect-error type / default mismatch
  229. defineModel<string>({ default: 123 })
  230. // @ts-expect-error unknown props option
  231. defineModel({ foo: 123 })
  232. // accept defineModel-only options
  233. defineModel({ local: true })
  234. defineModel('foo', { local: true })
  235. })
  236. describe('useModel', () => {
  237. defineComponent({
  238. props: ['foo'],
  239. setup(props) {
  240. const r = useModel(props, 'foo')
  241. expectType<Ref<any>>(r)
  242. // @ts-expect-error
  243. useModel(props, 'bar')
  244. }
  245. })
  246. defineComponent({
  247. props: {
  248. foo: String,
  249. bar: { type: Number, required: true },
  250. baz: { type: Boolean }
  251. },
  252. setup(props) {
  253. expectType<Ref<string | undefined>>(useModel(props, 'foo'))
  254. expectType<Ref<number>>(useModel(props, 'bar'))
  255. expectType<Ref<boolean>>(useModel(props, 'baz'))
  256. }
  257. })
  258. })
  259. describe('useAttrs', () => {
  260. const attrs = useAttrs()
  261. expectType<Record<string, unknown>>(attrs)
  262. })
  263. describe('useSlots', () => {
  264. const slots = useSlots()
  265. expectType<Slots>(slots)
  266. })