繁体   English   中英

此表达式不可调用类型“字符串”没有调用签名

[英]This expression is not callable Type 'String' has no call signatures

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

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 功能组件中调用此 Function,Redux 如下所示:

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

您将setIssuerCallState键入为字符串。 而且您不能调用字符串。

你基本上是在做:

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

看起来这个 function 的正确类型是:

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

现在setIssuerCallState被键入为 function ,它采用单个参数,即带有message属性的 object 。

setIssuerCallState是 function,因此将其设置为字符串会给您错误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' });
    }
);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM