functionalComponent.test-d.tsx 1.1 KB

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