0

There is a service is defined as below:

export class MyService {
    doSomething(callbacks: { onSuccess: (data: Object) => any, onError: (err: any) => any }) {
        // This function does something
    }
}

It is used in a component as below:

export class MyComponent implements OnInit {
    someFunction(): void { 
        this.myService.doSomething(
            {
                onSuccess(data: Object) {
                    onSuccessFunction(data) // Error here
                },
                onError(err: any) {
                }
            }
        )
    }
    
    onSuccessFunction(data: Object) {
    }
}

As can be seen above, the onSuccessFunction which is defined in MyComponent and invoked in the anonymous function onSuccess. But still typescript is giving error as below:

Property 'initActiveOrders' does not exist on type '{ onSuccess: (data: Object) => any; onError: (err: HttpErrorResponse) => any; }'.ts(2339)

What can be the possible reason?

0

2 Answers 2

1

Use an arrow function:

someFunction(): void { 
  this.myService.doSomething({
    onSuccess: (data: Object) => {
      this.onSuccessFunction(data);
    },
    ...
  });
}
Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you forgot to add this. before the method-call on line 5

1 Comment

Please see the edited question. It is looking for the function inside the anonymous function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.