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

333
Visualizações
Solid JS with Typescript - "as" prop \ "component" prop

So I'm trying to do the following thing with solid-js. But can't figure out the right way with typescript.

I want to modulate my styling through my components.

type RelevantTags = Exclude<keyof JSX.IntrinsicElements, 'script' | 'object' | 'style' | 'head'>;

type MustHaveProps = {
    class?: string;
}

type DynamicComponentProps<Props extends MustHaveProps> = Props & {
    as: Component<Props>
}

type DynamicHTMLTagProps<Tag extends RelevantTags> = JSX.IntrinsicElements[Tag] & {
    as: Tag;
}

type PossibleComponentTypes<Props extends MustHaveProps> = RelevantTags | Component<Props>;

type MyCustomDynamicComponentProps<ComponentType extends PossibleComponentTypes<MustHaveProps> = "div"> =  
ComponentType extends RelevantTags ? DynamicHTMLTagProps<ComponentType> :
DynamicComponentProps<ComponentProps<ComponentType>>;


function MyCustomComponent<C extends PossibleComponentTypes<MustHaveProps>>(props: MyCustomDynamicComponentProps<C>){
    return <Dynamic component={props.as} class={props.class} />
}

I tried doing this in a few different ways. I can't get typescript to work with the dynamic component props...

Would greatly appreciate some help.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are quite a few more tags, mostly SVG, declared in JSX.IntrinsicElements which do not accept class attribute. If you exclude them all, Typescript allows to access props.class on the last line

type RelevantTags = Exclude<keyof JSX.IntrinsicElements, 
  | 'script' | 'object' | 'style' | 'head' 
  | 'animate' | 'animateMotion' | 'animateTransform' | 'feDistantLight' 
  | 'feFuncA' | 'feFuncB' | 'feFuncG' | 'feFuncR' 
  | 'feMergeNode' | 'fePointLight' | 'feSpotLight' | 'metadata' | 'view'
>;
about 4 years ago · Juan Pablo Isaza Relatório
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