setup-helpers-test.ts 2.9 KB

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