函数返回类型为 Promise<{ then: () => void }> 时打字稿错误

Pav*_*vel 0 asynchronous promise async-await typescript

我不能声明函数,它返回对Derivable 的承诺。原因如下:

操场上的最小例子

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;
}
Run Code Online (Sandbox Code Playgroud)

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

Ber*_*rgi 5

不,这不是 Typescript 中的错误。这只是从根本不可能履行了承诺thenable(有对象then的方法),因为它试图解决的时候吸收它。作为一种变通方法,你可以包装Derivable在一个额外的对象,作为一个妥善的解决办法,我建议重命名thenchain