h.test-d.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import {
  2. h,
  3. defineComponent,
  4. ref,
  5. Fragment,
  6. Teleport,
  7. Suspense,
  8. Component,
  9. resolveComponent
  10. } from 'vue'
  11. import { describe, expectAssignable } from './utils'
  12. describe('h inference w/ element', () => {
  13. // key
  14. h('div', { key: 1 })
  15. h('div', { key: 'foo' })
  16. // @ts-expect-error
  17. h('div', { key: [] })
  18. // @ts-expect-error
  19. h('div', { key: {} })
  20. // ref
  21. h('div', { ref: 'foo' })
  22. h('div', { ref: ref(null) })
  23. h('div', { ref: _el => {} })
  24. // @ts-expect-error
  25. h('div', { ref: [] })
  26. // @ts-expect-error
  27. h('div', { ref: {} })
  28. // @ts-expect-error
  29. h('div', { ref: 123 })
  30. // slots
  31. const slots = { default: () => {} } // RawSlots
  32. h('div', {}, slots)
  33. })
  34. describe('h inference w/ Fragment', () => {
  35. // only accepts array children
  36. h(Fragment, ['hello'])
  37. h(Fragment, { key: 123 }, ['hello'])
  38. // @ts-expect-error
  39. h(Fragment, 'foo')
  40. // @ts-expect-error
  41. h(Fragment, { key: 123 }, 'bar')
  42. })
  43. describe('h inference w/ Teleport', () => {
  44. h(Teleport, { to: '#foo' }, 'hello')
  45. h(Teleport, { to: '#foo' }, { default() {} })
  46. // @ts-expect-error
  47. h(Teleport)
  48. // @ts-expect-error
  49. h(Teleport, {})
  50. // @ts-expect-error
  51. 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. 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. h(Func, { foo: 123 })
  69. // @ts-expect-error
  70. h(Func, {})
  71. // @ts-expect-error
  72. 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. h(Foo, {})
  100. // @ts-expect-error
  101. h(Foo, { foo: 'ok' })
  102. // @ts-expect-error should fail on wrong type
  103. 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. // h(Foo, {})
  115. // // @ts-expect-error
  116. // h(Foo, { foo: 'ok' })
  117. // // @ts-expect-error should fail on wrong type
  118. // 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. // h(Foo, {})
  128. // // @ts-expect-error
  129. // h(Foo, { foo: 'ok' })
  130. // // @ts-expect-error should fail on wrong type
  131. // h(Foo, { bar: 1, foo: 1 })
  132. // })
  133. // #922 and #3218
  134. describe('h support for generic component type', () => {
  135. function foo(bar: Component) {
  136. h(bar)
  137. h(bar, 'hello')
  138. h(bar, { id: 'ok' }, 'hello')
  139. }
  140. foo({})
  141. })
  142. // #993
  143. describe('describeComponent extends Component', () => {
  144. // functional
  145. expectAssignable<Component>(
  146. defineComponent((_props: { foo?: string; bar: number }) => () => {})
  147. )
  148. // typed props
  149. expectAssignable<Component>(defineComponent({}))
  150. // prop arrays
  151. expectAssignable<Component>(
  152. defineComponent({
  153. props: ['a', 'b']
  154. })
  155. )
  156. // prop object
  157. expectAssignable<Component>(
  158. defineComponent({
  159. props: {
  160. foo: String,
  161. bar: {
  162. type: Number,
  163. required: true
  164. }
  165. }
  166. })
  167. )
  168. })
  169. // #1385
  170. describe('component w/ props w/ default value', () => {
  171. const MyComponent = defineComponent({
  172. props: {
  173. message: {
  174. type: String,
  175. default: 'hello'
  176. }
  177. }
  178. })
  179. h(MyComponent, {})
  180. })
  181. // #2338
  182. describe('Boolean prop implicit false', () => {
  183. const MyComponent = defineComponent({
  184. props: {
  185. visible: Boolean
  186. }
  187. })
  188. h(MyComponent, {})
  189. const RequiredComponent = defineComponent({
  190. props: {
  191. visible: {
  192. type: Boolean,
  193. required: true
  194. }
  195. }
  196. })
  197. h(RequiredComponent, {
  198. visible: true
  199. })
  200. // @ts-expect-error
  201. h(RequiredComponent, {})
  202. })
  203. // #2357
  204. describe('resolveComponent should work', () => {
  205. h(resolveComponent('test'))
  206. h(resolveComponent('test'), {
  207. message: '1'
  208. })
  209. })