I'm currently writing components using React & Typescript, and am coming up against an issue when it comes to tuple types (arrays with set types in set positions). Let's say for example I have a Component set up as follows:
interface Props {
tuple: [string, number];
}
const Component: React.FunctionComponent<Props> = ({ tuple }) => <div>Component</div>;
Component.propTypes = {
tuple: PropTypes.arrayOf([PropTypes.string, PropTypes.number]).isRequired,
};
This throws an error in Typescript, and understandably so because arrayOf
is not the same as a tuple, since in arrayOf
the string or number types can be anywhere within the array.
So to my question - is there any way of getting around using tuples with PropTypes? Either a solution using the existing types or some kind of custom function that will do the job. I've tried Googling but content in this area seems very thin on the ground, and the experiments I've attempted with my own custom PropTypes functions have been unsuccessful.