The code examples in this answer assume the following declaration:
var implementation: MyInterface;
Providing an implementation of a callable interface
As a follow-up to the accepted answer, as suggested by some of its commentors, a function that matches the interface's call signature implicitly implements the interface. So you can use any matching function as an implementation.
For example:
implementation = () => "Hello";
You don't need to explicitly specify that the function implements the interface. However, if you want to be explicit, you can use a cast:
implementation = <MyInterface>() => "Hello";
Providing a reusable implementation
If you want to produce a reusable implementation of the interface like you normally would with a Java or C# interface, just store the function somewhere accessible to its consumers.
For example:
function Greet() {
return "Hello";
}
implementation = Greet;
Providing a parameterised implementation
You may want to be able to parameterise the implementation in the same way that you might parameterise a class. Here's one way to do this:
function MakeGreeter(greeting: string) {
return () => greeting;
}
implementation = MakeGreeter("Hello");
If you want the result to be typed as the interface, just explicitly set the return type or cast the value being returned.