import { ComponentProps, JSX, Context } from 'preact'; import { ForwardFn } from 'preact/compat'; type ElementType = JSX.ElementType; /** * Extract the props of a React element or component */ type PropsOf = ComponentProps & { as?: ElementType; }; type DistributiveOmit = T extends any ? Omit : never; /** * Assign property types from right to left. * Think `Object.assign` for types. * * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign */ type Assign = DistributiveOmit & B; type MergeWithAs, DefaultProps extends Record, ComponentProps extends Record> = /** * The following code is copied from the library react-polymorphed by nasheomirro. * Thank your for creating this TypeScript gold! * * doing this makes sure typescript infers events. Without the * extends check typescript won't do an additional inference phase, * but somehow we can trick typescript into doing so. Note that the check needs to be relating * to the generic for this to work. */ any extends Component ? Assign | Assign : never; type ComponentWithAs = Record> = { (props: MergeWithAs, ComponentProps>): JSX.Element; displayName?: string; contextTypes?: Context; defaultProps?: Partial; id?: string; }; declare function forwardRef(component: ForwardFn, Props> & { as?: ElementType; }>): ComponentWithAs; type DOMElements = keyof JSX.IntrinsicElements; type HTMLPolymorphicComponents = Record> = { [Tag in DOMElements]: ComponentWithAs; }; type HTMLPolymorphicProps = Omit, 'ref'> & { as?: ElementType; }; type PolymorphFactory = Record, Options = never> = { = Props>(component: T, option?: Options): ComponentWithAs; }; interface PolyFactoryParam, Options> { styled?: (component: Component, options?: Options) => ComponentWithAs; } /** * Create a polymorphic factory, which is an object of JSX elements to render React Components accepting the `as` prop. * * @example * const poly = polymorphicFactory() * // => renders div * // => renders main * => // renders main */ declare function polymorphicFactory, Options = never, Component extends ElementType = ElementType>({ styled }?: PolyFactoryParam): PolymorphFactory & HTMLPolymorphicComponents; export { Assign, ComponentWithAs, HTMLPolymorphicComponents, HTMLPolymorphicProps, PropsOf, forwardRef, polymorphicFactory };