【问题标题】:Are these interfaces the same这些接口是否相同
【发布时间】:2020-11-08 18:58:12
【问题描述】:

我仍在努力掌握在 typesecript 中编写相同内容的多种方式。 但是这个我不确定它们是否相同,但我得到了相同的结果,所以想知道是否存在我没​​有得到的细微差别,或者只是打字稿混乱。

第一种方式:

export default interface IApiService {
  send: (url: string) => Promise<string>;
}

接口的第二种写法

export default interface IApiService {
  send(url: string): Promise<string>;
}

我的函数本身。

  public send = async (url: string) => {
    return fetch(url).then((response) => {
      return response.json();
    });
  };

【问题讨论】:

    标签: javascript typescript


    【解决方案1】:

    是的,您可以在参数和返回类型之间使用=&gt;: 来声明函数的类型——它们的含义完全相同。尽管在 JavaScript 中,箭头函数与标准函数或方法有很大不同,但对于 TypeScript,函数的类型可以任意定义。

    另外,请注意,由于send 方法不使用await,您可以从其定义中删除async

    【讨论】:

      猜你喜欢
      • 2019-06-05
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      相关资源
      最近更新 更多