我试图在TypeScript中创建async
库的*-as-promised
version,重用@types/async
类型。如何在TypeScript中合并单独的函数声明?
我的问题是@types/async at the .filter function是出口两个函数类型具有相同的名称:
export function filter<T, E>(arr: T[] | IterableIterator<T>, iterator: AsyncBooleanIterator<T, E>, callback?: AsyncResultArrayCallback<T, E>): void;
export function filter<T, E>(arr: Dictionary<T>, iterator: AsyncBooleanIterator<T, E>, callback?: AsyncResultArrayCallback<T, E>): void;
但我only export one .filter function:
function filter<T>(
arr: async.Dictionary<T> | T[] | IterableIterator<T>,
iterator: (item: T) => Promise<boolean>
): Promise<Array<(T | undefined)> | undefined> {
return new Promise((resolve, reject) => {
async.filter(arr, (item, cb) => {
iterator(item)
.then(res => cb(undefined, res))
.catch(err => cb(err));
}, (err, results) =>
err
? reject(err)
: resolve(results)
);
});
}
编译时,这给了我以下错误:
lib/filter.ts(32,18): error TS2345: Argument of type 'Dictionary<T> | IterableIterator<T> | T[]' is not assignable to parameter of type 'Dictionary<T>'.
Type 'IterableIterator<T>' is not assignable to type 'Dictionary<T>'.
那么,我怎样才能合并这些声明在一个?
谢谢你。
我会用这个解决方案,谢谢@jcalz! –