As someone new to TypeScript, I have a curious question about classes.
In pre-ES6 JavaScript, there were no classes. So, naturally, one would think it's possible to avoid using them in TypeScript as well.
However, I am struggling to figure out the correct approach. Consider this sample class:
class FooConcrete {
getImpl : () => number;
constructor(getImpl: () => number) {
this.getImpl = getImpl;
}
get() : number {
return this.getImpl();
}
}
When looking at typical type definition files, they expose something like this:
interface FooConcrete
{
get() : number;
}
interface FooConcreteStatic
{
new () : FooConcrete;
}
Now, how do I define the FooConcrete
constructor function?
I attempted:
var FooConcrete : FooConcreteStatic = function FooConcrete() {
return {
get: () => 42
};
};
Unfortunately, that doesn't compile, even though the corresponding JavaScript code functions correctly:
new FooConcrete().get() // => 42
Is there a way to declare FooConcrete
as FooConcreteStatic
without using a class?
(My curiosity about this stemmed from wondering if it's possible to eliminate the unnecessary redirection in the class from the prototype's get
to the implementation getImpl
- in this scenario, a get
in a prototype is not needed.)