tsx-test.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { VNode, defineComponent, ref, RenderContext } from '../../index'
  2. import { expectType } from '../utils'
  3. expectType<VNode>(<div />)
  4. expectType<JSX.Element>(<div />)
  5. expectType<JSX.Element>(<div id="foo" />)
  6. expectType<JSX.Element>(<input value="foo" />)
  7. // @ts-expect-error style css property validation
  8. expectError(<div style={{ unknown: 123 }} />)
  9. // allow array styles and nested array styles
  10. expectType<JSX.Element>(<div style={[{ color: 'red' }]} />)
  11. expectType<JSX.Element>(
  12. <div style={[{ color: 'red' }, [{ fontSize: '1em' }]]} />
  13. )
  14. // @ts-expect-error unknown prop
  15. expectError(<div foo="bar" />)
  16. // allow key/ref on arbitrary element
  17. expectType<JSX.Element>(<div key="foo" />)
  18. expectType<JSX.Element>(<div ref="bar" />)
  19. // allow Ref type type on arbitrary element
  20. const fooRef = ref<HTMLElement>()
  21. expectType<JSX.Element>(<div ref={fooRef} />)
  22. expectType<JSX.Element>(
  23. <div
  24. ref={el => {
  25. fooRef.value = el as HTMLElement
  26. }}
  27. />
  28. )
  29. expectType<JSX.Element>(
  30. <input
  31. onInput={e => {
  32. // infer correct event type
  33. expectType<EventTarget | null>(e.target)
  34. }}
  35. />
  36. )
  37. const Foo = defineComponent({
  38. props: {
  39. foo: String,
  40. bar: {
  41. type: Number,
  42. required: true
  43. }
  44. }
  45. })
  46. // @ts-expect-error
  47. ;<Foo />
  48. // @ts-expect-error
  49. ;<Foo bar="1" />
  50. // @ts-expect-error
  51. ;<Foo bar={1} foo={2} />
  52. // working
  53. ;<Foo bar={1} />
  54. ;<Foo bar={1} foo="baz" />
  55. ;<div slot="x" />
  56. export default ({ data }: RenderContext) => {
  57. return <button {...data} />
  58. }