Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type should I use for a React component with arbitrary properties?

I want to use a component like:

<toolProperties.ToolElement
                        onGenerate={onGenerate}
                        postData={postData}
                        status={status}
                        setStatus={setStatus}
                        setToolOutputs={setToolOutputs} />

I have toolProperties typed with:

interface IToolProperties {
    toolName: string;
    title: string;
    description: string;
    ToolElement: React.ComponentType
}

It gives a type error: Type '({ status, setStatus, setToolOutputs, onGenerate, postData }: IToolProps) => Element' is not assignable to type 'ComponentType<{}>'

Is there a better way to type it?

like image 456
Shamoon Avatar asked Jan 18 '26 22:01

Shamoon


1 Answers

You could make IToolProperties generic as well, e.g.:

interface IToolProperties<T> {
    toolName: string;
    title: string;
    description: string;
    ToolElement: React.ComponentType<T>
}

Then when you have a specific use for this interface, provide the props interface of your ToolElement:

const nameTools: IToolsProperties<{name: string}>

And then this could be used like this:

<nameTools.ToolElement name="foo" />
like image 109
thedude Avatar answered Jan 21 '26 12:01

thedude



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!