We're creating a library of components for our internal applications. The old version of that library was written in JavaScript and JSX, thus we have declared propTypes for all the components for type-safety.
The newer version of the component library is written in TypeScript. For type-safety we used interfaces for PropTypes such as the following -
interface Props {
size?: 'small' | 'large';
color?: string;
text?: string;
}
export class MyComponent extends React.Component<Props, any> { ... }
This was working fine since the newer projects using those components were written in TypeScript too (We do publish d.ts file for all the component classes). Now we needed to update the component library in the old projects too. Since they were not using TypeScript, the type-safety were not working. They're still relying on the old propTypes way for type-safety.
//Usage of MyComponent
<MyComponent size={20} color={'red'} text={'HelloWorld'} />
//Size must be one of 'small' or 'large' not the numeric value.
So my question is, what are the ways of handling this issue of using TypeScript (and tsx) written components in a non-TypeScript project?
There're some ways I've identified -
- Using some parser for d.ts file
- Creating a HoC for all the components
- Writing propTypes along with interfaces for all the components manually.
Are there any other cleaner and less manual way?