With the Reflect object, you can access any object and change it programmatically. This approach also fails "The element is implicitly of the type" any "because an expression of the type" string "cannot be used to index errors of the type" {} "".
class Cat { name: string age: number constructor(name: string, age: number){ this.name = name this.age = age } } function printObject(obj: any):void{ const keys = Object.keys(obj) const values = keys.map(key => '${key}: ${Reflect.get(obj,key)}') console.log(values) } const cat = new Cat("Fluffy", 5) const dog = { name: "Charlie", age: 12, weight: 20 } printObject(cat) printObject(dog)
( code on the playground )
source share