setupHelpers.test-d.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {
  2. expectType,
  3. defineProps,
  4. defineEmit,
  5. defineEmits,
  6. useContext,
  7. Slots,
  8. describe
  9. } from './index'
  10. describe('defineProps w/ type declaration', () => {
  11. // type declaration
  12. const props = defineProps<{
  13. foo: string
  14. }>()
  15. // explicitly declared type should be refined
  16. expectType<string>(props.foo)
  17. // @ts-expect-error
  18. props.bar
  19. })
  20. describe('defineProps w/ runtime declaration', () => {
  21. // runtime declaration
  22. const props = defineProps({
  23. foo: String,
  24. bar: {
  25. type: Number,
  26. default: 1
  27. },
  28. baz: {
  29. type: Array,
  30. required: true
  31. }
  32. })
  33. expectType<{
  34. foo?: string
  35. bar: number
  36. baz: unknown[]
  37. }>(props)
  38. props.foo && props.foo + 'bar'
  39. props.bar + 1
  40. // @ts-expect-error should be readonly
  41. props.bar++
  42. props.baz.push(1)
  43. const props2 = defineProps(['foo', 'bar'])
  44. props2.foo + props2.bar
  45. // @ts-expect-error
  46. props2.baz
  47. })
  48. describe('defineEmits w/ type declaration', () => {
  49. const emit = defineEmits<(e: 'change') => void>()
  50. emit('change')
  51. // @ts-expect-error
  52. emit()
  53. // @ts-expect-error
  54. emit('bar')
  55. type Emits = { (e: 'foo' | 'bar'): void; (e: 'baz', id: number): void }
  56. const emit2 = defineEmits<Emits>()
  57. emit2('foo')
  58. emit2('bar')
  59. emit2('baz', 123)
  60. // @ts-expect-error
  61. emit2('baz')
  62. })
  63. describe('defineEmits w/ runtime declaration', () => {
  64. const emit = defineEmits({
  65. foo: () => {},
  66. bar: null
  67. })
  68. emit('foo')
  69. emit('bar', 123)
  70. // @ts-expect-error
  71. emit('baz')
  72. const emit2 = defineEmits(['foo', 'bar'])
  73. emit2('foo')
  74. emit2('bar', 123)
  75. // @ts-expect-error
  76. emit2('baz')
  77. })
  78. describe('deprecated defineEmit', () => {
  79. const emit = defineEmit({
  80. foo: () => {},
  81. bar: null
  82. })
  83. emit('foo')
  84. emit('bar', 123)
  85. // @ts-expect-error
  86. emit('baz')
  87. })
  88. describe('useContext', () => {
  89. const { attrs, emit, slots } = useContext()
  90. expectType<Record<string, unknown>>(attrs)
  91. expectType<(...args: any[]) => void>(emit)
  92. expectType<Slots>(slots)
  93. // @ts-expect-error
  94. props.foo
  95. // should be able to emit anything
  96. emit('foo')
  97. emit('bar')
  98. })