javascript - this `implicitly` has type any it does not have type annotation? - Stack Overflow

admin2025-04-19  0

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"
        };
    }
Share Improve this question asked Oct 15, 2018 at 14:38 hussainhussain 7,14121 gold badges87 silver badges165 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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"
    };
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745062891a282807.html

最新回复(0)