1

I have the following interface in typescript

interface ConfigOptions {
    autoloadCallback: (err: any) => void;
}

in my implementation I have

options = {
  autoloadCallback: this.autoLoadCallBack(err)
}

public autoLoadCallBack(err: any) : void {
  console.log('im a callback');
};

which throws the following error

Types of property 'autoloadCallback' are incompatible. Type 'void' is not assignable to type '((err: any) => void) | undefined'.

since autoLoadCallBack takes any type and doesn't return anything should it match the interface specification?

1
  • options.autoloadCallback is supposed to be a function, right? But you've set it to the void return value of a function. And the error is telling you that. Why not options = {autoloadCallback: this.autoLoadCallback.bind(this)} or something? Commented Oct 23, 2018 at 15:46

1 Answer 1

2

You are calling this.autoLoadCallBack then assigning the return value of that function (which in this case is nothing, as it's void) to options.autoloadCallback.

You seem to be trying to assigning the this.autoLoadCallBack function to options.autoloadCallback directly, which would look like this:

options = {
  autoloadCallback: this.autoLoadCallBack
}

public autoLoadCallBack(err: any) : void {
  console.log('im a callback');
};
Sign up to request clarification or add additional context in comments.

2 Comments

so why does adding parameters to this.autoLoadCallBack cause this to fail?
@user618509: this.autoLoadCallBack(err) calls the function, this.autoLoadCallBack is the function. If you want a more practical example, examine the difference between running console.log and console.log("hello, world") in your browser's console.

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.