import React from 'react'; /* ------------------------------------------------------------------------------------------------- * Utility types * -----------------------------------------------------------------------------------------------*/ export type Merge = Omit & P2; export type MergeProps = P & Merge< E extends React.ElementType ? React.ComponentPropsWithRef : never, P >; /** * Infers the OwnProps if E is a ForwardRefExoticComponentWithAs */ export type OwnProps = E extends ForwardRefComponent ? P : {}; /** * Infers the JSX.IntrinsicElement if E is a ForwardRefExoticComponentWithAs */ export type IntrinsicElement = E extends ForwardRefComponent ? I : never; export type NarrowIntrinsic = E extends keyof JSX.IntrinsicElements ? E : never; /* ------------------------------------------------------------------------------------------------- * ForwardRefComponent * -----------------------------------------------------------------------------------------------*/ export interface ForwardRefComponent< IntrinsicElementString, OwnProps = {} /** * Extends original type to ensure built in React types play nice * with polymorphic components still e.g. `React.ElementRef` etc. */ > extends React.ForwardRefExoticComponent< MergeProps< IntrinsicElementString, OwnProps & { as?: IntrinsicElementString } > > { /** * When passing an `as` prop as a string, use this overload. * Merges original own props (without DOM props) and the inferred props * from `as` element with the own props taking precendence. * * We explicitly define a `JSX.IntrinsicElements` overload so that * events are typed for consumers. */ < As extends keyof JSX.IntrinsicElements = NarrowIntrinsic< IntrinsicElementString > >( props: MergeProps ): React.ReactElement | null; /** * When passing an `as` prop as a component, use this overload. * Merges original own props (without DOM props) and the inferred props * from `as` element with the own props taking precendence. * * We don't use `React.ComponentType` here as we get type errors * when consumers try to do inline `as` components. */ ( props: MergeProps ): React.ReactElement | null; }