当 DefinitelyTyped 类型定义用于比我想要的版本更新的版本时该怎么办?

What to do when DefinitelyTyped type definitions are for a newer version than what I want?

我正在尝试使用 async's filter 函数,该函数来自 v1.5.2 的文档(如果您 npm i async,则默认获得该函数),采用单个(布尔)值迭代器中的回调:

iterator(item, callback) - A truth test to apply to each item in arr. The iterator is passed a callback(truthValue), which must be called with a boolean argument once it has completed.

我检查过the code,这个版本是真的。

问题是正在为 async v2 开发的候选版本似乎将其更改为采用通常的 (error, value) 回调。后一个定义是 DefinitelyTyped 中异步的类型定义所使用的定义:

interface AsyncBooleanIterator<T> { (item: T, callback: (err: string, truthValue: boolean) => void): void; }
// ...
filter<T>(arr: T[], iterator: AsyncBooleanIterator<T>, callback?: AsyncResultArrayCallback<T>): any;

现在发生了什么?我无法发布 PR,因为当前的类型定义是异步的未来。但是我在使用 async 时遇到了一个错误,我想如何使用 v1.5.2。我可以访问 DefinitelyTyped 类型定义的不同版本吗?

我刚开始使用 Typescript,听说过一种叫做合并的东西。显然,可以将 AsyncBooleanIterator 的定义扩展为一个也可以使用单个布尔参数进行回调的定义……但我不确定该怎么做。任何帮助将不胜感激。

您可以使用以前的版本之一 - 查看历史记录并选择适当的提交。
如果你想手动允许两个回调签名,你可以这样做:

interface AsyncBooleanIterator<T> {
    (item: T, callback: ((truthValue: boolean) => void) | ((err:string,truthValue:boolean) => void)): void;
}