Skip to content

CodeNot attempted

Type challenge: polymorphic 'as' props

Difficulty: hard

Implement PolymorphicProps<E, Own> for a <Text as="a" href="…"> component: the native props follow the tag passed in as, and your own props replace native props with the same name (an input's numeric size must give way to 'sm' | 'md' | 'lg'). A small IntrinsicProps map stands in for JSX.IntrinsicElements.

Show reference solution
// No React here: this map stands in for JSX.IntrinsicElements (tag -> its native props).
export interface IntrinsicProps {
span: { title?: string };
a: { href?: string; target?: '_blank' | '_self' };
label: { htmlFor?: string };
input: { size?: number; placeholder?: string };
}
export type Tag = keyof IntrinsicProps;
export type TextOwnProps = { size?: 'sm' | 'md' | 'lg'; tone?: 'primary' | 'secondary' };
// Omit first: intersecting would intersect clashing props (input's `size: number` & 'sm' = never).
// In React: Own & { as?: E } & Omit<ComponentPropsWithoutRef<E>, keyof Own | 'as'>.
export type PolymorphicProps<E extends Tag, Own> = Own & { as?: E } & Omit<IntrinsicProps[E], keyof Own | 'as'>;
Loading editor…