Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
¿Cómo escribir correctamente un componente que reenvía una referencia _arbitrary_?

Así que quiero tener un componente que tome as apoyo, ya partir de eso crea un elemento. En código, esto es bastante simple:

 function Box({ as = 'div', children, ...props }) { return createElement(as, props, children); }

Puedo derivar los apoyos para un as usando el siguiente código básico:

 type Elements = JSX.IntrinsicElements; type ElementType = keyof Elements | ComponentType<any>; type ElementProps<TElement> = TElement extends keyof Elements ? Elements[TElement] : TElement extends ComponentType<infer TProps> ? TProps : never; type Props<TElement> = { readonly as: TElement; } & ElementProps<TElement>; type DivProps = Props<{as: 'div'}>; // this works

Sin embargo, con lo que tengo problemas es con el reenvío de la referencia .

Puedo hacer lo siguiente para convertir la cadena literal en un HTMLElement

 type InferElement<TElement> = TElement extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[TElement] : TElement; type DivElement = InferElement<'div'>; // HTMLDivElement

Pero parece que no puedo hacer que funcione cuando reenvío la referencia:

 function BaseBox<T extends ElementType>( { as = 'div', children, ...props }: Props<T>, ref: ForwardedRef<InferElement<T>> ) { return createElement(as, props, children); } const Box = forwardRef(BaseBox); // this doesn't work

¿Cómo haría para decirle a mecanografiado cómo interpretar esto correctamente?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!