是否可以在 TypeScript 中实现函数接口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42840466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:21:25  来源:igfitidea点击:

Is it possible to implement a function interface in TypeScript?

typescript

提问by Yuhuan Jiang

I would like to be able to do this

我希望能够做到这一点

class MyFunc extends ((s: string) => boolean) { ... }

so that an instance of MyFunccan be used as a function which takes a string as input and returns a boolean as follows:

这样一个 的实例MyFunc就可以用作一个函数,它接受一个字符串作为输入并返回一个布尔值,如下所示:

const f = new MyFunc();
const b: boolean = f('someString');

Is this possible in TypeScript?

这在 TypeScript 中可能吗?

In languages such as Scala, one can extend the type String => Boolean, and provide an applymethod to achieve this.

在 Scala 等语言中,可以扩展 type String => Boolean,并提供一种apply方法来实现这一点。

class MyFunc extends (String => Boolean)
val f = new MyFunc()
val b: Boolean = f("someString")

回答by David Sherret

Perhaps you are thinking of something like this?

也许你正在考虑这样的事情?

interface FunctionInterface {
    (s: string): boolean;
}

const f: FunctionInterface = s => true;
const b: boolean = f('someString');

回答by y2bd

There is no default applyconcept in TypeScript, but there are ways of creating typed objects that are also functions.

applyTypeScript 中没有默认概念,但是有一些方法可以创建也是函数的类型化对象。

interface MyCallable {
    (param1: string, param2: number): string;

    prop1: string;
    prop2: number;
    extraMethod: (param1: boolean) => boolean;
}

function makeMyCallable(prop1: string, prop2: number): MyCallable {
    let that = ((param1: string, param2: number) => param1 + param2) as MyCallable;

    that.prop1 = prop1;
    that.prop2 = prop2;
    that.extraMethod = (param1: boolean) => !param1;

    return that;
}

let mc = makeMyCallable("3", 4);

mc("3", 4);
mc.prop1 = "string";
mc.prop2 = 5;
mc.extraMethod(false);

回答by CBarr

If all you want is a simple function to accept a string and return a boolean, you don't need to use a class for this.

如果你想要的只是一个简单的函数来接受一个字符串并返回一个布尔值,那么你不需要为此使用一个类。

const myFunc = (s: string): boolean => {
  return s !== "";  //whatever logic is needed here
};

But, yes you can extend classes in TypeScript

但是,是的,您可以在 TypeScript 中扩展类

class MyBaseClass {
  constructor() { }

  public doThing = (s: string): boolean => {
    return s !== "";  //whatever logic is needed here
  }
}

class MyFunc extends MyBaseClass {
  constructor() {
    super();
  }
}

const f = new MyFunc();
const b = f.doThing("hi"); //returns a boolean


Updated for response to comment

更新以回应评论

As mentioned below in another answer, you cannot really newup a class, assign that instance to a variable and then call it as a function. You could do that on the creation though, like this:

正如下面在另一个答案中提到的,您不能真正new提升一个类,将该实例分配给一个变量,然后将其作为函数调用。不过,您可以在创建时执行此操作,如下所示:

class MyBaseClass {
  constructor() { }

  public doThing = (s: string): boolean => {
    return s !== "";  //whatever logic is needed here
  }
}

class MyFunc extends MyBaseClass {
  constructor(private s: string) {
    super();
    this.doThing(this.s);
  }
}

const f = new MyFunc("hi"); //returns a boolean

You can play with the above code on the Typescript playground here

您可以在此处的 Typescript 操场上使用上述代码

回答by Christoph

If you only have this function in your interface, you can define it like

如果你的界面中只有这个功能,你可以像这样定义它

type FuncType = (s: string) => boolean;

to use it like

像使用它一样

const myFunc: FuncType;
function getFunction(): FuncType {}