man*_*nas 3 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;
}
Run Code Online (Sandbox Code Playgroud)
我所了解的Foo是一个函数接口,实现Foo的任何函数都必须返回一个必须返回字符串的函数。
但是年龄是多少?
用换行符和分号代替逗号更容易理解(我很感兴趣逗号是有效的,但是根据操场的说法,是这样):
interface Foo {
(): () => string;
age?: number;
}
Run Code Online (Sandbox Code Playgroud)
Foo 为以下功能定义接口:
返回一个函数,该函数返回一个string和
有一个可选age属性,即number。
更多关于函数类型的信息。
这是一个正在使用的示例:
interface Foo {
(): () => string,
age?: number;
}
const f: Foo = () => () => "foo";
f.age = 42; // Note this is on the function
Run Code Online (Sandbox Code Playgroud)
可以说这是一个有趣的界面。:-)
| 归档时间: |
|
| 查看次数: |
41 次 |
| 最近记录: |