1
votes

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);
    };
2
You're trying to call it as if it is a function here: setIssuerCallState({ message: response.status }); and again a few lines later. Obviously TypeScript won't allow that on a string. Why are you trying to annotate it as string if it's not a string?Thomas
What would you expect const a = "string"; a() to do?Alex Wayne
@AlexWayne -- I understand that your snarky response scenario is wrong. I'm learning... hence the post...bort
@Thomas -- Thank you, I understand what I'm doing wrong now. I just needed that second pair of eyes, I guess.bort
I meant no disrespect! I was merely trying to illustrate the what typescript is trying to tell you by the error you are getting. We were all beginners once. You've asked a very clear question that I am happy to help you with :)Alex Wayne

2 Answers

1
votes

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.

0
votes

setIssuerCallState is a function, so setting it as string will give you the error String' has no call signatures.

Try setting the type of setIssuerCallState as 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' });
    }
);
}