Borislav Hadzhiev
Sat Mar 05 2022·2 min read
Photo by Alla Biriuchkova
The "This expression is not callable. Type 'String' has no call signatures"
TypeScript error occurs when we try to call a string
as a function or have
typed a function as a string
. To solve the error, make sure you're calling a
function and it is typed as a function.
Here is an example of how the error occurs.
const str = 'hello world'; // ⛔️ This expression is not callable. // Type 'String' has no call signatures.ts(2349) str();
The example above shows how trying to call a string
as a function causes the
error. The same is the case with any other type that is not callable.
You could be forgetting to access a built-in method on the string.
const str = 'hello world'; console.log(str.toUpperCase()); // 👉️ "HELLO WORLD"
Make sure to access any built-in methods as str.someMethod()
, and not str()
.
console.log
the thing you're trying to call.If you console.log
the value and it is a function, then maybe your typings are
incorrect.
Hover over the function you're trying to invoke and make sure it has a type of function.
function getFunc(): any { return (a: number, b: number) => { return a + b; }; } // 👇️ const result: string const result = getFunc() as string; // ⛔️ Error: This expression is not callable. // Type 'String' has no call signatures.ts(2349) result(50, 50);
The example above shows how somehow the function got assigned a different type (a string) somewhere in the codebase (most commonly caused by using type assertions).
result
variable stores a function, we have told TypeScript that the type of the stored value is a string, and strings have no call signatures.Here are some examples of how to type a function using a type alias or an interface.
// 👇️ using Type Alias (for only function) 👇️ type FunctionA = (a: number, b: number) => number; const funcA: FunctionA = (a, b) => a + b; console.log(funcA(25, 25)); // 👇️ Using interface (for only function) 👇️ interface FunctionB { (a: number, b: number): number; } const funcB: FunctionB = (a, b) => a + b; console.log(funcB(30, 30)); // 👇️ Type Alias with other members 👇️ type DateThings = { date: string; getTimestamp: (str: string) => number; }; const d: DateThings = { date: '2023-09-24', getTimestamp(str) { return new Date(str).getTime(); }, }; console.log(d.getTimestamp('2024-04-16')); // 👇️ Interface with other members 👇️ interface MathThings { sum: (a: number, b: number) => number; num: number; } const obj: MathThings = { num: 100, sum(a, b) { return a + b; }, }; console.log(obj.sum(45, 45));
The first two examples show how you would correctly type a function using a type alias or an interface that only represents a single function.
To solve the "This expression is not callable. Type 'String' has no call signatures" TypeScript error: