This is my CommentResponse interface, which is the structure of an object inside the array from API response
interface CommentResponse {
body: string;
email: string;
id: number;
name: string;
postId: number;
}
While setting the data from the API response using setAllComments (useState-hook), I'm getting the following error
/typescript-demo/src/components/Dashboard/index.tsx
TypeScript error in /typescript-demo/src/components/Dashboard/index.tsx(35,20):
Argument of type 'Response' is not assignable to parameter of type 'SetStateAction<CommentResponse[]>'.
Type 'Response' is missing the following properties from type 'CommentResponse[]': length, pop, push, concat, and 28 more. TS2345
32 | comments = await comments.json();
33 | console.log(comments);
34 | // comments = await JSON.parse(comments);
> 35 | setAllComments(comments);
| ^
36 | };
37 |
38 | useEffect(() => {
@RajaJaganathan
const [allComments, setAllComments] = useState<CommentResponse[]>([]);
const commentService = async () => {
const COMMENTS_API = `https://jsonplaceholder.typicode.com/comments`;
let comments = await fetch<Array<CommentResponse>>(COMMENTS_API);
comments = await comments.json();
console.log(comments);
setAllComments(comments);
};
I visited other StackOverflow & Google links, but couldn't get the solution