setupHelpers.test-d.ts 2.7 KB

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