1
votes

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? - jcalz

1 Answers

2
votes

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');
};