When I attempt to set a property on an object with a value from a dynamically generated form, I utilize a for-in loop to identify a property in the object and assign it.
FormFeatureArray.forEach((el) => {
// form handling stuff omitted
For(const key in myObject){
If(key === el.feature){
myObject.key = formInputField.value
}
}
})
This leads to a ts(2339) error that I would like to resolve by casting key as a property of myObject. However, since I do not know which property of the interface key corresponds to, is there a way to cast key as a guaranteed property of the interface of myObject, or suppress the warning for that line? I am looking for something along these lines:
myObject.(key as aPropertyOfMyObject) = formInputField.value
The interface for myObject would be defined as follows:
Interface MyObjects {
Name: string
A: number
B: number
C: number
}
key could match any of those properties of myObject, depending on the field selected in the form at runtime.