Within my class, I have defined a method like so:
lock(key: string, opts: any, cb?: LMClientLockCallBack): void;
When a user calls it with all arguments:
lock('foo', null, (err,val) => {
});
The typings are correct. However, if they skip the options argument:
lock('foo', (err,val) => {
});
Then tsc
interprets the callback function as type any
, illustrated here:
https://i.stack.imgur.com/ONspp.png
Is there a way for users to skip passing an empty object or null and directly provide the callback?
I attempted overloading the method with two definitions:
lock(key: string, cb: LMClientLockCallBack, n?: LMClientLockCallBack) : void;
lock(key: string, opts: any, cb?: LMClientLockCallBack) { ... }
However, this approach did not compile and raised new issues:
https://i.stack.imgur.com/Lktmo.png
And when trying this alternative:
lock(key: string, cb: LMClientLockCallBack) : void;
lock(key: string, opts: any, cb?: LMClientLockCallBack) { ... }
This complication arose:
https://i.stack.imgur.com/rq2ED.png
There must be a resolution to this dilemma, right?