I have a class B
which extends A
. I am trying to copy A
onto B
(since B
has all the same properties (and more) as A
).
Example:
class TypeA {
propX: number = 0;
propY: number = 0;
}
class TypeB extends TypeA {
propZ: number = 0;
}
let A: TypeA = {propX: 1, propY: 2};
let B: TypeB = new TypeB();
//Here I want to copy A's properties onto B
Object.keys(A).forEach(prop => B[prop] = A[prop]); //this how we did it on older version of typescript, still works in this example but won't compile in my new project
//now set B's non-shared property
B.propZ = 3;
//desired output {propX: 1, propY: 2, propZ: 3}
console.log(B);
That Ojbect.keys(A) ...
line is how we did it on a project with an earlier TS version, but now it won't compile. In fact, in this TS fiddle it will successfully run with desired results. However, there are errors on that line. And in my Angular project, it simply won't compile at all.
How can/should I do this now?
Also, yes I did look at this similar question, but did not find a working solution. The accepted "solution" there does not appear correct to me, despite not understanding it, I tried implementing it to my example with:
let key: keyof TypeA;
for (key in A) {
A = {
...A,
[key]: B[key]
}
}
That really doesn't make much sense to me, but I tried it anyway before posting