My current challenge involves a method that is consuming data from an external API:
public async getStatus(id: string): Promise<APIRes | undefined> {
try {
const result = await getRequest(`${this.URL}/endpoint/${id}`)
const response: APIRes = result.data
return response
} catch (err) {
errorHandler(err)
}
}
The structure of the interface for APIRes is as follows:
export interface APIRes{
version: string,
status: string,
uuid: string,
time: string
}
An issue arises when I attempt to call the getStatus function from another method:
public async getListOfResults(id: string) {
try {
const getStatus = await this.getStatus(id)
if (getStatus.status === 'Queue' || getStatus.status === 'Progr') {
...//MORE CODE
}
const result = await getRequest(`${this.URL}/endpoint/${id}/result`)
return result.data
} catch (err) {
errorHandler(err)
}
}
Upon doing so, I encounter the error Object is possibly undefined
related to getStatus.status. While I understand the root cause of this issue, I am unsure of the best solution without resorting to adding the nostrict
flag.
If I remove the <| undefined>
in the return type declaration for getStatus
, I receive the following error:
Function lacks ending return statement and return type does not include 'undefined'.ts(2366)`
Even after attempting to change it from undefined to void, the error persists on getStatus.status
.