I've been delving into Typescript experimentation and I'm attempting to enhance the Object prototype by adding a property that can be accessed by all objects within my modules.
Here's what I've developed 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 want to implement it in another file like this:
module SomeModule{
class Engine{
constructor(public horsePower: number, public engineType: string){}
}
class Car{
private _engine: Engine;
constructor(private engine: Engine){
//Compiler error arises here due to the absence of notNull
this._engine = engine.notNull('engine');
}
}
}
I'm currently unsure about exporting "Object" with module.exports in Common.ts as it doesn't seem to serve any purpose when imported into other files. Is there a more effective way to achieve this?
Thank you.