I want to convert the JavaScript code that I wrote in TypeScript. I am more familiar with TypeScript syntax and thinking as a JavaScript developer.
What gives me a headache is a difficult time when I had to convert some piece of code that uses a drop-down module template in TypeScript.
The following is an example:
var obj;
(function(){
function myFunction(){
}
function MyOtherConstructor(){
return {
publicMethod: myFunction
}
}
obj = new MyOtherConstructor();
})();
One workaround I thought:
var obj;
class MyOtherConstructor {
private callback: any;
constructor(f: any){
this.callback = f;
}
publicMethod(): any{
this.callback();
}
}
(() => {
function myFunction(){
console.log("Called myFunction");
}
obj = new MyOtherConstructor(myFunction);
})();
which works, but it is ugly.
Any suggestion on how to make this better?
source
share