【问题标题】:TypeScript 0.9.5 - ...args: any[] dead in the water? (Looking at Q.d.ts)TypeScript 0.9.5 - ...args: any[] 死在水里了吗? (看着Q.d.ts)
【发布时间】:2013-12-23 17:50:55
【问题描述】:

我希望其中一个 TypeScript 能够在此处投入使用。我是绝对类型贡献者之一,我一直致力于修复与 Q.d.ts 相关的损坏测试。

q-tests.ts 中的以下测试在 TS 0.9.5 之前有效:

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

promised 方法定义如下所示:

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

但是现在我们在 0.9.5 上,这个测试没有编译并且失败并出现这个错误:

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.

我尝试调整测试以获得更深入的了解 - 例如通过这样指定类型:

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

但我没有得到更多信息 - 它以类似的方式失败:

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.

谁能提供任何见解?我没有看到描述打字方式的任何问题。它看起来正确,但它不起作用。我似乎记得使用 TS 0.9.5 现在默认将任何内容视为 {}?这会将...args: any[] 回调签名从水中吹走吗?我希望不会!

【问题讨论】:

标签: typescript q definitelytyped


【解决方案1】:

感谢 IgorBek 在这里回答:https://github.com/borisyankov/DefinitelyTyped/pull/1467

我引用:

“我将参数重写为可选。...args: any[] 表示该函数需要 0 个或更多参数。所以即使没有参数它也应该工作。(a: number, b: number) =&gt; number 不可转换,因为它需要 2 个参数。所以当你写 @ 987654324@,该函数需要 0、1 或 2 个参数(或更多,如果我们将其余参数视为未使用)。"

Ryan Cavanaugh 是正确的,这是重复的。我希望按照建议对 TypeScript 进行调整,因此将非可选参数指定为可选参数不再是解决方案。它更像是一种解决方法,而不是合法代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-07
    • 2014-01-31
    • 2022-12-18
    • 2017-03-18
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 2020-02-24
    相关资源
    最近更新 更多