functionalComponent.test-d.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import type { FunctionalVaporComponent } from 'vue'
  2. import { expectType } from '../utils'
  3. import type { Block } from 'vue'
  4. // simple function signature
  5. const VaporComp = (props: { foo: number }) => [<div>123</div>]
  6. // TSX
  7. expectType<JSX.Element>(<VaporComp foo={1} />)
  8. expectType<JSX.Element>(<VaporComp foo={1} key="1" />)
  9. expectType<JSX.Element>(<VaporComp foo={1} ref="ref" />)
  10. // @ts-expect-error
  11. ;<Foo />
  12. // @ts-expect-error
  13. ;<Foo foo="bar" />
  14. // @ts-expect-error
  15. ;<Foo baz="bar" />
  16. // Explicit signature with props + emits
  17. const Bar: FunctionalVaporComponent<
  18. { foo: number },
  19. { update: (value: number) => void }
  20. > = (props, { emit }) => {
  21. expectType<number>(props.foo)
  22. emit('update', 123)
  23. // @ts-expect-error
  24. emit('nope')
  25. // @ts-expect-error
  26. emit('update')
  27. // @ts-expect-error
  28. emit('update', 'nope')
  29. return []
  30. }
  31. // assigning runtime options
  32. Bar.props = {
  33. foo: Number,
  34. }
  35. // @ts-expect-error
  36. Bar.props = { foo: String }
  37. Bar.emits = {
  38. update: value => value > 1,
  39. }
  40. // @ts-expect-error
  41. Bar.emits = { baz: () => void 0 }
  42. // TSX
  43. expectType<JSX.Element>(<Bar foo={1} onUpdate={() => {}} />)
  44. // @ts-expect-error
  45. ;<Foo />
  46. // @ts-expect-error
  47. ;<Bar foo="bar" />
  48. // @ts-expect-error
  49. ;<Foo baz="bar" />
  50. const Quux: FunctionalVaporComponent<
  51. {},
  52. {},
  53. {
  54. default: (props: { foo: number }) => Block
  55. optional?: (props: { foo: number }) => Block
  56. }
  57. > = (props, { emit, slots }) => {
  58. expectType<{
  59. default: (scope: { foo: number }) => Block
  60. optional?: (scope: { foo: number }) => Block
  61. }>(slots)
  62. slots.default({ foo: 123 })
  63. // @ts-expect-error
  64. slots.default({ foo: 'fesf' })
  65. slots.optional?.({ foo: 123 })
  66. // @ts-expect-error
  67. slots.optional?.({ foo: 'fesf' })
  68. // @ts-expect-error
  69. slots.optional({ foo: 123 })
  70. return []
  71. }
  72. ;<Quux />