【问题标题】:Typescript error when function return type is Promise<{ then: () => void }>函数返回类型为 Promise<{ then: () => void }> 时的打字稿错误
【发布时间】:2017-11-04 13:39:21
【问题描述】:

我不能声明函数,它返回承诺给Derivable。原因是下一个:

Minimal exmaple in playground

interface Some {
  then(callback);
}

// Error: the return type of an async function must either be a valid promise or must not contain a callable 'then' member.
async function foo(): Promise<Some> {
  return null;
}

这是打字稿中的错误吗?有任何解决方法吗?因此我不能使用 async/await。

【问题讨论】:

    标签: typescript asynchronous promise async-await


    【解决方案1】:

    不,这不是 Typescript 中的错误。只是fundamentally impossiblethenable (具有then 方法的对象)来履行承诺,因为它在解析时试图同化它。作为一种解决方法,您可以将Derivable 包装在一个额外的对象中,作为一个正确的解决方案,我建议将then 重命名为chain

    【讨论】:

      【解决方案2】:

      我认为问题在于 typescript 无法识别方法 then 的不同签名,因为您在界面中覆盖了它。

      【讨论】:

        猜你喜欢
        • 2021-08-24
        • 2018-01-04
        • 2022-01-12
        • 2015-08-10
        • 2020-07-08
        • 2022-07-07
        • 1970-01-01
        • 2015-06-12
        • 2021-03-17
        相关资源
        最近更新 更多