0

I want to compare two class objects in typescript to see which properties have changed. I have tried this but get an error:

Element implicitly has an 'any' type because index expression is not of type 'number'.(7015)

class MyObject {
    prop1: string|undefined;
    prop2: string|undefined;
    prop3: string|undefined;

    static getValue(): number{
        return 1;
    }
}

let obj1: MyObject = {
    prop1: "prop1Val",
    prop2: "prop2Val",
    prop3: "prop3Val"
}

let obj2: MyObject = {
    prop1: "prop1Val",
    prop2: "prop2Changed",
    prop3: "prop3Changed"
}

let compObj1 = JSON.stringify(obj1);
let compObj2 = JSON.stringify(obj2);

let difference = Object.entries(compObj1).filter(([k, v], i) => compObj1[k] !== compObj2[k]);

In this example I want to try and return something like: [{"prop2": "prop2Changed"}, {"prop3": "prop3Changed"}]

  • Why do you stringify the objects? – Andrea Simone Costa Apr 04 '22 at 10:18
  • it was just something I tried because I was having this problem: https://stackoverflow.com/questions/65383113/object-keys-iteration-causing-typescript-error-element-implicitly-has-an-any –  Apr 04 '22 at 14:37

0 Answers0