E
E
Enma2022-03-30 01:24:25
typescript
Enma, 2022-03-30 01:24:25

How to access object property by string key in React JS + Typescript?

There is a checkbox

<Checkbox
    label="Is active?"
    checked={fields.settings.is_active}
    onChange={handleChange('is_active')}
/>


fields is of the following type
export type FormSettingsProps = {
    setting_id: Field<number>,
    icon_id: Field<number>,
    is_active: Field<boolean>
    ...
}


handleChange checkbox
const handleChange = (field: FormSettingsProps) => (value: boolean) => {
        fields.settings[field].onChange(value);
    }


this works, but the editor endlessly swears
"Type 'FormSettingsProps' cannot be used as an index type.ts(2538)"

what is wrong and how can I fix it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question