setupHelpers.test-d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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/ object union + withDefaults', () => {
  97. const props = withDefaults(
  98. defineProps<
  99. {
  100. foo: string
  101. } & (
  102. | {
  103. type: 'hello'
  104. bar: string
  105. }
  106. | {
  107. type: 'world'
  108. bar: number
  109. }
  110. )
  111. >(),
  112. {
  113. foo: 'default value!',
  114. },
  115. )
  116. expectType<
  117. | {
  118. readonly type: 'hello'
  119. readonly bar: string
  120. readonly foo: string
  121. }
  122. | {
  123. readonly type: 'world'
  124. readonly bar: number
  125. readonly foo: string
  126. }
  127. >(props)
  128. })
  129. describe('defineProps w/ generic type declaration + withDefaults', <T extends
  130. number, TA extends {
  131. a: string
  132. }, TString extends string>() => {
  133. const res = withDefaults(
  134. defineProps<{
  135. n?: number
  136. bool?: boolean
  137. s?: string
  138. generic1?: T[] | { x: T }
  139. generic2?: { x: T }
  140. generic3?: TString
  141. generic4?: TA
  142. }>(),
  143. {
  144. n: 123,
  145. generic1: () => [123, 33] as T[],
  146. generic2: () => ({ x: 123 }) as { x: T },
  147. generic3: () => 'test' as TString,
  148. generic4: () => ({ a: 'test' }) as TA,
  149. },
  150. )
  151. res.n + 1
  152. // @ts-expect-error should be readonly
  153. res.n++
  154. // @ts-expect-error should be readonly
  155. res.s = ''
  156. expectType<T[] | { x: T }>(res.generic1)
  157. expectType<{ x: T }>(res.generic2)
  158. expectType<TString>(res.generic3)
  159. expectType<TA>(res.generic4)
  160. expectType<boolean>(res.bool)
  161. })
  162. describe('withDefaults w/ boolean type', () => {
  163. const res1 = withDefaults(
  164. defineProps<{
  165. bool?: boolean
  166. }>(),
  167. { bool: false },
  168. )
  169. expectType<boolean>(res1.bool)
  170. const res2 = withDefaults(
  171. defineProps<{
  172. bool?: boolean
  173. }>(),
  174. {
  175. bool: undefined,
  176. },
  177. )
  178. expectType<boolean | undefined>(res2.bool)
  179. })
  180. describe('defineProps w/ runtime declaration', () => {
  181. // runtime declaration
  182. const props = defineProps({
  183. foo: String,
  184. bar: {
  185. type: Number,
  186. default: 1,
  187. },
  188. baz: {
  189. type: Array,
  190. required: true,
  191. },
  192. })
  193. expectType<{
  194. foo?: string
  195. bar: number
  196. baz: unknown[]
  197. }>(props)
  198. props.foo && props.foo + 'bar'
  199. props.bar + 1
  200. // @ts-expect-error should be readonly
  201. props.bar++
  202. props.baz.push(1)
  203. const props2 = defineProps(['foo', 'bar'])
  204. props2.foo + props2.bar
  205. // @ts-expect-error
  206. props2.baz
  207. })
  208. describe('defineEmits w/ type declaration', () => {
  209. const emit = defineEmits<(e: 'change') => void>()
  210. emit('change')
  211. // @ts-expect-error
  212. emit()
  213. // @ts-expect-error
  214. emit('bar')
  215. type Emits = { (e: 'foo' | 'bar'): void; (e: 'baz', id: number): void }
  216. const emit2 = defineEmits<Emits>()
  217. emit2('foo')
  218. emit2('bar')
  219. emit2('baz', 123)
  220. // @ts-expect-error
  221. emit2('baz')
  222. })
  223. describe('defineEmits w/ alt type declaration', () => {
  224. const emit = defineEmits<{
  225. foo: [id: string]
  226. bar: any[]
  227. baz: []
  228. }>()
  229. emit('foo', 'hi')
  230. // @ts-expect-error
  231. emit('foo')
  232. emit('bar')
  233. emit('bar', 1, 2, 3)
  234. emit('baz')
  235. // @ts-expect-error
  236. emit('baz', 1)
  237. })
  238. describe('defineEmits w/ runtime declaration', () => {
  239. const emit = defineEmits({
  240. foo: () => {},
  241. bar: null,
  242. })
  243. emit('foo')
  244. emit('bar', 123)
  245. // @ts-expect-error
  246. emit('baz')
  247. const emit2 = defineEmits(['foo', 'bar'])
  248. emit2('foo')
  249. emit2('bar', 123)
  250. // @ts-expect-error
  251. emit2('baz')
  252. })
  253. describe('defineSlots', () => {
  254. // literal fn syntax (allow for specifying return type)
  255. const fnSlots = defineSlots<{
  256. default(props: { foo: string; bar: number }): any
  257. optional?(props: string): any
  258. }>()
  259. expectType<(scope: { foo: string; bar: number }) => VNode[]>(fnSlots.default)
  260. expectType<undefined | ((scope: string) => VNode[])>(fnSlots.optional)
  261. const slotsUntype = defineSlots()
  262. expectType<Slots>(slotsUntype)
  263. })
  264. describe('defineSlots generic', <T extends Record<string, any>>() => {
  265. const props = defineProps<{
  266. item: T
  267. }>()
  268. const slots = defineSlots<
  269. {
  270. [K in keyof T as `slot-${K & string}`]?: (props: { item: T }) => any
  271. } & {
  272. label?: (props: { item: T }) => any
  273. }
  274. >()
  275. for (const key of Object.keys(props.item) as (keyof T & string)[]) {
  276. slots[`slot-${String(key)}`]?.({
  277. item: props.item,
  278. })
  279. }
  280. slots.label?.({ item: props.item })
  281. // @ts-expect-error calling wrong slot
  282. slots.foo({})
  283. })
  284. describe('defineModel', () => {
  285. // overload 1
  286. const modelValueRequired = defineModel<boolean>({ required: true })
  287. expectType<Ref<boolean>>(modelValueRequired)
  288. // overload 2
  289. const modelValue = defineModel<string>()
  290. expectType<Ref<string | undefined>>(modelValue)
  291. modelValue.value = 'new value'
  292. const modelValueDefault = defineModel<boolean>({ default: true })
  293. expectType<Ref<boolean>>(modelValueDefault)
  294. // overload 3
  295. const countRequired = defineModel<number>('count', { required: false })
  296. expectType<Ref<number | undefined>>(countRequired)
  297. // overload 4
  298. const count = defineModel<number>('count')
  299. expectType<Ref<number | undefined>>(count)
  300. const countDefault = defineModel<number>('count', { default: 1 })
  301. expectType<Ref<number>>(countDefault)
  302. // infer type from default
  303. const inferred = defineModel({ default: 123 })
  304. expectType<Ref<number | undefined>>(inferred)
  305. const inferredRequired = defineModel({ default: 123, required: true })
  306. expectType<Ref<number>>(inferredRequired)
  307. // modifiers
  308. const [_, modifiers] = defineModel<string>()
  309. expectType<true | undefined>(modifiers.foo)
  310. // limit supported modifiers
  311. const [__, typedModifiers] = defineModel<string, 'trim' | 'capitalize'>()
  312. expectType<true | undefined>(typedModifiers.trim)
  313. expectType<true | undefined>(typedModifiers.capitalize)
  314. // @ts-expect-error
  315. typedModifiers.foo
  316. // transformers with type
  317. defineModel<string>({
  318. get(val) {
  319. return val.toLowerCase()
  320. },
  321. set(val) {
  322. return val.toUpperCase()
  323. },
  324. })
  325. // transformers with runtime type
  326. defineModel({
  327. type: String,
  328. get(val) {
  329. return val.toLowerCase()
  330. },
  331. set(val) {
  332. return val.toUpperCase()
  333. },
  334. })
  335. // @ts-expect-error type / default mismatch
  336. defineModel<string>({ default: 123 })
  337. // @ts-expect-error unknown props option
  338. defineModel({ foo: 123 })
  339. })
  340. describe('useModel', () => {
  341. defineComponent({
  342. props: ['foo'],
  343. setup(props) {
  344. const r = useModel(props, 'foo')
  345. expectType<Ref<any>>(r)
  346. // @ts-expect-error
  347. useModel(props, 'bar')
  348. },
  349. })
  350. defineComponent({
  351. props: {
  352. foo: String,
  353. bar: { type: Number, required: true },
  354. baz: { type: Boolean },
  355. },
  356. setup(props) {
  357. expectType<Ref<string | undefined>>(useModel(props, 'foo'))
  358. expectType<Ref<number>>(useModel(props, 'bar'))
  359. expectType<Ref<boolean>>(useModel(props, 'baz'))
  360. },
  361. })
  362. })
  363. describe('useAttrs', () => {
  364. const attrs = useAttrs()
  365. expectType<Record<string, unknown>>(attrs)
  366. })
  367. describe('useSlots', () => {
  368. const slots = useSlots()
  369. expectType<Slots>(slots)
  370. })
  371. describe('defineSlots generic', <T extends Record<string, any>>() => {
  372. const props = defineProps<{
  373. item: T
  374. }>()
  375. const slots = defineSlots<
  376. {
  377. [K in keyof T as `slot-${K & string}`]?: (props: { item: T }) => any
  378. } & {
  379. label?: (props: { item: T }) => any
  380. }
  381. >()
  382. // @ts-expect-error slots should be readonly
  383. slots.label = () => {}
  384. // @ts-expect-error non existing slot
  385. slots['foo-asdas']?.({
  386. item: props.item,
  387. })
  388. for (const key in props.item) {
  389. slots[`slot-${String(key)}`]?.({
  390. item: props.item,
  391. })
  392. slots[`slot-${String(key as keyof T)}`]?.({
  393. item: props.item,
  394. })
  395. }
  396. for (const key of Object.keys(props.item) as (keyof T)[]) {
  397. slots[`slot-${String(key)}`]?.({
  398. item: props.item,
  399. })
  400. }
  401. slots.label?.({ item: props.item })
  402. // @ts-expect-error calling wrong slot
  403. slots.foo({})
  404. })
  405. describe('defineSlots generic strict', <T extends {
  406. foo: 'foo'
  407. bar: 'bar'
  408. }>() => {
  409. const props = defineProps<{
  410. item: T
  411. }>()
  412. const slots = defineSlots<
  413. {
  414. [K in keyof T as `slot-${K & string}`]?: (props: { item: T }) => any
  415. } & {
  416. label?: (props: { item: T }) => any
  417. }
  418. >()
  419. // slot-bar/foo should be automatically inferred
  420. slots['slot-bar']?.({ item: props.item })
  421. slots['slot-foo']?.({ item: props.item })
  422. slots.label?.({ item: props.item })
  423. // @ts-expect-error not part of the extends
  424. slots['slot-RANDOM']?.({ item: props.item })
  425. // @ts-expect-error slots should be readonly
  426. slots.label = () => {}
  427. // @ts-expect-error calling wrong slot
  428. slots.foo({})
  429. })
  430. // #6420
  431. describe('toRefs w/ type declaration', () => {
  432. const props = defineProps<{
  433. file?: File | File[]
  434. }>()
  435. expectType<Ref<File | File[] | undefined>>(toRefs(props).file)
  436. })