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

标签 typescript definitelytyped

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

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,因为当前的类型定义是异步的 future 。但是我在使用 async 时遇到了一个错误,我想如何使用 v1.5.2。我可以访问 DefinitelyTyped 类型定义的不同版本吗?

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

最佳答案

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

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

关于typescript - 当 DefinitelyTyped 类型定义用于比我想要的版本更新的版本时该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37928526/

相关文章:

Angular Material 在某些组件中不起作用

typescript - 如何使用新的 @types 包进行 TypeScript 类型?

typescript - DefinitelyTyped 的 Lodash

typescript - 类型 'prototype' 中缺少属性 'MyObject'

typescript - 使用 aws cdk 创建 elasticbeanstalk 实例

angular - @ngx-translate Nativescript/Angular 应用程序

typescript - 解决 npm 包提供的错误 index.d.ts 文件

javascript - 类体之外不允许使用 "this"关键字

typescript - 为什么 Webpack 不排除我指定的文件夹?

javascript - 如何区分 javascript 声明 typescript 文件中的静态方法和实例方法?