I have a react component, which is a form, and the input IDs need to be passed to its' props. It also accepts a submit handler, which should get an object with the same keys as the passed inputs' IDs.
Here's an example:
const inputs = [
{ id: 'first' },
{ id: 'second' },
];
//the parameter's keys should match the inputs' IDs
type ExpectedValues = {
first: string;
second: string;
};
const handleSubmit = (valuesById: ExpectedValues) => {};
const ExampleForm = () => (
<Form
inputs={inputs}
onSubmit={handleSubmit}
//Type '{ [inputId: string]: string; }' is missing the following properties
//from type 'ExpectedValues': first, second
/>
);
type Input = {
id: string;
};
/*
some magic would be needed here in the types to check the inputs IDs,
and onSubmit should get those as keys in the object
*/
type Props = {
inputs: Input[];
onSubmit: (values: { [inputId: string]: string; })
}
const Form: FC<Props> = props => null;