javascript - 返回多个类型的函数,以逗号分隔。 Typescript 中的内容是什么?

标签 javascript typescript

我是 Typescript 新手,我遇到了以下语法

interface Foo {

    // what is age is here and 
    // what if it is not optional i.e () => string, age:number;

    (): () => string, age?: number;
}

我理解的Foo是一个函数接口(interface),任何实现Foo的函数都必须返回一个必须返回字符串的函数。

但是什么年龄?:号码适用于。

最佳答案

用换行符和分号代替逗号更容易理解(我很好奇逗号是有效的,但根据 Playground ,它是有效的):

interface Foo {
    (): () => string;
    age?: number;
}

Foo 定义了一个函数的接口(interface):

  • 返回一个返回字符串的函数,并且

  • 有一个可选的age属性,它是一个数字

有关函数类型的更多信息 here .

这是一个使用中的示例:

interface Foo {
    (): () => string,
    age?: number;
}

const f: Foo = () => () => "foo";
f.age = 42; // Note this is on the function

Playground link

公平地说,这是一个...有趣...界面。 :-)

关于javascript - 返回多个类型的函数,以逗号分隔。 Typescript 中的内容是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58672696/

相关文章:

Angular 2 : Pass a function to the ngClass tag on the template

typescript - 如何在 webpack 中使用 underscore.js

typescript - MVVM 模式 - 公共(public)方法和属性仅适用于 View

javascript - 在 jQuery 中使用正则表达式检查模式

javascript - 如何自动生成域和范围?有人知道为什么我的数据没有显示吗?

javascript - 让 ng-repeat 在 AngularJS 的 $interpolate 服务中工作

javascript - 如何以编程方式更改 mxCell 值?

javascript - 在 mailto : string not working as expected 中使用 es6 模板文字

javascript - 将对象传递到选择框中的 ng-model

javascript - 如何使用 javascript 将必填字段设置为 false