I'm trying to implement a function without name like this:
interface I {
(name: string): void;
}
class C implements I {
(name: string):void { }
}
I want to use C like this, but it doesn't work:
C("test");
I can write it in javascript and use the interface declaration: I("test");
But I want to do the same in Typescript.
You can't do that on classes, but you can do it with a regular function:
interface I {
(name: string): void;
}
var C: I = function(name: string) {
};
C("test"); // ok
C(1); // compile error
Then if you change your interface you will be notified by a compile error to change the C
function.
Classes can't have call signatures in TypeScript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With