我在typescript中定义了以下接口:
interface MyInterface {
() : string;
}
Run Code Online (Sandbox Code Playgroud)
该接口简单地引入了一个不带参数的调用签名并返回一个字符串.如何在类中实现此类型?我尝试过以下方法:
class MyType implements MyInterface {
function () : string {
return "Hello World.";
}
}
Run Code Online (Sandbox Code Playgroud)
编译器一直告诉我
类'MyType'声明接口'MyInterface'但没有实现它:Type'MyInterface'需要一个调用签名,但Type'MyType'缺少一个
如何实现呼叫签名?
我如何实现可索引的接口:
interface fooInterface{
// indexable
[index:string]:number;
[index:number]:number;
}
class Foo implements fooInterface{
// What goes here?
}
Run Code Online (Sandbox Code Playgroud) typescript ×2