Reactjs TS2349:无法调用其类型缺少调用签名的表达式
我知道其他帖子也提到了这个问题,但我仍然不知道如何做到这一点——我得到了一个错误:Reactjs TS2349:无法调用其类型缺少调用签名的表达式,reactjs,typescript,Reactjs,Typescript,我知道其他帖子也提到了这个问题,但我仍然不知道如何做到这一点——我得到了一个错误: TS2349:无法调用其类型缺少调用签名的表达式。类型“string | Function”没有兼容的调用签名。 控制台记录了一个函数,但我不能调用它 提前感谢您的帮助。这是我的密码: const func = (): string => 'hi'; const array: (string | Function)[][] = [['string', func]]; const response = a
TS2349:无法调用其类型缺少调用签名的表达式。类型“string | Function”没有兼容的调用签名。
控制台记录了一个函数,但我不能调用它
提前感谢您的帮助。这是我的密码:
const func = (): string => 'hi';
const array: (string | Function)[][] = [['string', func]];
const response = array[0][1];
console.log(response); // ƒ () { return 'hi'; }
response(); // TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'string | Function' has no compatible call signatures
编译器无法确定
数组
中包含的元素是否真的是一个函数,因为您已将其声明为unionstring |函数
要解决这个问题,可以将数组
声明为嵌套元组[string,Function][]
const func = (): string => 'hi';
const array: [string, Function][] = [['string', func]];
const response = array[0][1];
response();
或者使用
const func = (): string => 'hi';
const array: (string | Function)[][] = [['string', func]];
const response = array[0][1];
if (typeof response === "function") {
response()
}
编译器无法确定
数组中包含的元素是否真的是函数,正如您将其声明为union字符串|函数一样
要解决这个问题,可以将数组
声明为嵌套元组[string,Function][]
const func = (): string => 'hi';
const array: [string, Function][] = [['string', func]];
const response = array[0][1];
response();
或者使用
const func = (): string => 'hi';
const array: (string | Function)[][] = [['string', func]];
const response = array[0][1];
if (typeof response === "function") {
response()
}