我是 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
公平地说,这是一个...有趣...界面。 :-)
关于javascript - 返回多个类型的函数,以逗号分隔。 Typescript 中的内容是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58672696/