【问题标题】:This expression is not callable Type 'String' has no call signatures此表达式不可调用类型“字符串”没有调用签名
【发布时间】:2020-08-08 12:39:13
【问题描述】:

在我的 Typescript 函数中,当我将类型注释设置为 String 时,我收到错误“此表达式不可调用类型 'String' 没有调用签名。”如下代码所示。

function thing<T>(setThingState: string ){

    return Axios.request({
        method: 'get',
        url:'https://jsonplaceholder.typicode.com/todos/',//'https://jsonplaceholder.typicode.com/todos/'
    }).subscribe(
        response => {
            console.log(response);
            setThingState({ message: response.status });
        },
        error => {
            console.log(error);
            setThingState({ message: '404' });
        }
    );

}

但是,如果我将类型设置为任何,那么我没有问题。如下面的代码所示。我仍在研究 TypeScript,因此我们将不胜感激。

function thing<T>(setThingState: any ){

    return Axios.request({
        method: 'get',
        url:'https://jsonplaceholder.typicode.com/todos/',//'https://jsonplaceholder.typicode.com/todos/'
    }).subscribe(
        response => {
            console.log(response);
            setThingState({ message: response.status });
        },
        error => {
            console.log(error);
            setThingState({ message: '404' });
        }
    );

}

然后在 React 功能组件中调用此函数,Redux 如下所示:

const [thingState, setThingState] = useState({ message: ''});
    function testCall(){
        thing(setThingState);
    };

【问题讨论】:

  • 您尝试将其称为此处的函数:setIssuerCallState({ message: response.status }); 并在几行之后再次调用它。显然 TypeScript 不允许在 string 上这样做。如果它不是string,为什么要尝试将其注释为string
  • 你希望const a = "string"; a() 做什么?
  • @Thomas -- 谢谢,我明白我现在做错了什么。我猜我只是需要第二双眼睛。
  • 我没有不尊重的意思!我只是想通过你得到的错误来说明打字稿试图告诉你的内容。我们曾经都是初学者。您提出了一个非常明确的问题,我很乐意为您提供帮助:)

标签: javascript reactjs typescript redux


【解决方案1】:

您输入了setIssuerCallState 作为字符串。而且你不能调用一个字符串。

你基本上是在做:

const aString = "a string"
aString() // error

看起来这个函数的正确类型应该是:

function IssuerCall(setIssuerCallState: (newState: { message: string }) => void) {
   //...
}

现在setIssuerCallState 被键入为一个函数,该函数接受一个参数,该参数是一个具有message 属性的对象。

【讨论】:

  • 谢谢——为什么我需要“void”?我在 TypeScript 文档中看到“对将被忽略的回调使用返回类型 void”——所以我可以利用这个学习机会,你能向我解释一下为什么这样做吗?我没有忽略消息的价值。
  • void 是返回类型。 typescript 中的函数类型类似于(arg1: Type) =&gt; ReturnType。所以箭头后面的void 表示你不在乎setIssuerCallState 函数返回 是什么。我假设它是void,因为您没有分配或使用函数的返回值。通常,对于更改状态的函数(例如来自 React),它们不会返回值。这看起来像那种功能。 Read the docs on function types for more info
  • 太棒了。谢谢你的解释!
【解决方案2】:

setIssuerCallState 是一个函数,因此将其设置为字符串会给出错误 String' has no call signatures。

尝试将 setIssuerCallState 的类型设置为 setIssuerCallState: (param: any) => void

function IssuerCall<T>(setIssuerCallState: (param: any) => void ){

return Axios.request({
    method: 'get',
    url:'https://jsonplaceholder.typicode.com/todos/',//'https://jsonplaceholder.typicode.com/todos/'
}).subscribe(
    response => {
        console.log(response);
        setIssuerCallState({ message: response.status });
    },
    error => {
        console.log(error);
        setIssuerCallState({ message: '404' });
    }
);
}

【讨论】:

    猜你喜欢
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 2020-07-10
    • 2020-09-09
    相关资源
    最近更新 更多