Reputation: 1812
I am having following issue, when i call to another function in typescript
This expression is not callable. Type '{ getUserInfo(requestData: object): Promise; }' has no call signatures. in my 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,
])
}
Any idea how to fix it?
Upvotes: 30
Views: 109243
Reputation: 699
import sha512 from "js-sha512";
I had error This expression is not callable.
in code above.
I fix it with add brackets
import { sha512 } from "js-sha512";
Upvotes: 0
Reputation: 388
My case was:
import * as jwt_decode from 'jwt-decode';
Then I import it like this:
import jwt_decode from 'jwt-decode';
and Error is gone.
Upvotes: 3
Reputation: 3873
In my case this was happening on TS 4.5 with the new Node ES Modules support. Basically, modules written in CommonJs import as objects, and default exports are not supported. To access what's proably the default export, in my case a function, use the .default
property
Instead of
import chariot from 'EastIndiaCo'
I had to use:
import chariotPkg from 'EastIndiaCo'
const chariot = chariotPkg.default
To fix this upstream, the maintainer of EastIndiaCo should probably define package.json exports for modules.
Source: https://nodejs.org/api/packages.html#packages_exports
Upvotes: 13
Reputation: 276239
The error is valid.
Based on your error Type '{ getUserInfo(requestData: object): Promise; }' has no call signatures
instead of calling something(requestData)
you should be calling something.getUserInfo(requestData)
.
Change:
export default {
async getUserInfo(requestData: object): Promise<object> {
return await request(userInfoApi, requestData, 'GET')
},
}
to
export default async function getUserInfo(requestData: object): Promise<object> {
return await request(userInfoApi, requestData, 'GET')
};
Upvotes: 20
Reputation: 5529
Change to
service.ts
export default async getUserInfo(requestData: object): Promise<object> {
return await request(userInfoApi, requestData, 'GET')
};
index.ts
import getUserInfo from 'service.ts'
Upvotes: 1