I am doing some experimentation with Typescript and I am trying to add a property to the Object prototype so that it is available to all objects in all of my modules.
Here is what I have so far:
In a Common.ts file
Object.defineProperty(Object.prototype, 'notNull', {
value: function(name: string){
if(this === null || this === undefined){
throw new Error(`${name} cannot be null nor undefined`);
}
return this;
},
enumerable: false
});
Now I would like to use it in another file like so:
module SomeModule{
class Engine{
constructor(public horsePower: number, public engineType: string){}
}
class Car{
private _engine: Engine;
constructor(private engine: Engine){
//Of course here the compiler complains about notNull not existing
this._engine = engine.notNull('engine');
}
}
}
Now I am at a loss since I am not sure that by exporting "Object" with module.exports in Common.ts makes sense at all. And even if I do that and import it on my other file, that does not seem to do anything.
Is there a way to implement something like this?
Thank you.