In trying to set a property on an object where the value is coming from a dynamically generated form, I’m using a for in loop to find a property in the object and set it
FormFeatureArray.forEach((el) => {
// form handling stuff omitted
For(const key in myObject){
If(key === el.feature){
myObject.key = formInputField.value
}
}
})
This of course throws ts(2339) to which I would like to be able cast key as a property of myObject, but I can’t know which property of the interface key is referring to, so is there a way to cast key as a guaranteed property of the interface of myObject, or override the warning for that line? Something like this is what I’m looking for
myObject.(key as aPropertyOfMyObject) = formInputField.value
The interface for myObject would be some like
Interface MyObjects {
Name: string
A: number
B: number
C: number
}
And key can be matching any of those properties of myObject, depending on the field of the form being selected, which is at runtime