setupHelpers.test-d.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import {
  2. defineProps,
  3. defineEmits,
  4. useAttrs,
  5. useSlots,
  6. withDefaults,
  7. Slots
  8. } from 'vue'
  9. import { describe, expectType } from './utils'
  10. describe('defineProps w/ type declaration', () => {
  11. // type declaration
  12. const props = defineProps<{
  13. foo: string
  14. bool?: boolean
  15. boolAndUndefined: boolean | undefined
  16. }>()
  17. // explicitly declared type should be refined
  18. expectType<string>(props.foo)
  19. // @ts-expect-error
  20. props.bar
  21. expectType<boolean>(props.bool)
  22. expectType<boolean>(props.boolAndUndefined)
  23. })
  24. describe('defineProps w/ generics', () => {
  25. function test<T extends boolean>() {
  26. const props = defineProps<{ foo: T; bar: string; x?: boolean }>()
  27. expectType<T>(props.foo)
  28. expectType<string>(props.bar)
  29. expectType<boolean>(props.x)
  30. }
  31. test()
  32. })
  33. describe('defineProps w/ type declaration + withDefaults', () => {
  34. const res = withDefaults(
  35. defineProps<{
  36. number?: number
  37. arr?: string[]
  38. obj?: { x: number }
  39. fn?: (e: string) => void
  40. genStr?: string
  41. x?: string
  42. y?: string
  43. z?: string
  44. bool?: boolean
  45. boolAndUndefined: boolean | undefined
  46. }>(),
  47. {
  48. number: 123,
  49. arr: () => [],
  50. obj: () => ({ x: 123 }),
  51. fn: () => {},
  52. genStr: () => '',
  53. y: undefined,
  54. z: 'string'
  55. }
  56. )
  57. res.number + 1
  58. res.arr.push('hi')
  59. res.obj.x
  60. res.fn('hi')
  61. res.genStr.slice()
  62. // @ts-expect-error
  63. res.x.slice()
  64. // @ts-expect-error
  65. res.y.slice()
  66. expectType<string | undefined>(res.x)
  67. expectType<string | undefined>(res.y)
  68. expectType<string>(res.z)
  69. expectType<boolean>(res.bool)
  70. expectType<boolean>(res.boolAndUndefined)
  71. })
  72. describe('defineProps w/ union type declaration + withDefaults', () => {
  73. withDefaults(
  74. defineProps<{
  75. union1?: number | number[] | { x: number }
  76. union2?: number | number[] | { x: number }
  77. union3?: number | number[] | { x: number }
  78. union4?: number | number[] | { x: number }
  79. }>(),
  80. {
  81. union1: 123,
  82. union2: () => [123],
  83. union3: () => ({ x: 123 }),
  84. union4: () => 123
  85. }
  86. )
  87. })
  88. describe('defineProps w/ runtime declaration', () => {
  89. // runtime declaration
  90. const props = defineProps({
  91. foo: String,
  92. bar: {
  93. type: Number,
  94. default: 1
  95. },
  96. baz: {
  97. type: Array,
  98. required: true
  99. }
  100. })
  101. expectType<{
  102. foo?: string
  103. bar: number
  104. baz: unknown[]
  105. }>(props)
  106. props.foo && props.foo + 'bar'
  107. props.bar + 1
  108. // @ts-expect-error should be readonly
  109. props.bar++
  110. props.baz.push(1)
  111. const props2 = defineProps(['foo', 'bar'])
  112. props2.foo + props2.bar
  113. // @ts-expect-error
  114. props2.baz
  115. })
  116. describe('defineEmits w/ type declaration', () => {
  117. const emit = defineEmits<(e: 'change') => void>()
  118. emit('change')
  119. // @ts-expect-error
  120. emit()
  121. // @ts-expect-error
  122. emit('bar')
  123. type Emits = { (e: 'foo' | 'bar'): void; (e: 'baz', id: number): void }
  124. const emit2 = defineEmits<Emits>()
  125. emit2('foo')
  126. emit2('bar')
  127. emit2('baz', 123)
  128. // @ts-expect-error
  129. emit2('baz')
  130. })
  131. describe('defineEmits w/ runtime declaration', () => {
  132. const emit = defineEmits({
  133. foo: () => {},
  134. bar: null
  135. })
  136. emit('foo')
  137. emit('bar', 123)
  138. // @ts-expect-error
  139. emit('baz')
  140. const emit2 = defineEmits(['foo', 'bar'])
  141. emit2('foo')
  142. emit2('bar', 123)
  143. // @ts-expect-error
  144. emit2('baz')
  145. })
  146. describe('useAttrs', () => {
  147. const attrs = useAttrs()
  148. expectType<Record<string, unknown>>(attrs)
  149. })
  150. describe('useSlots', () => {
  151. const slots = useSlots()
  152. expectType<Slots>(slots)
  153. })