In javascript, accessing object value via obj.field can be done by obj['field'] too. But produces error in typescript.
function F() {
const obj = { field: "firstname", title: "First Name", value: 10 };
let field = "field"
console.log(obj[field]); // <-- Typescript error: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ field: string; title: string; value: number; }'
}
How can I cast obj to resolve typescript error?
console.log(obj[field as keyof typeof obj] )