h.test-d.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import {
  2. describe,
  3. h,
  4. defineComponent,
  5. ref,
  6. Fragment,
  7. Teleport,
  8. Suspense,
  9. Component,
  10. expectError,
  11. expectAssignable
  12. } from './index'
  13. describe('h inference w/ element', () => {
  14. // key
  15. h('div', { key: 1 })
  16. h('div', { key: 'foo' })
  17. // @ts-expect-error
  18. expectError(h('div', { key: [] }))
  19. // @ts-expect-error
  20. expectError(h('div', { key: {} }))
  21. // ref
  22. h('div', { ref: 'foo' })
  23. h('div', { ref: ref(null) })
  24. h('div', { ref: _el => {} })
  25. // @ts-expect-error
  26. expectError(h('div', { ref: [] }))
  27. // @ts-expect-error
  28. expectError(h('div', { ref: {} }))
  29. // @ts-expect-error
  30. expectError(h('div', { ref: 123 }))
  31. // slots
  32. const slots = { default: () => {} } // RawSlots
  33. h('div', {}, slots)
  34. })
  35. describe('h inference w/ Fragment', () => {
  36. // only accepts array children
  37. h(Fragment, ['hello'])
  38. h(Fragment, { key: 123 }, ['hello'])
  39. // @ts-expect-error
  40. expectError(h(Fragment, 'foo'))
  41. // @ts-expect-error
  42. expectError(h(Fragment, { key: 123 }, 'bar'))
  43. })
  44. describe('h inference w/ Teleport', () => {
  45. h(Teleport, { to: '#foo' }, 'hello')
  46. // @ts-expect-error
  47. expectError(h(Teleport))
  48. // @ts-expect-error
  49. expectError(h(Teleport, {}))
  50. // @ts-expect-error
  51. expectError(h(Teleport, { to: '#foo' }))
  52. })
  53. describe('h inference w/ Suspense', () => {
  54. h(Suspense, { onRecede: () => {}, onResolve: () => {} }, 'hello')
  55. h(Suspense, 'foo')
  56. h(Suspense, () => 'foo')
  57. h(Suspense, null, {
  58. default: () => 'foo'
  59. })
  60. // @ts-expect-error
  61. expectError(h(Suspense, { onResolve: 1 }))
  62. })
  63. describe('h inference w/ functional component', () => {
  64. const Func = (_props: { foo: string; bar?: number }) => ''
  65. h(Func, { foo: 'hello' })
  66. h(Func, { foo: 'hello', bar: 123 })
  67. // @ts-expect-error
  68. expectError(h(Func, { foo: 123 }))
  69. // @ts-expect-error
  70. expectError(h(Func, {}))
  71. // @ts-expect-error
  72. expectError(h(Func, { bar: 123 }))
  73. })
  74. describe('h support w/ plain object component', () => {
  75. const Foo = {
  76. props: {
  77. foo: String
  78. }
  79. }
  80. h(Foo, { foo: 'ok' })
  81. h(Foo, { foo: 'ok', class: 'extra' })
  82. // no inference in this case
  83. })
  84. describe('h inference w/ defineComponent', () => {
  85. const Foo = defineComponent({
  86. props: {
  87. foo: String,
  88. bar: {
  89. type: Number,
  90. required: true
  91. }
  92. }
  93. })
  94. h(Foo, { bar: 1 })
  95. h(Foo, { bar: 1, foo: 'ok' })
  96. // should allow extraneous props (attrs fallthrough)
  97. h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
  98. // @ts-expect-error should fail on missing required prop
  99. expectError(h(Foo, {}))
  100. // @ts-expect-error
  101. expectError(h(Foo, { foo: 'ok' }))
  102. // @ts-expect-error should fail on wrong type
  103. expectError(h(Foo, { bar: 1, foo: 1 }))
  104. })
  105. // describe('h inference w/ defineComponent + optional props', () => {
  106. // const Foo = defineComponent({
  107. // setup(_props: { foo?: string; bar: number }) {}
  108. // })
  109. // h(Foo, { bar: 1 })
  110. // h(Foo, { bar: 1, foo: 'ok' })
  111. // // should allow extraneous props (attrs fallthrough)
  112. // h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
  113. // // @ts-expect-error should fail on missing required prop
  114. // expectError(h(Foo, {}))
  115. // // @ts-expect-error
  116. // expectError(h(Foo, { foo: 'ok' }))
  117. // // @ts-expect-error should fail on wrong type
  118. // expectError(h(Foo, { bar: 1, foo: 1 }))
  119. // })
  120. // describe('h inference w/ defineComponent + direct function', () => {
  121. // const Foo = defineComponent((_props: { foo?: string; bar: number }) => {})
  122. // h(Foo, { bar: 1 })
  123. // h(Foo, { bar: 1, foo: 'ok' })
  124. // // should allow extraneous props (attrs fallthrough)
  125. // h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
  126. // // @ts-expect-error should fail on missing required prop
  127. // expectError(h(Foo, {}))
  128. // // @ts-expect-error
  129. // expectError(h(Foo, { foo: 'ok' }))
  130. // // @ts-expect-error should fail on wrong type
  131. // expectError(h(Foo, { bar: 1, foo: 1 }))
  132. // })
  133. // #922
  134. describe('h support for generic component type', () => {
  135. function foo(bar: Component) {
  136. h(bar)
  137. h(bar, 'hello')
  138. // @ts-expect-error
  139. h(bar, { id: 'ok' }, 'hello')
  140. }
  141. foo({})
  142. })
  143. // #993
  144. describe('describeComponent extends Component', () => {
  145. // functional
  146. expectAssignable<Component>(
  147. defineComponent((_props: { foo?: string; bar: number }) => {})
  148. )
  149. // typed props
  150. expectAssignable<Component>(defineComponent({}))
  151. // prop arrays
  152. expectAssignable<Component>(
  153. defineComponent({
  154. props: ['a', 'b']
  155. })
  156. )
  157. // prop object
  158. expectAssignable<Component>(
  159. defineComponent({
  160. props: {
  161. foo: String,
  162. bar: {
  163. type: Number,
  164. required: true
  165. }
  166. }
  167. })
  168. )
  169. })
  170. // #1385
  171. describe('component w/ props w/ default value', () => {
  172. const MyComponent = defineComponent({
  173. props: {
  174. message: {
  175. type: String,
  176. default: 'hello'
  177. }
  178. }
  179. })
  180. h(MyComponent, {})
  181. })