CódigoNão tentada
Desafio de tipos: props polimórficas 'as'
Dificuldade: difícil
Implemente PolymorphicProps<E, Own> para um componente <Text as="a" href="…">: as props nativas seguem a tag passada em as, e as suas próprias props substituem as props nativas com o mesmo nome (o size numérico de um input deve dar lugar a 'sm' | 'md' | 'lg'). Um pequeno mapa IntrinsicProps faz o papel de JSX.IntrinsicElements.
Ver solução de referência
// 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'>;
Ctrl Enter