setupHelpers.test-d.ts 1.7 KB

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