type ObjectDescriptor<D, M> = {
data?: D;
methods?: M & ThisType<D & M>; // Type of 'this' in methods is D & M
};
function makeObject<D, M>(desc: ObjectDescriptor<D, M>): D & M {
let data: object = desc.data || {};
let methods: object = desc.methods || {};
return { ...data, ...methods } as D & M;
}
let obj123 = makeObject({
data: {
x: 20,
y: 50,
moveBy(dx: number, dy: number) {
this.x += dx; // Strongly typed this
this.y += dy; // Strongly typed this
},
},
methods: { x: 0, y: 0 },
});
console.dir(obj123);
const nemObj = { method: obj123.moveBy };
nemObj.method(5, 5);
# console.dir(nemObj); // here newObj also getting two extra property x and y but how????
I created an Object called obj123 using the function called makeObject() and then I am assigning the method of this object to another Object called nemObj but this nemObj also getting the x and y property from the previous object. How is this happening?