I have a interface :
interface MyInterface {
field_1 : string
field_2 : string
// ... other fields with a certain number
}
And I have a getter function that receives an instance of MyInterface and should retrieve a value of an attribute of this object dynamically
const myFactory = (myInstance:MyInterface):string => {
// Here some logic to retrieve a specific number
// for convenience lets say that the output of this logic is 1
let output = 1
return myInstance[`field_${output}`] // <=== This does not work since it calls a property of the interface dynmically
}
How can I call a property of an interface dynamically ?
I saw the keyof
operator :
but I am not sure how to use it here