setupHelpers.test-d.ts 3.2 KB

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