i am calling function this.buildSingleRequestBody
it is throwing error this implicilty has type any it does not have type annotation , not sure what has implemented wrong.
main.ts
private buildRequestArray(specialtyMembers: ISpecialtyInfoObj[],
request: ICombinedAccountBalanceRequest): IRequestURL[] {
const specialtyUrl = urlConfig.specialtyBaseUrl + "payments/getAccountBalance";
const serviceContext = request.header.serviceContext;
const requestArray: IRequestURL[] = specialtyMembers.map(function(member) {
const body: any = this.buildSingleRequestBody(member, serviceContext);
return {url: specialtyUrl, body};
});
return requestArray;
}
private buildSingleRequestBody(specialtyMember: ISpecialtyInfoObj, serviceContext: IServiceContext) {
return {
"header": {
serviceContext
},
"specialtyId": specialtyMember.Id || "78988716",
"specialtySource": specialtyMember.specialtySource || "HBS"
};
}
i am calling function this.buildSingleRequestBody
it is throwing error this implicilty has type any it does not have type annotation , not sure what has implemented wrong.
main.ts
private buildRequestArray(specialtyMembers: ISpecialtyInfoObj[],
request: ICombinedAccountBalanceRequest): IRequestURL[] {
const specialtyUrl = urlConfig.specialtyBaseUrl + "payments/getAccountBalance";
const serviceContext = request.header.serviceContext;
const requestArray: IRequestURL[] = specialtyMembers.map(function(member) {
const body: any = this.buildSingleRequestBody(member, serviceContext);
return {url: specialtyUrl, body};
});
return requestArray;
}
private buildSingleRequestBody(specialtyMember: ISpecialtyInfoObj, serviceContext: IServiceContext) {
return {
"header": {
serviceContext
},
"specialtyId": specialtyMember.Id || "78988716",
"specialtySource": specialtyMember.specialtySource || "HBS"
};
}
function
does not capture this
from the declaration context, rather this
is decided by the caller. Since you use function
when you call map
, the this
inside anonymous function will not refer to the class. Since map
does not explicitly declare what this
it will pass into the function, the type of this
will implicitly be any inside the anonymous function, hence the error.
Use an arrow function instead as that will capture the declaration this
private buildRequestArray(specialtyMembers: ISpecialtyInfoObj[],
request: ICombinedAccountBalanceRequest): IRequestURL[] {
const specialtyUrl = urlConfig.specialtyBaseUrl + "payments/getAccountBalance";
const serviceContext = request.header.serviceContext;
const requestArray: IRequestURL[] = specialtyMembers.map((member) => {
const body: any = this.buildSingleRequestBody(member, serviceContext);
return {url: specialtyUrl, body};
});
return requestArray;
}
private buildSingleRequestBody(specialtyMember: ISpecialtyInfoObj, serviceContext: IServiceContext) {
return {
"header": {
serviceContext
},
"specialtyId": specialtyMember.Id || "78988716",
"specialtySource": specialtyMember.specialtySource || "HBS"
};
}