我希望能做到这一点
class MyFunc extends ((s: string) => boolean) { ... }
这样
MyFunc
的一个实例就可以用作一个函数,它接受一个字符串作为输入并返回一个布尔值,如下所示:const f = new MyFunc();
const b: boolean = f('someString');
这可能是打字稿吗?
在Scala等语言中,可以扩展类型
String => Boolean
,并提供一个apply
方法来实现这一点。class MyFunc extends (String => Boolean)
val f = new MyFunc()
val b: Boolean = f("someString")
最佳答案
也许你在想这样的事?
interface FunctionInterface {
(s: string): boolean;
}
const f: FunctionInterface = s => true;
const b: boolean = f('someString');