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

320
Views
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 answers
Answer question

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 Report
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!