2👍
✅
You need a wrap of fetch function like this:
function api<T>(url: string): Promise<T> {
return fetch(url).then(response => {
if (!response.ok) {
throw new Error(response.statusText)
}
return response.json<T>()
})
}
api<Project >('v1/posts/1').then((projectData:Project) => {
console.log(title, message)
}).catch(error => {
/* show error message */
})
Source:stackexchange.com