Typically, I prefer using observables. However, in order to avoid 'callback hell' in this particular scenario, I decided to use toPromise()
. Unfortunately, I encountered a lint error message when trying to define the return type:
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? Type 'Object' is missing the following properties from type 'ReferralReasons[]': length
Here is my code (this.apiSvc.get
returns the http.get object):
async getJobReferralReasons(): Promise<Array<ReferralReasons>> {
return this.apiSvc.get(`${this.API_JOB}/ReferralReasons`).toPromise();
}
However, the error disappears when I use:
async getJobReferralReasons(): Promise<any> {
return this.apiSvc.get(`${this.API_JOB}/ReferralReasons`).toPromise();
}
As a result, when I call it, I receive the array of referral codes directly in an array:
const reasons = await this.helpdeskSvc.getJobReferralReasons();
I assumed that changing any
and setting the type to Array<ReferralReasons>
would work. What am I missing in setting the correct type?
---Update
This is how my service call looks like:
get(apiURL, data = {}) {
return this.http.get(this.parseURL(apiURL), {
headers: this.getHeaders(),
params: this.parseGetParams(data)
});
}
While this works, it's not exactly what I intended (slightly different and unable to specify the type before the get method):
async getJobReferralReasons2(): Promise<ReferralReasons[]> {
return this.http.get<ReferralReasons[]>('', {}).toPromise();
}