index.d.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import * as React from "react";
  2. export type Merge<P1 = {}, P2 = {}> = Omit<P1, keyof P2> & P2;
  3. /**
  4. * Infers the OwnProps if E is a ForwardRefExoticComponentWithAs
  5. */
  6. export type OwnProps<E> = E extends ForwardRefComponent<any, infer P> ? P : {};
  7. /**
  8. * Infers the JSX.IntrinsicElement if E is a ForwardRefExoticComponentWithAs
  9. */
  10. export type IntrinsicElement<E> = E extends ForwardRefComponent<infer I, any> ? I : never;
  11. type ForwardRefExoticComponent<E, OwnProps> = React.ForwardRefExoticComponent<Merge<E extends React.ElementType ? React.ComponentPropsWithRef<E> : never, OwnProps & {
  12. as?: E;
  13. }>>;
  14. export interface ForwardRefComponent<IntrinsicElementString, OwnProps = {}
  15. /**
  16. * Extends original type to ensure built in React types play nice
  17. * with polymorphic components still e.g. `React.ElementRef` etc.
  18. */
  19. > extends ForwardRefExoticComponent<IntrinsicElementString, OwnProps> {
  20. /**
  21. * When `as` prop is passed, use this overload.
  22. * Merges original own props (without DOM props) and the inferred props
  23. * from `as` element with the own props taking precendence.
  24. *
  25. * We explicitly avoid `React.ElementType` and manually narrow the prop types
  26. * so that events are typed when using JSX.IntrinsicElements.
  27. */
  28. <As = IntrinsicElementString>(props: As extends '' ? {
  29. as: keyof JSX.IntrinsicElements;
  30. } : As extends React.ComponentType<infer P> ? Merge<P, OwnProps & {
  31. as: As;
  32. }> : As extends keyof JSX.IntrinsicElements ? Merge<JSX.IntrinsicElements[As], OwnProps & {
  33. as: As;
  34. }> : never): React.ReactElement | null;
  35. }
  36. //# sourceMappingURL=index.d.ts.map