setupHelpers.test-d.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. type Emits = { (e: 'foo' | 'bar'): void; (e: 'baz', id: number): void }
  55. const emit2 = defineEmit<Emits>()
  56. emit2('foo')
  57. emit2('bar')
  58. emit2('baz', 123)
  59. // @ts-expect-error
  60. emit2('baz')
  61. })
  62. describe('defineEmit w/ runtime declaration', () => {
  63. const emit = defineEmit({
  64. foo: () => {},
  65. bar: null
  66. })
  67. emit('foo')
  68. emit('bar', 123)
  69. // @ts-expect-error
  70. emit('baz')
  71. const emit2 = defineEmit(['foo', 'bar'])
  72. emit2('foo')
  73. emit2('bar', 123)
  74. // @ts-expect-error
  75. emit2('baz')
  76. })
  77. describe('useContext', () => {
  78. const { attrs, emit, slots } = useContext()
  79. expectType<Record<string, unknown>>(attrs)
  80. expectType<(...args: any[]) => void>(emit)
  81. expectType<Slots>(slots)
  82. // @ts-expect-error
  83. props.foo
  84. // should be able to emit anything
  85. emit('foo')
  86. emit('bar')
  87. })