functionalComponent.test-d.tsx 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { expectError, expectType } from 'tsd'
  2. import { FunctionalComponent } from './index'
  3. // simple function signature
  4. const Foo = (props: { foo: number }) => props.foo
  5. // TSX
  6. expectType<JSX.Element>(<Foo foo={1} />)
  7. // expectError(<Foo />) // tsd does not catch missing type errors
  8. expectError(<Foo foo="bar" />)
  9. expectError(<Foo baz="bar" />)
  10. // Explicit signature with props + emits
  11. const Bar: FunctionalComponent<
  12. { foo: number },
  13. { update: (value: number) => void }
  14. > = (props, { emit }) => {
  15. expectType<number>(props.foo)
  16. emit('update', 123)
  17. expectError(emit('nope'))
  18. expectError(emit('update'))
  19. expectError(emit('update', 'nope'))
  20. }
  21. // assigning runtime options
  22. Bar.props = {
  23. foo: Number
  24. }
  25. expectError((Bar.props = { foo: String }))
  26. Bar.emits = {
  27. update: value => value > 1
  28. }
  29. expectError((Bar.emits = { baz: () => void 0 }))
  30. // TSX
  31. expectType<JSX.Element>(<Bar foo={1} />)
  32. // expectError(<Foo />) // tsd does not catch missing type errors
  33. expectError(<Bar foo="bar" />)
  34. expectError(<Foo baz="bar" />)