I have object with interface and I would like to dynamically access values by property key.
const tmpUser: IUser = {
username: "test",
namespace: "test",
password: "test"
}
Object.keys(tmpUser).forEach(property=>{
// Error: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'IUser'.
console.log(tmpUser[property]);
})
// works
console.log(tmpUser["username"]);
// works
const someKey = "username";
console.log(tmpUser[someKey]);
also tmpUser.hasOwnProperty(property) doesn't help neither.
forEach(property =>, tryforEach((property: keyof IUser) =>Object.keys(tmpUser)is alreadystring[], soforEachexpects a callback whose first parameter isstring:Type 'string' is not assignable to type '"username" | "namespace" | "password"'.