Reputation: 3607
I'm trying to use async's filter function, which from the doc for v1.5.2 (the one you get by default if you npm i async
), takes a single (boolean) value for the callback in the iterator:
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.
I have checked the code and it is true of this version.
The problem is that the release candidate being worked on for async v2 seems to change this to take the usual (error, value) callback. This latter definition is what's being used by the type definitions for async in DefinitelyTyped:
interface AsyncBooleanIterator<T> { (item: T, callback: (err: string, truthValue: boolean) => void): void; }
// ...
filter<T>(arr: T[], iterator: AsyncBooleanIterator<T>, callback?: AsyncResultArrayCallback<T>): any;
What happens now? I can't issue a PR because the current type definitions are the future of async. But I get an error using async how I'm suppose to for v1.5.2. Are there different versions I can access for DefinitelyTyped type defs?
I'm starting out with Typescript and have heard of something called merging. Apparently, it's possible to extend the definition of AsyncBooleanIterator
to be one which can also take a callback with a single boolean argument… but I'm not sure how to do this. Any help would be appreciated.
Upvotes: 3
Views: 448
Reputation: 37938
You can use one of the previous versions - view history and pick appropriate commit.
If you want manually allow both callback signatures you can do something like this:
interface AsyncBooleanIterator<T> {
(item: T, callback: ((truthValue: boolean) => void) | ((err:string,truthValue:boolean) => void)): void;
}
Upvotes: 1