我希望能做到这一点

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');

10-06 04:06
查看更多