functionalComponent.test-d.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { FunctionalComponent, expectError, expectType } from './index'
  2. // simple function signature
  3. const Foo = (props: { foo: number }) => props.foo
  4. // TSX
  5. expectType<JSX.Element>(<Foo foo={1} />)
  6. expectType<JSX.Element>(<Foo foo={1} key="1" />)
  7. expectType<JSX.Element>(<Foo foo={1} ref="ref" />)
  8. // @ts-expect-error
  9. expectError(<Foo />)
  10. // @ts-expect-error
  11. expectError(<Foo foo="bar" />)
  12. // @ts-expect-error
  13. expectError(<Foo baz="bar" />)
  14. // Explicit signature with props + emits
  15. const Bar: FunctionalComponent<
  16. { foo: number },
  17. { update: (value: number) => void }
  18. > = (props, { emit }) => {
  19. expectType<number>(props.foo)
  20. emit('update', 123)
  21. // @ts-expect-error
  22. expectError(emit('nope'))
  23. // @ts-expect-error
  24. expectError(emit('update'))
  25. // @ts-expect-error
  26. expectError(emit('update', 'nope'))
  27. }
  28. // assigning runtime options
  29. Bar.props = {
  30. foo: Number
  31. }
  32. // @ts-expect-error
  33. expectError((Bar.props = { foo: String }))
  34. Bar.emits = {
  35. update: value => value > 1
  36. }
  37. // @ts-expect-error
  38. expectError((Bar.emits = { baz: () => void 0 }))
  39. // TSX
  40. expectType<JSX.Element>(<Bar foo={1} />)
  41. // @ts-expect-error
  42. expectError(<Foo />)
  43. // @ts-expect-error
  44. expectError(<Bar foo="bar" />)
  45. // @ts-expect-error
  46. expectError(<Foo baz="bar" />)