0

I have this method, with 2 possibilities:

  requestLockInfo(key: string, cb: EVCb<any>): void;
  requestLockInfo(key: string, opts: any, cb: EVCb<any>): void;
  requestLockInfo(key: string, opts: any, cb: EVCb<any>) {
      // implementation
  }

the problem is it won't compile, it says:

enter image description here

Does anyone know how to create an optional options object in the signature like this? I have this problem a lot, and making the last argument optional with cb? doesn't usually solve the root problem.

1 Answers1

3

The implementation signature needs to basically be the result of merging the two other signatures. In this case that means:

requestLockInfo(key: string, cb: EVCb<any>): void;
requestLockInfo(key: string, opts: any, cb: EVCb<any>): void;
requestLockInfo(key: string, opts: any | EVCb<any>, cb?: EVCb<any>) {
    // implementation
}

The second argument opts can be either any (in case of the signature with 3 arguments) or EVCb<any> (in case of the signature with 2 arguments).

The third argument cb needs to be optional because in case of the signature with 2 arguments, it is not defined.

lukasgeiter
  • 147,337
  • 26
  • 332
  • 270