Object
class where I add a recursive function of the Object.assign
method to an Object.prototype like this:
/**
* extende o método Object.assign usando recursividade sobre os objetos
* filhos nos argumentos que forem passados
*/
(Object.prototype as any).deepAssign = (...objects: object[]) => {
objects.reduce((base, next) => {
Object.keys(next).forEach(key =>
base[key] = typeof (next[key]) === 'object' && typeof (base[key]) === 'object' ?
this.deepAssign(base[key], next[key]) : next[key]
)
return base;
}, {})
}
The function is ok, it works and compiles but when trying to use it in my classes the IDE tells me that there is no method Object.deepAssign
so I tried to create in my file typings.d.ts > declaration of this method in the class of these forms:
interface ObjectConstructor {
deepAssign(...objects: object[]): object;
}
interface Object {
deepAssign(...objects: object[]): object;
}
declare class Object {
deepAssign(...objects: object[]): object;
}
The problem is that no matter how I make the declaration of it in typings.d.ts , my solution can not understand that the Object
class contains the deepAssign
method. I can only use the method if it is one of the 2 forms below (which is not readable and, in my view illogical ...):
(Object as any).deepAssign({},{})
Object['deepAssign']({},{})
How do I declare this class extension so that I can use my function within Typescript?