Features
- Automatic code completion, based on the value of the
as
prop - Static type checking against the associated component’s inferred props
- HTML element name validation
Custom components like the previous one may utilize the package as shown below.
import type { PolymorphicPropsWithoutRef } from "react-polymorphic-types"; // An HTML tag or a different React component can be rendered by default export const HeadingDefaultElement = "h2"; // Component-specific props should be specified separately export type HeadingOwnProps = { color?: string; }; // Extend own props with others inherited from the underlying element type // Own props take precedence over the inherited ones export type HeadingProps< T extends React.ElementType = typeof HeadingDefaultElement > = PolymorphicPropsWithoutRef<HeadingOwnProps, T>; export function Heading< T extends React.ElementType = typeof HeadingDefaultElement >({ as, color, style, ...restProps }: HeadingProps<T>) { const Element: React.ElementType = as || HeadingDefaultElement; return <Element style={{ color, ...style }} {...restProps} />; }