h.test-d.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // This file tests a number of cases that *should* fail using tsd:
  2. // https://github.com/SamVerschueren/tsd
  3. // It will probably show up red in VSCode, and it's intended. We cannot use
  4. // directives like @ts-ignore or @ts-nocheck since that would suppress the
  5. // errors that should be caught.
  6. import { expectError } from 'tsd'
  7. import { h, createComponent, ref, Fragment, Portal, Suspense } from './index'
  8. // h inference w/ element
  9. // key
  10. h('div', { key: 1 })
  11. h('div', { key: 'foo' })
  12. expectError(h('div', { key: [] }))
  13. expectError(h('div', { key: {} }))
  14. // ref
  15. h('div', { ref: 'foo' })
  16. h('div', { ref: ref(null) })
  17. h('div', { ref: el => {} })
  18. expectError(h('div', { ref: [] }))
  19. expectError(h('div', { ref: {} }))
  20. expectError(h('div', { ref: 123 }))
  21. // h inference w/ Fragment
  22. // only accepts array children
  23. h(Fragment, ['hello'])
  24. h(Fragment, { key: 123 }, ['hello'])
  25. expectError(h(Fragment, 'foo'))
  26. expectError(h(Fragment, { key: 123 }, 'bar'))
  27. // h inference w/ Portal
  28. h(Portal, { target: '#foo' }, 'hello')
  29. expectError(h(Portal))
  30. expectError(h(Portal, {}))
  31. expectError(h(Portal, { target: '#foo' }))
  32. // h inference w/ Suspense
  33. h(Suspense, { onRecede: () => {}, onResolve: () => {} }, 'hello')
  34. h(Suspense, 'foo')
  35. h(Suspense, () => 'foo')
  36. h(Suspense, null, {
  37. default: () => 'foo'
  38. })
  39. expectError(h(Suspense, { onResolve: 1 }))
  40. // h inference w/ functional component
  41. const Func = (_props: { foo: string; bar?: number }) => ''
  42. h(Func, { foo: 'hello' })
  43. h(Func, { foo: 'hello', bar: 123 })
  44. expectError(h(Func, { foo: 123 }))
  45. expectError(h(Func, {}))
  46. expectError(h(Func, { bar: 123 }))
  47. // h inference w/ plain object component
  48. const Foo = {
  49. props: {
  50. foo: String
  51. }
  52. }
  53. h(Foo, { foo: 'ok' })
  54. h(Foo, { foo: 'ok', class: 'extra' })
  55. // should fail on wrong type
  56. expectError(h(Foo, { foo: 1 }))
  57. // h inference w/ createComponent
  58. const Bar = createComponent({
  59. props: {
  60. foo: String,
  61. bar: {
  62. type: Number,
  63. required: true
  64. }
  65. }
  66. })
  67. h(Bar, { bar: 1 })
  68. h(Bar, { bar: 1, foo: 'ok' })
  69. // should allow extraneous props (attrs fallthrough)
  70. h(Bar, { bar: 1, foo: 'ok', class: 'extra' })
  71. // should fail on missing required prop
  72. expectError(h(Bar, {}))
  73. expectError(h(Bar, { foo: 'ok' }))
  74. // should fail on wrong type
  75. expectError(h(Bar, { bar: 1, foo: 1 }))