打字稿:此表达式不可调用。类型'{getUserInfo(requestData:object):Promise <object>; }'没有呼叫签名

时间:2020-03-30 06:14:10

标签: typescript asynchronous service

我在打字稿中调用另一个函数时遇到以下问题

该表达式不可调用。 类型'{getUserInfo(requestData:object):Promise; }'没有呼叫签名。 在我的index.ts中

index.ts

  const fetchApiData = await getUserInfo(requestData)

service.ts

import { userInfoApi } from '../constants/api'
import request from '../utils/request'

export default {
  async getUserInfo(requestData: object): Promise<object> {
    return await request(userInfoApi, requestData, 'GET')
  },
}

request.ts

const request = (operation: string, data: object, method: any): Promise<object> => {
  return new Promise(function(resolve, reject) {
    my.request({
      url: operation,
      data: data,
      method: method,
      success: function(res) {
        resolve(res)
      },
      fail: function(err) {
        reject(err)
      },
    })
  })
}

export default (operation: string, data: object, method: any): Promise<any> => {
  let timeHandle
  const timeout = 65 * 1000
  const promiseTimeout = new Promise(resolve => {
    timeHandle = setTimeout(() => {
      resolve({
        success: false,
        errorCode: 'NETWORK_TIMEOUT',
        errorMessage: 'Network Timeout',
      })
    }, timeout)
  })

  return Promise.race([
    request(operation, data, method).then(result => {
      clearTimeout(timeHandle)
      return result
    }),
    promiseTimeout,
  ])
}

有什么想法要解决吗?

2 个答案:

答案 0 :(得分:1)

该错误有效。

修复选项1:在通话时修复

基于您的错误Type '{ getUserInfo(requestData: object): Promise; }' has no call signatures,而不是致电something(requestData),您应该致电something.getUserInfo(requestData)

修复选项2:修复定义

更改:

export default {
  async getUserInfo(requestData: object): Promise<object> {
    return await request(userInfoApi, requestData, 'GET')
  },
}

export default async function getUserInfo(requestData: object): Promise<object> {
    return await request(userInfoApi, requestData, 'GET')
};

答案 1 :(得分:0)

更改为

service.ts

export default async getUserInfo(requestData: object): Promise<object> {
    return await request(userInfoApi, requestData, 'GET')
};

index.ts

import getUserInfo from 'service.ts'