Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
How to appropriately type a component which forwards an _arbitrary_ ref?

So I want to have a component which takes an as prop, and from that creates an element. In code, this is simple enough:

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

I can derive the props for a given as using the following basic code:

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

However, what I'm having problems with is forwarding the ref.

I can do the following to convert the literal string into an HTMLElement

type InferElement<TElement> = TElement extends keyof HTMLElementTagNameMap
  ? HTMLElementTagNameMap[TElement]
  : TElement;

type DivElement = InferElement<'div'>; // HTMLDivElement

But I can't seem to get it to work when forwarding the ref:

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

How would I go about telling typescript how to interpret this correctly?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda