In my project, I am currently developing a TypeScript version of the async
library, specifically focusing on creating an *-as-promised
version. To achieve this, I am utilizing the types provided by @types/async
.
One issue I have encountered is that in the declaration for the .filter function within @types/async, there are two function types with identical names:
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;
In contrast, in my project's code (found here), I only export a single .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)
);
});
}
This has resulted in a compilation error stating:
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>'.
Given this situation, I am seeking advice on how to merge these declarations into one successfully. Any insights or solutions would be greatly appreciated.
Thank you.