setupHelpers.test-d.ts 2.7 KB

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