setup-helpers-test.ts 2.7 KB

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