Can someone help me with understanding interfaces in typescript? I am currently facing an issue with the code.
The error message says: Type 'Response' is missing the following properties from type 'myObj': userId, title, id
I believe that the fetchDetails
function should return a value of type Promise<myObj>
, but since the type of data
is Response
, it is causing the error. I want to find a solution without adding any additional variables within the function. How can I make sure the required response is returned?
interface myObj {
userId:number,
title:string,
id:string,
body:string
}[]
async function fetchDetails(url:string):Promise<myObj>{
let data:Response = await fetch(url);
data = await data.json();
return data;
}
fetchDetails('https://jsonplaceholder.typicode.com/posts')