javascriptreactjstypescriptredux

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


In my Typescript Function, when I set the type annotation to String, I receive the error "This expression is not callable Type 'String' has no call signatures." as seen in the code below.

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' });
        }
    );

}

However, if I set the type to Any, then I have no issues. As seen in the code below. I'm still wrapping my head around TypeScript, so any feedback would be appreciated.

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' });
        }
    );

}

This Function is then being called in a React Functional Component, with Redux as seen in the following:

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

Solution

  • You typed setIssuerCallState as a string. And you cannot invoke a string.

    You are basically doing:

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

    It looks like the proper type for this function would be:

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

    Now setIssuerCallState is typed as a function that takes a single argument that is an object with a message property.