setupHelpers.test-d.ts 2.9 KB

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