setupHelpers.test-d.ts 2.6 KB

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