我試圖在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! –