I need to dynamically generate a TypeScript class and then add a property to it on the go.
The property could be of any type like a function, Promise, etc., and should use this
with the intention that it refers to the class itself.
let MyClass = class{
public property: any;
public ok = 1;
}
let property = function(){ console.log(this.ok); }
// The goal is to make `this` refer to MyClass rather than its function
// First attempt
MyClass.prototype.property= property;
// Second attempt: using bind()
// But there's an issue because property is not always a function
let propertyBind = property.bind(MyClass)
// Third attempt: adding property while creating the class
let MyClass = class{
public property = property ;
public ok =1;
}
If property
happens to be a function, we can't pass `this` as a parameter due to the constraint set by a third party for the class signature (without any parameters).
let property = (this)=>{ console.log(this.ok); }
``