I'm stuck with a function that is supposed to fetch data from an API and then return an array of a predefined type. However, I would always get an error message indicating that A function whose declared type is neither 'void' nor 'any' must return a value.
I don't really understand why this message shows up since I have a return-statement in my function, even if an error occurs while fetching the data:
export async function queryOpenCage(query: string): Promise<MarkerPoint[]> {
const {opencageApiKey}: ClientSettings = JSON.parse(
document.getElementById(ElementID.Settings)!.textContent!
)
const fetchURL = `https://api.opencagedata.com/geocode/v1/json?key=${opencageApiKey}&q=${query}&limit=5&pretty=1`
const resultItems: MarkerPoint[] = []
fetch(fetchURL)
.then(response => {
return response.json()
})
.then(json => {
const items = json.results.map((res: any) => {
return {
lat: res.geometry.lat,
lng: res.geometry.lng,
address: res.formatted
}
})
return resultItems.push(...items)
})
.catch(error => {
return []
})
}
Any help or suggestions are highly appreciated!