I'm trying to figure out how to use type-safety provided by TypeScript with old plain constructor functions in JS. I have a very simple example, that looks straightforward, but I miss something and can't make it compile with TypeScript:
interface IMyService {
new(): IMyService //I'm not sure if this line should be here, I just trying to make it working...
doSomething(name: string): void
}
function MyService(this: IMyService): void {
let _name = ""
this.doSomething = (name) => {
_name = name
}
}
//The line below won't compile and it saying:
//"new" expression, whose target lacks a construct signature, implicitly has an "any" type
let service = new MyService();
service.setName("Test Name")
What I'm missing? I know the preferred way of using TypeScript is with "class", but in my case I would like to use simple constructor functions.