gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:12:17 24 4
gpt4 key购买 nike

我是 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 的函数都必须返回一个必须返回字符串的函数。

但是什么年龄?:数字是给的。

最佳答案

用换行符和分号代替逗号更容易理解(我很感兴趣逗号是有效的,但根据操场,它是):

interface Foo {
(): () => string;
age?: number;
}
Foo为一个函数定义一个接口(interface):
  • 返回一个返回 string 的函数, 和
  • 有一个可选的 age属于 number 的属性.

  • 更多关于函数类型 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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com