Skip to content

Instantly share code, notes, and snippets.

@robinweser
Last active April 23, 2024 07:01
Show Gist options
  • Save robinweser/9d99fbddce57cd703cec3701de02d84b to your computer and use it in GitHub Desktop.
Save robinweser/9d99fbddce57cd703cec3701de02d84b to your computer and use it in GitHub Desktop.
// this is exported by "some-package"
export default function El({ as: Component = "div", ...props }) {
return <Component {...props} />
}
import El from "some-package"
function Foo(props: { foo: string }) {
return <div>{props.foo}</div>
}
const test = () => [
<El as="div" className="foo" bar="baz" />,
<El as={Foo} foo="bar" bar="baz" />,
]
export interface PolymorphicProps<
T extends keyof JSX.IntrinsicElements | React.ComponentType<any>,
> {
as: T
}
type ComponentProps<T> = T extends keyof JSX.IntrinsicElements
? JSX.IntrinsicElements[T]
: T extends React.ComponentType<infer P>
? P
: never
declare module 'some-package' {
export function El<
T extends keyof JSX.IntrinsicElements | React.ComponentType<any>,
>(props: PolymorphicProps<T> & ComponentProps<T>): JSX.Element
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment