-
Couldn't load subscription status.
- Fork 24
Add TS typings #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add TS typings #10
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,295 @@ | ||
| declare module 'asyncro' { | ||
| type AsyncFn<T> = () => PromiseLike<T>; | ||
| type AsyncFnReturnType<T> = T extends AsyncFn<infer U> ? U : any; | ||
|
|
||
| interface Asyncro { | ||
| /** | ||
| * Invoke an async reducer function on each item in the given Array, | ||
| * where the reducer transforms an accumulator value based on each item | ||
| * iterated over. | ||
| * **Note**: because reduce() is order-sensitive, iteration is sequential. | ||
| * > This is an asynchronous version of Array.prototype.reduce(). | ||
| * @param array The Array to reduce. | ||
| * @param reducer Async function, gets passed (accumulator, value, index, | ||
| * array) and returns a new value for accumulator. | ||
| * @param accumulator Optional initial accumulator value. | ||
| * @returns Any final accumulator value. | ||
| */ | ||
| reduce<T>( | ||
| array: ArrayLike<T>, | ||
| reducer: ( | ||
| accumulator: T, | ||
| value: T, | ||
| index: number, | ||
| array: ArrayLike<T>, | ||
| ) => PromiseLike<T>, | ||
| accumulator?: T, | ||
| ): T; | ||
| reduce<T, U>( | ||
| array: ArrayLike<T>, | ||
| reducer: ( | ||
| accumulator: U, | ||
| value: T, | ||
| index: number, | ||
| array: ArrayLike<T>, | ||
| ) => PromiseLike<U>, | ||
| accumulator: U, | ||
| ): Promise<U>; | ||
|
|
||
| /** | ||
| * Invoke an async transform function on each item in the given Array in | ||
| * parallel, returning the resulting Array of mapped/transformed items. | ||
| * @param array Invoke an async transform function on each item in the | ||
| * given Array in parallel, returning the resulting Array of | ||
| * mapped/transformed items. | ||
| * > This is an asynchronous, parallelized version of Array.prototype.map(). | ||
| * @param mapper Async function, gets passed (value, index, array), | ||
| * returns the new value. | ||
| * @returns Array resulting mapped/transformed values. | ||
| */ | ||
| map<T, U>( | ||
| array: ArrayLike<T>, | ||
| mapper: (value: T, index: number, array: ArrayLike<T>) => PromiseLike<U>, | ||
| ): Promise<U[]>; | ||
|
|
||
| /** | ||
| * Invoke an async filter function on each item in the given Array in | ||
| * parallel, returning an Array of values for which the filter function | ||
| * returned a truthy value. | ||
| * > This is an asynchronous, parallelized version of Array.prototype.filter(). | ||
| * @param array The Array to filter. | ||
| * @param filterer Async function. Gets passed (value, index, array), | ||
| * returns true to keep the value in the resulting filtered Array. | ||
| * @returns Array resulting filtered values. | ||
| */ | ||
| filter<T>( | ||
| array: ArrayLike<T>, | ||
| filterer: (value: T, index: number, array: T[]) => PromiseLike<any>, | ||
| ): Promise<T[]>; | ||
| /** | ||
| * Invoke an async function on each item in the given Array in parallel, | ||
| * returning the first element predicate returns truthy for. | ||
| * > This is an asynchronous, parallelized version of | ||
| * Array.prototype.find(). | ||
| * @param array The Array to find. | ||
| * @param predicate Async function. Gets passed (value, index, array), returns true | ||
| * to be the find result. | ||
| * @returns Any resulting find value. | ||
| */ | ||
| find<T>( | ||
| array: ArrayLike<T>, | ||
| predicate: (value: T, index: number, array: T[]) => PromiseLike<boolean>, | ||
| ): Promise<T | undefined>; | ||
|
|
||
| /** | ||
| * Checks if predicate returns truthy for all elements of collection in | ||
| * parallel. | ||
| * > This is an asynchronous, parallelized version of | ||
| * Array.prototype.every(). | ||
| * @param array The Array to iterate over. | ||
| * @param predicate Async function. Gets passed (value, index, array), The | ||
| * function invoked per iteration. | ||
| * @returns Returns true if all element passes the predicate check, else | ||
| * false. | ||
| */ | ||
| every<T>( | ||
| array: ArrayLike<T>, | ||
| predicate: (value: T, index: number, array: T[]) => PromiseLike<boolean>, | ||
| ): boolean; | ||
|
|
||
| /** | ||
| * Checks if predicate returns truthy for any element of collection in | ||
| * parallel. | ||
| * @param array The Array to iterate over. | ||
| * @param predicate Async function. Gets passed (value, index, array), The | ||
| * function invoked per iteration. | ||
| * @returns Returns true if any element passes the predicate check, else | ||
| * false. | ||
| */ | ||
| some<T>( | ||
| array: ArrayLike<T>, | ||
| predicate: (value: T, index: number, array: T[]) => PromiseLike<boolean>, | ||
| ): boolean; | ||
|
|
||
| /** | ||
| * Invoke all async functions in an Object in parallel, returning the | ||
| * result. | ||
| * @param list Object with values that are async functions to invoke. | ||
| * @returns Same structure as list input, but with values now resolved. | ||
| */ | ||
| parallel<T extends Record<string, AsyncFn<any>>>( | ||
| list: T, | ||
| ): Promise<{[K in keyof T]: AsyncFnReturnType<T[K]>}>; | ||
|
|
||
| /** | ||
| * Invoke all async functions in an Array in parallel, returning the | ||
| * result. | ||
| * @param list Array with values that are async functions to invoke. | ||
| * @returns Same structure as list input, but with values now resolved. | ||
| */ | ||
| parallel<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>( | ||
| list: [ | ||
| AsyncFn<T1>, | ||
| AsyncFn<T2>, | ||
| AsyncFn<T3>, | ||
| AsyncFn<T4>, | ||
| AsyncFn<T5>, | ||
| AsyncFn<T6>, | ||
| AsyncFn<T7>, | ||
| AsyncFn<T8>, | ||
| AsyncFn<T9>, | ||
| AsyncFn<T10> | ||
| ], | ||
| ): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; | ||
| parallel<T1, T2, T3, T4, T5, T6, T7, T8, T9>( | ||
| list: [ | ||
| AsyncFn<T1>, | ||
| AsyncFn<T2>, | ||
| AsyncFn<T3>, | ||
| AsyncFn<T4>, | ||
| AsyncFn<T5>, | ||
| AsyncFn<T6>, | ||
| AsyncFn<T7>, | ||
| AsyncFn<T8>, | ||
| AsyncFn<T9> | ||
| ], | ||
| ): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; | ||
| parallel<T1, T2, T3, T4, T5, T6, T7, T8>( | ||
| list: [ | ||
| AsyncFn<T1>, | ||
| AsyncFn<T2>, | ||
| AsyncFn<T3>, | ||
| AsyncFn<T4>, | ||
| AsyncFn<T5>, | ||
| AsyncFn<T6>, | ||
| AsyncFn<T7>, | ||
| AsyncFn<T8> | ||
| ], | ||
| ): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; | ||
| parallel<T1, T2, T3, T4, T5, T6, T7>( | ||
| list: [ | ||
| AsyncFn<T1>, | ||
| AsyncFn<T2>, | ||
| AsyncFn<T3>, | ||
| AsyncFn<T4>, | ||
| AsyncFn<T5>, | ||
| AsyncFn<T6>, | ||
| AsyncFn<T7> | ||
| ], | ||
| ): Promise<[T1, T2, T3, T4, T5, T6, T7]>; | ||
| parallel<T1, T2, T3, T4, T5, T6>( | ||
| list: [ | ||
| AsyncFn<T1>, | ||
| AsyncFn<T2>, | ||
| AsyncFn<T3>, | ||
| AsyncFn<T4>, | ||
| AsyncFn<T5>, | ||
| AsyncFn<T6> | ||
| ], | ||
| ): Promise<[T1, T2, T3, T4, T5, T6]>; | ||
| parallel<T1, T2, T3, T4, T5>( | ||
| list: [AsyncFn<T1>, AsyncFn<T2>, AsyncFn<T3>, AsyncFn<T4>, AsyncFn<T5>], | ||
| ): Promise<[T1, T2, T3, T4, T5]>; | ||
| parallel<T1, T2, T3, T4>( | ||
| list: [AsyncFn<T1>, AsyncFn<T2>, AsyncFn<T3>, AsyncFn<T4>], | ||
| ): Promise<[T1, T2, T3]>; | ||
| parallel<T1, T2, T3>( | ||
| list: [AsyncFn<T1>, AsyncFn<T2>, AsyncFn<T3>], | ||
| ): Promise<[T1, T2, T3]>; | ||
| parallel<T1, T2>(list: [AsyncFn<T1>, AsyncFn<T2>]): Promise<[T1, T2]>; | ||
| parallel<T>(list: ArrayLike<AsyncFn<T>>): Promise<T[]>; | ||
|
|
||
| /** | ||
| * Invoke all async functions in an Object sequentially, returning the | ||
| * result. | ||
| * @param list Object with values that are async functions to invoke. | ||
| * @returns Same structure as list input, but with values now resolved. | ||
| */ | ||
| series<T extends Record<string, AsyncFn<any>>>( | ||
| list: T, | ||
| ): Promise<{[K in keyof T]: AsyncFnReturnType<T[K]>}>; | ||
|
|
||
| /** | ||
| * Invoke all async functions in an Array sequentially, returning the | ||
| * result. | ||
| * @param list Array with values that are async functions to invoke. | ||
| * @returns Same structure as list input, but with values now resolved. | ||
| */ | ||
| series<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>( | ||
| list: [ | ||
| AsyncFn<T1>, | ||
| AsyncFn<T2>, | ||
| AsyncFn<T3>, | ||
| AsyncFn<T4>, | ||
| AsyncFn<T5>, | ||
| AsyncFn<T6>, | ||
| AsyncFn<T7>, | ||
| AsyncFn<T8>, | ||
| AsyncFn<T9>, | ||
| AsyncFn<T10> | ||
| ], | ||
| ): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; | ||
| series<T1, T2, T3, T4, T5, T6, T7, T8, T9>( | ||
| list: [ | ||
| AsyncFn<T1>, | ||
| AsyncFn<T2>, | ||
| AsyncFn<T3>, | ||
| AsyncFn<T4>, | ||
| AsyncFn<T5>, | ||
| AsyncFn<T6>, | ||
| AsyncFn<T7>, | ||
| AsyncFn<T8>, | ||
| AsyncFn<T9> | ||
| ], | ||
| ): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; | ||
| series<T1, T2, T3, T4, T5, T6, T7, T8>( | ||
| list: [ | ||
| AsyncFn<T1>, | ||
| AsyncFn<T2>, | ||
| AsyncFn<T3>, | ||
| AsyncFn<T4>, | ||
| AsyncFn<T5>, | ||
| AsyncFn<T6>, | ||
| AsyncFn<T7>, | ||
| AsyncFn<T8> | ||
| ], | ||
| ): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; | ||
| series<T1, T2, T3, T4, T5, T6, T7>( | ||
| list: [ | ||
| AsyncFn<T1>, | ||
| AsyncFn<T2>, | ||
| AsyncFn<T3>, | ||
| AsyncFn<T4>, | ||
| AsyncFn<T5>, | ||
| AsyncFn<T6>, | ||
| AsyncFn<T7> | ||
| ], | ||
| ): Promise<[T1, T2, T3, T4, T5, T6, T7]>; | ||
| series<T1, T2, T3, T4, T5, T6>( | ||
| list: [ | ||
| AsyncFn<T1>, | ||
| AsyncFn<T2>, | ||
| AsyncFn<T3>, | ||
| AsyncFn<T4>, | ||
| AsyncFn<T5>, | ||
| AsyncFn<T6> | ||
| ], | ||
| ): Promise<[T1, T2, T3, T4, T5, T6]>; | ||
| series<T1, T2, T3, T4, T5>( | ||
| list: [AsyncFn<T1>, AsyncFn<T2>, AsyncFn<T3>, AsyncFn<T4>, AsyncFn<T5>], | ||
| ): Promise<[T1, T2, T3, T4, T5]>; | ||
| series<T1, T2, T3, T4>( | ||
| list: [AsyncFn<T1>, AsyncFn<T2>, AsyncFn<T3>, AsyncFn<T4>], | ||
| ): Promise<[T1, T2, T3]>; | ||
| series<T1, T2, T3>( | ||
| list: [AsyncFn<T1>, AsyncFn<T2>, AsyncFn<T3>], | ||
| ): Promise<[T1, T2, T3]>; | ||
| series<T1, T2>(list: [AsyncFn<T1>, AsyncFn<T2>]): Promise<[T1, T2]>; | ||
| series<T>(list: ArrayLike<AsyncFn<T>>): Promise<T[]>; | ||
| } | ||
|
|
||
| const Asyncro: Asyncro; | ||
|
|
||
| export = Asyncro; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to avoid having to cap the list here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, yes, but only if we use rest parameters instead of a single
listparameter.With the introduction of rest parameters with tuple types in TypeScript 3.0 and mapped types on tuples in TypeScript 3.1, we can simplify the type definition for the
parallelmethod to this:But then, it's probably not an option to change the function signature.
However, with the introduction of optional elements in tuple types in TypeScript 3.0, we can get rid of overloads and reduce the method's type definition to:
Then, we can increase the capping to a higher number of elements (by adding extra
any?elements to the generic tuple type) without adding a ton of overloads.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@developit @yenbekbay any chance to get a new version with the updated typings? This is really a blocker for those who use Typescript :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypeScript is version 3.7 now. Maybe we can do something.
cc @developit