setupHelpers.test-d.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. import {
  2. defineProps,
  3. defineEmits,
  4. useAttrs,
  5. useSlots,
  6. withDefaults,
  7. Slots,
  8. defineSlots,
  9. VNode,
  10. Ref,
  11. defineModel,
  12. toRefs
  13. } from 'vue'
  14. import { describe, expectType } from './utils'
  15. import { defineComponent } from 'vue'
  16. import { useModel } from 'vue'
  17. describe('defineProps w/ type declaration', () => {
  18. // type declaration
  19. const props = defineProps<{
  20. foo: string
  21. bool?: boolean
  22. boolAndUndefined: boolean | undefined
  23. file?: File | File[]
  24. }>()
  25. // explicitly declared type should be refined
  26. expectType<string>(props.foo)
  27. // @ts-expect-error
  28. props.bar
  29. expectType<boolean>(props.bool)
  30. expectType<boolean>(props.boolAndUndefined)
  31. })
  32. describe('defineProps w/ generics', () => {
  33. function test<T extends boolean>() {
  34. const props = defineProps<{ foo: T; bar: string; x?: boolean }>()
  35. expectType<T>(props.foo)
  36. expectType<string>(props.bar)
  37. expectType<boolean>(props.x)
  38. }
  39. test()
  40. })
  41. describe('defineProps w/ type declaration + withDefaults', () => {
  42. const res = withDefaults(
  43. defineProps<{
  44. number?: number
  45. arr?: string[]
  46. obj?: { x: number }
  47. fn?: (e: string) => void
  48. genStr?: string
  49. x?: string
  50. y?: string
  51. z?: string
  52. bool?: boolean
  53. boolAndUndefined: boolean | undefined
  54. }>(),
  55. {
  56. number: 123,
  57. arr: () => [],
  58. obj: () => ({ x: 123 }),
  59. fn: () => {},
  60. genStr: () => '',
  61. y: undefined,
  62. z: 'string'
  63. }
  64. )
  65. res.number + 1
  66. res.arr.push('hi')
  67. res.obj.x
  68. res.fn('hi')
  69. res.genStr.slice()
  70. // @ts-expect-error
  71. res.x.slice()
  72. // @ts-expect-error
  73. res.y.slice()
  74. expectType<string | undefined>(res.x)
  75. expectType<string | undefined>(res.y)
  76. expectType<string>(res.z)
  77. expectType<boolean>(res.bool)
  78. expectType<boolean>(res.boolAndUndefined)
  79. })
  80. describe('defineProps w/ union type declaration + withDefaults', () => {
  81. withDefaults(
  82. defineProps<{
  83. union1?: number | number[] | { x: number }
  84. union2?: number | number[] | { x: number }
  85. union3?: number | number[] | { x: number }
  86. union4?: number | number[] | { x: number }
  87. }>(),
  88. {
  89. union1: 123,
  90. union2: () => [123],
  91. union3: () => ({ x: 123 }),
  92. union4: () => 123
  93. }
  94. )
  95. })
  96. describe('defineProps w/ generic type declaration + withDefaults', <T extends
  97. number, TA extends {
  98. a: string
  99. }, TString extends string>() => {
  100. const res = withDefaults(
  101. defineProps<{
  102. n?: number
  103. bool?: boolean
  104. s?: string
  105. generic1?: T[] | { x: T }
  106. generic2?: { x: T }
  107. generic3?: TString
  108. generic4?: TA
  109. }>(),
  110. {
  111. n: 123,
  112. generic1: () => [123, 33] as T[],
  113. generic2: () => ({ x: 123 }) as { x: T },
  114. generic3: () => 'test' as TString,
  115. generic4: () => ({ a: 'test' }) as TA
  116. }
  117. )
  118. res.n + 1
  119. // @ts-expect-error should be readonly
  120. res.n++
  121. // @ts-expect-error should be readonly
  122. res.s = ''
  123. expectType<T[] | { x: T }>(res.generic1)
  124. expectType<{ x: T }>(res.generic2)
  125. expectType<TString>(res.generic3)
  126. expectType<TA>(res.generic4)
  127. expectType<boolean>(res.bool)
  128. })
  129. describe('withDefaults w/ boolean type', () => {
  130. const res1 = withDefaults(
  131. defineProps<{
  132. bool?: boolean
  133. }>(),
  134. { bool: false }
  135. )
  136. expectType<boolean>(res1.bool)
  137. const res2 = withDefaults(
  138. defineProps<{
  139. bool?: boolean
  140. }>(),
  141. {
  142. bool: undefined
  143. }
  144. )
  145. expectType<boolean | undefined>(res2.bool)
  146. })
  147. describe('defineProps w/ runtime declaration', () => {
  148. // runtime declaration
  149. const props = defineProps({
  150. foo: String,
  151. bar: {
  152. type: Number,
  153. default: 1
  154. },
  155. baz: {
  156. type: Array,
  157. required: true
  158. }
  159. })
  160. expectType<{
  161. foo?: string
  162. bar: number
  163. baz: unknown[]
  164. }>(props)
  165. props.foo && props.foo + 'bar'
  166. props.bar + 1
  167. // @ts-expect-error should be readonly
  168. props.bar++
  169. props.baz.push(1)
  170. const props2 = defineProps(['foo', 'bar'])
  171. props2.foo + props2.bar
  172. // @ts-expect-error
  173. props2.baz
  174. })
  175. describe('defineEmits w/ type declaration', () => {
  176. const emit = defineEmits<(e: 'change') => void>()
  177. emit('change')
  178. // @ts-expect-error
  179. emit()
  180. // @ts-expect-error
  181. emit('bar')
  182. type Emits = { (e: 'foo' | 'bar'): void; (e: 'baz', id: number): void }
  183. const emit2 = defineEmits<Emits>()
  184. emit2('foo')
  185. emit2('bar')
  186. emit2('baz', 123)
  187. // @ts-expect-error
  188. emit2('baz')
  189. })
  190. describe('defineEmits w/ alt type declaration', () => {
  191. const emit = defineEmits<{
  192. foo: [id: string]
  193. bar: any[]
  194. baz: []
  195. }>()
  196. emit('foo', 'hi')
  197. // @ts-expect-error
  198. emit('foo')
  199. emit('bar')
  200. emit('bar', 1, 2, 3)
  201. emit('baz')
  202. // @ts-expect-error
  203. emit('baz', 1)
  204. })
  205. describe('defineEmits w/ runtime declaration', () => {
  206. const emit = defineEmits({
  207. foo: () => {},
  208. bar: null
  209. })
  210. emit('foo')
  211. emit('bar', 123)
  212. // @ts-expect-error
  213. emit('baz')
  214. const emit2 = defineEmits(['foo', 'bar'])
  215. emit2('foo')
  216. emit2('bar', 123)
  217. // @ts-expect-error
  218. emit2('baz')
  219. })
  220. describe('defineSlots', () => {
  221. // literal fn syntax (allow for specifying return type)
  222. const fnSlots = defineSlots<{
  223. default(props: { foo: string; bar: number }): any
  224. optional?(props: string): any
  225. }>()
  226. expectType<(scope: { foo: string; bar: number }) => VNode[]>(fnSlots.default)
  227. expectType<undefined | ((scope: string) => VNode[])>(fnSlots.optional)
  228. const slotsUntype = defineSlots()
  229. expectType<Slots>(slotsUntype)
  230. })
  231. describe('defineSlots generic', <T extends Record<string, any>>() => {
  232. const props = defineProps<{
  233. item: T
  234. }>()
  235. const slots = defineSlots<
  236. {
  237. [K in keyof T as `slot-${K & string}`]?: (props: { item: T }) => any
  238. } & {
  239. label?: (props: { item: T }) => any
  240. }
  241. >()
  242. for (const key of Object.keys(props.item) as (keyof T & string)[]) {
  243. slots[`slot-${String(key)}`]?.({
  244. item: props.item
  245. })
  246. }
  247. slots.label?.({ item: props.item })
  248. // @ts-expect-error calling wrong slot
  249. slots.foo({})
  250. })
  251. describe('defineModel', () => {
  252. // overload 1
  253. const modelValueRequired = defineModel<boolean>({ required: true })
  254. expectType<Ref<boolean>>(modelValueRequired)
  255. // overload 2
  256. const modelValue = defineModel<string>()
  257. expectType<Ref<string | undefined>>(modelValue)
  258. modelValue.value = 'new value'
  259. const modelValueDefault = defineModel<boolean>({ default: true })
  260. expectType<Ref<boolean>>(modelValueDefault)
  261. // overload 3
  262. const countRequired = defineModel<number>('count', { required: false })
  263. expectType<Ref<number | undefined>>(countRequired)
  264. // overload 4
  265. const count = defineModel<number>('count')
  266. expectType<Ref<number | undefined>>(count)
  267. const countDefault = defineModel<number>('count', { default: 1 })
  268. expectType<Ref<number>>(countDefault)
  269. // infer type from default
  270. const inferred = defineModel({ default: 123 })
  271. expectType<Ref<number | undefined>>(inferred)
  272. const inferredRequired = defineModel({ default: 123, required: true })
  273. expectType<Ref<number>>(inferredRequired)
  274. // @ts-expect-error type / default mismatch
  275. defineModel<string>({ default: 123 })
  276. // @ts-expect-error unknown props option
  277. defineModel({ foo: 123 })
  278. // accept defineModel-only options
  279. defineModel({ local: true })
  280. defineModel('foo', { local: true })
  281. })
  282. describe('useModel', () => {
  283. defineComponent({
  284. props: ['foo'],
  285. setup(props) {
  286. const r = useModel(props, 'foo')
  287. expectType<Ref<any>>(r)
  288. // @ts-expect-error
  289. useModel(props, 'bar')
  290. }
  291. })
  292. defineComponent({
  293. props: {
  294. foo: String,
  295. bar: { type: Number, required: true },
  296. baz: { type: Boolean }
  297. },
  298. setup(props) {
  299. expectType<Ref<string | undefined>>(useModel(props, 'foo'))
  300. expectType<Ref<number>>(useModel(props, 'bar'))
  301. expectType<Ref<boolean>>(useModel(props, 'baz'))
  302. }
  303. })
  304. })
  305. describe('useAttrs', () => {
  306. const attrs = useAttrs()
  307. expectType<Record<string, unknown>>(attrs)
  308. })
  309. describe('useSlots', () => {
  310. const slots = useSlots()
  311. expectType<Slots>(slots)
  312. })
  313. describe('defineSlots generic', <T extends Record<string, any>>() => {
  314. const props = defineProps<{
  315. item: T
  316. }>()
  317. const slots = defineSlots<
  318. {
  319. [K in keyof T as `slot-${K & string}`]?: (props: { item: T }) => any
  320. } & {
  321. label?: (props: { item: T }) => any
  322. }
  323. >()
  324. // @ts-expect-error slots should be readonly
  325. slots.label = () => {}
  326. // @ts-expect-error non existing slot
  327. slots['foo-asdas']?.({
  328. item: props.item
  329. })
  330. for (const key in props.item) {
  331. slots[`slot-${String(key)}`]?.({
  332. item: props.item
  333. })
  334. slots[`slot-${String(key as keyof T)}`]?.({
  335. item: props.item
  336. })
  337. }
  338. for (const key of Object.keys(props.item) as (keyof T)[]) {
  339. slots[`slot-${String(key)}`]?.({
  340. item: props.item
  341. })
  342. }
  343. slots.label?.({ item: props.item })
  344. // @ts-expect-error calling wrong slot
  345. slots.foo({})
  346. })
  347. describe('defineSlots generic strict', <T extends {
  348. foo: 'foo'
  349. bar: 'bar'
  350. }>() => {
  351. const props = defineProps<{
  352. item: T
  353. }>()
  354. const slots = defineSlots<
  355. {
  356. [K in keyof T as `slot-${K & string}`]?: (props: { item: T }) => any
  357. } & {
  358. label?: (props: { item: T }) => any
  359. }
  360. >()
  361. // slot-bar/foo should be automatically inferred
  362. slots['slot-bar']?.({ item: props.item })
  363. slots['slot-foo']?.({ item: props.item })
  364. slots.label?.({ item: props.item })
  365. // @ts-expect-error not part of the extends
  366. slots['slot-RANDOM']?.({ item: props.item })
  367. // @ts-expect-error slots should be readonly
  368. slots.label = () => {}
  369. // @ts-expect-error calling wrong slot
  370. slots.foo({})
  371. })
  372. // #6420
  373. describe('toRefs w/ type declaration', () => {
  374. const props = defineProps<{
  375. file?: File | File[]
  376. }>()
  377. expectType<Ref<File | File[] | undefined>>(toRefs(props).file)
  378. })