setupHelpers.test-d.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. import {
  2. type Ref,
  3. type Slots,
  4. type VNode,
  5. defineComponent,
  6. defineEmits,
  7. defineModel,
  8. defineProps,
  9. defineSlots,
  10. toRefs,
  11. useAttrs,
  12. useModel,
  13. useSlots,
  14. withDefaults,
  15. } from 'vue'
  16. import { describe, expectType } from './utils'
  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. // modifiers
  275. const [_, modifiers] = defineModel<string>()
  276. expectType<true | undefined>(modifiers.foo)
  277. // limit supported modifiers
  278. const [__, typedModifiers] = defineModel<string, 'trim' | 'capitalize'>()
  279. expectType<true | undefined>(typedModifiers.trim)
  280. expectType<true | undefined>(typedModifiers.capitalize)
  281. // @ts-expect-error
  282. typedModifiers.foo
  283. // transformers with type
  284. defineModel<string>({
  285. get(val) {
  286. return val.toLowerCase()
  287. },
  288. set(val) {
  289. return val.toUpperCase()
  290. },
  291. })
  292. // transformers with runtime type
  293. defineModel({
  294. type: String,
  295. get(val) {
  296. return val.toLowerCase()
  297. },
  298. set(val) {
  299. return val.toUpperCase()
  300. },
  301. })
  302. // @ts-expect-error type / default mismatch
  303. defineModel<string>({ default: 123 })
  304. // @ts-expect-error unknown props option
  305. defineModel({ foo: 123 })
  306. })
  307. describe('useModel', () => {
  308. defineComponent({
  309. props: ['foo'],
  310. setup(props) {
  311. const r = useModel(props, 'foo')
  312. expectType<Ref<any>>(r)
  313. // @ts-expect-error
  314. useModel(props, 'bar')
  315. },
  316. })
  317. defineComponent({
  318. props: {
  319. foo: String,
  320. bar: { type: Number, required: true },
  321. baz: { type: Boolean },
  322. },
  323. setup(props) {
  324. expectType<Ref<string | undefined>>(useModel(props, 'foo'))
  325. expectType<Ref<number>>(useModel(props, 'bar'))
  326. expectType<Ref<boolean>>(useModel(props, 'baz'))
  327. },
  328. })
  329. })
  330. describe('useAttrs', () => {
  331. const attrs = useAttrs()
  332. expectType<Record<string, unknown>>(attrs)
  333. })
  334. describe('useSlots', () => {
  335. const slots = useSlots()
  336. expectType<Slots>(slots)
  337. })
  338. describe('defineSlots generic', <T extends Record<string, any>>() => {
  339. const props = defineProps<{
  340. item: T
  341. }>()
  342. const slots = defineSlots<
  343. {
  344. [K in keyof T as `slot-${K & string}`]?: (props: { item: T }) => any
  345. } & {
  346. label?: (props: { item: T }) => any
  347. }
  348. >()
  349. // @ts-expect-error slots should be readonly
  350. slots.label = () => {}
  351. // @ts-expect-error non existing slot
  352. slots['foo-asdas']?.({
  353. item: props.item,
  354. })
  355. for (const key in props.item) {
  356. slots[`slot-${String(key)}`]?.({
  357. item: props.item,
  358. })
  359. slots[`slot-${String(key as keyof T)}`]?.({
  360. item: props.item,
  361. })
  362. }
  363. for (const key of Object.keys(props.item) as (keyof T)[]) {
  364. slots[`slot-${String(key)}`]?.({
  365. item: props.item,
  366. })
  367. }
  368. slots.label?.({ item: props.item })
  369. // @ts-expect-error calling wrong slot
  370. slots.foo({})
  371. })
  372. describe('defineSlots generic strict', <T extends {
  373. foo: 'foo'
  374. bar: 'bar'
  375. }>() => {
  376. const props = defineProps<{
  377. item: T
  378. }>()
  379. const slots = defineSlots<
  380. {
  381. [K in keyof T as `slot-${K & string}`]?: (props: { item: T }) => any
  382. } & {
  383. label?: (props: { item: T }) => any
  384. }
  385. >()
  386. // slot-bar/foo should be automatically inferred
  387. slots['slot-bar']?.({ item: props.item })
  388. slots['slot-foo']?.({ item: props.item })
  389. slots.label?.({ item: props.item })
  390. // @ts-expect-error not part of the extends
  391. slots['slot-RANDOM']?.({ item: props.item })
  392. // @ts-expect-error slots should be readonly
  393. slots.label = () => {}
  394. // @ts-expect-error calling wrong slot
  395. slots.foo({})
  396. })
  397. // #6420
  398. describe('toRefs w/ type declaration', () => {
  399. const props = defineProps<{
  400. file?: File | File[]
  401. }>()
  402. expectType<Ref<File | File[] | undefined>>(toRefs(props).file)
  403. })