Reputation: 97
I'm building my first TypeScript app, having come from a JavaScript background.
I'm still getting my head around declaring types, but I have this current issue coming back.
My code is below, and the current error I'm getting is:
Argument of type 'Promise<AxiosResponse<any, any>>' is not assignable to parameter of type 'SetStateAction'.
//interface File
export default interface InterfaceNameHere {
author?: string;
date?: number;
title?: string;
__v?: number;
_id?: string;
dataArray?: Array<IAnotherInterface>;
}
//component
const [returnedSearch, setReturnedSearch] = useState<InterfaceNameHere >({});
useEffect(() => {
axios
//NOTE: I have also tried .get<Promise<{data: InterfaceNameHere}>> and it still throws an error
.get<Promise<AxiosResponse>>(
`URLGOESHERE`
)
.then((res) => {
setReturnedSearch(res.data);
})
.catch((err) => {
//error handling here
});
}, []);
I previously had axios.get
, and of course it was working fine
But as I'm trying to declare the Type, it's not working anymore.
Upvotes: 0
Views: 1008
Reputation: 5386
You need to use the interface as the generic argument
axios.get<InterfaceNameHere>().then(...)
Upvotes: 3