Question

I'm hoping that one of the TypeScript will be able to pitch in here. I'm one of the Definitely Typed contributors and I've been working on fixing up the broken tests related to Q.d.ts.

The following test in q-tests.ts worked pre TS 0.9.5:

var eventualAdd = Q.promised((a: number, b: number) => a + b);

The promised method definition looks like this:

export function promised<T>(callback: (...args: any[]) => T): (...args: any[]) => Promise<T>;

But now we're on 0.9.5 this test does not compile and fails with this error:

error TS2082: Supplied parameters do not match any signature of call target:
Call signatures of types '(a: number, b: number) => number' and '(...args: any[]) => {}' are incompatible:
Call signature expects 0 or fewer parameters.
error TS2087: Could not select overload for 'call' expression.

I've tried tweaking the test to get a little more insight - for instance by specifying the typing like so:

var eventualAdd = Q.promised<number>((a: number, b: number) => a + b);

But I'm not getting much more information - it fails in a similar fashion like this:

error TS2082: Supplied parameters do not match any signature of call target:
Call signatures of types '(a: number, b: number) => number' and '(...args: any[]) => number' are incompatible:
Call signature expects 0 or fewer parameters.
error TS2087: Could not select overload for 'call' expression.

Can anyone provide any insight? I don't see any issues with the way the typing is described. It looks correct but it doesn't work. I seem to remember that with TS 0.9.5 any is now treated as {} by default? Does this blow the ...args: any[] callback signature out of the water? I do hope not!

Was it helpful?

Solution

Credit to IgorBek for answering here: https://github.com/borisyankov/DefinitelyTyped/pull/1467

I quote:

"I rewrote parameters as optional. ...args: any[] means that function expects 0 or more parameters. So it should work even if no parameters. (a: number, b: number) => number not convertible by the reason that it expects 2 parameters. So when you write (a?: number, b?: number) => number, the function expects 0, 1 or 2 parameters (or more, if we treat rest parameters as unused)."

Ryan Cavanaugh is correct that this is a duplicate. I hope TypeScript is tweaked as suggested so specifying non-optional parameters as optional is no longer the solution. It kind of feels more like a workaround as it is rather than legitimate code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top