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);
};
setIssuerCallState({ message: response.status });
and again a few lines later. Obviously TypeScript won't allow that on astring
. Why are you trying to annotate it asstring
if it's not astring
? – Chromeconst a = "string"; a()
to do? – Diagnostician