【问题标题】:TypeScript Error: Cannot invoke an expression whose type lacks a call signature. Type 'Something' has no compatible call signaturesTypeScript 错误:无法调用其类型缺少调用签名的表达式。类型“某事”没有兼容的呼叫签名
【发布时间】:2022-01-25 16:59:13
【问题描述】:

过去几天我一直被这个打字稿错误困扰。

Cannot invoke an expression whose type lacks a call signature. Type '(<TResult1 = ApiResponse, TResult2 = never>(onfulfilled?: ((value: ApiResponse) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<...>) | null | undefined) => Promise<...>) | (<TResult1 = void, TResult2 = never>(onfulfilled?: ((va...' has no compatible call signatures.

代码:

const getDetailsPromise = myCode
              ? storeApi.getDetails(params, [myCode])
              : Promise.resolve();
return getDetailsPromise.then(
              (result: ApiResponse | void) => {
                .......Some logic
                .......Some logic
                return Promise.resolve<
                  [MethodResponse, ResponseHeaders]
                >([ShippingResponse, response.headers]);
              }
            );

storeApi.js

getDetails(
        requestParams: RequestParams,
        myCode: string[]
    ): Promise<ApiResponse> { 
        .......Some logic
        .......Some logic
    }

这里 getDetailsPromise 返回 Promise 或 Promise。如果值存在,我们也期望 myCode 类型为字符串。我在尝试构建应用程序时遇到上述错误。有人可以帮助/指导我解决这个问题吗?任何帮助将不胜感激。

游乐场链接:https://www.typescriptlang.org/play?ts=3.3.3#code/FASwdgLgpgTgZgQwMZQAQEEAOIBKUDOmA9mPmgN7CrWpgIC2UAXKvhDOAObAC+wwSEm1ScoEACJiEIADb5UAXlQAKQQBNmrdlwDaAXQCUTAAowi9EGQA8WXAWKkoAPkUvKNVDDEBXGGFSm5pZQAHRe+EQyAG5Qyu4eNHSMLADkAFIIANZQKQA0VAnU6prFBdQ8BrwA3PyCpBCo9ACeAMJEGoqoKQCMAAy93Sk1BXXCohJSsviBFmSdzW0aZYUJAPwiYpIQ0nLKOgvtUIbLKzQsM8FhBJExygbDow0AFlAyMkSdd66o8dReEL5-OMtjtpmZZqEIC8wMoTqcVOFvDIICxbHhCEI0AAfVBRIggNQGb6-eEef6AgLgy7hG6xRHI+5w058UnURl8YAvN5EO7DYBAA

提前致谢!

【问题讨论】:

  • 我怀疑您需要在 : Promise.resolve() 中指明正确的泛型类型
  • @Anatoly 你能解释一下吗?这里 getDetailsPromise 返回 Promise |承诺
  • 哦,等等。你能在这里创建一个最小的可重现示例吗:typescriptlang.org/…
  • @Anatoly 请在此处找到操场链接。另请注意,我们使用的打字稿版本有点旧 v3.0.1。但我们仍然可以重现该问题。
  • @Anatoly 游乐场链接 tinyurl.com/errortypescript

标签: node.js typescript promise es6-promise


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 2020-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
  • 2017-10-11
相关资源
最近更新 更多