Is it possible to use a string
as an object index? Although using keyof
is the recommended approach due to its specificity, I am curious why using a string
is not allowed even after confirming it is a valid index with the in
operator.
For example:
if (fieldName in someObject) {
const fieldValue = someObject[fieldName];
}
Typescript raises an error when attempting this. While I understand that determining the type of fieldValue
may be challenging, assigning it an any
type could suffice in such cases.