I have this simple generic React component where I won't always know which element type or props I'll be using and don't want to have to set up an interface for each potential prop.
Using TS v2.9.
The fact that I'm using React Native and not React shouldn't be relevant in this instance.
import React, { StatelessComponent as SC } from "react";
interface IGenComponentProps {
/**
* Component type
*/
ComponentName: string;
/**
* Test Id used for automation testing
*/
testId?: string;
/**
* I'd like to define this here to supress TS errors I'm getting
*/
remainderProps?: any;
}
const GenComponent:SC<IGenComponentProps> = ({ComponentName, testId, children, ...remainderProps}) => {
return (
<ComponentName id={testId} {...remainderProps}>
{children}
</ComponentName>
)
}
export default GenComponent;
This works great and as expected but I get TS errors when using the component like so:
<GenComponent
ComponentName={Image}
testId="test-image"
source={{uri: 'someImageSrc'}}
opacity={0.1}
/>
[ts] Property 'source' does not exist on type 'IntrinsicAttributes & IGenComponentProps & { children?: ReactNode; }'.
[ts] Property 'opacity' does not exist on type 'IntrinsicAttributes & IGenComponentProps & { children?: ReactNode; }'.
or:
<GenComponent
ComponentName={View}
testId="left-container"
accessibilityHint="left-container"
>
{ someContent }
</GenComponent>
[ts] Property 'accessibilityHint' does not exist on type 'IntrinsicAttributes & IGenComponentProps & { children?: ReactNode; }'.