[p-limit] Update types to v2.1

This commit is contained in:
Dimitri Benin
2019-02-25 21:32:53 +01:00
parent b1285dace3
commit 4dbdfd2843
2 changed files with 46 additions and 33 deletions
+35 -10
View File
@@ -1,18 +1,43 @@
// Type definitions for p-limit 2.0
// Type definitions for p-limit 2.1
// Project: https://github.com/sindresorhus/p-limit#readme
// Definitions by: BendingBender <https://github.com/BendingBender>
// Linus Unnebäck <https://github.com/LinusU>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
export = pLimit;
declare function limit<T, A, B, C, D, E, F>(cb: (a: A, b: B, c: C, d: D, e: E, f: F, ...args: any[]) => PromiseLike<T> | T, a: A, b: B, c: C, d: D, e: E, f: F, ...args: any[]): Promise<T>;
declare function limit<T, A, B, C, D, E, F>(cb: (a: A, b: B, c: C, d: D, e: E, f: F) => PromiseLike<T> | T, a: A, b: B, c: C, d: D, e: E, f: F): Promise<T>;
declare function limit<T, A, B, C, D, E>(cb: (a: A, b: B, c: C, d: D, e: E) => PromiseLike<T> | T, a: A, b: B, c: C, d: D, e: E): Promise<T>;
declare function limit<T, A, B, C, D>(cb: (a: A, b: B, c: C, d: D) => PromiseLike<T> | T, a: A, b: B, c: C, d: D): Promise<T>;
declare function limit<T, A, B, C>(cb: (a: A, b: B, c: C) => PromiseLike<T> | T, a: A, b: B, c: C): Promise<T>;
declare function limit<T, A, B>(cb: (a: A, b: B) => PromiseLike<T> | T, a: A, b: B): Promise<T>;
declare function limit<T, A>(cb: (a: A) => PromiseLike<T> | T, a: A): Promise<T>;
declare function limit<T>(cb: () => PromiseLike<T> | T): Promise<T>;
/**
* Run multiple promise-returning & async functions with limited concurrency.
* @param concurrency Concurrency limit. Minimum: `1`.
* @returns A `limit` function.
*/
declare function pLimit(concurrency: number): pLimit.Limit;
declare function pLimit(concurrency: number): typeof limit;
declare namespace pLimit {
interface Limit {
/**
* Returns the promise returned by calling `fn(...args)`.
*
* @param fn Promise-returning/async function.
* @param args Any arguments to pass through to `fn`.
* Support for passing arguments on to the `fn` is provided in order to be able to avoid
* creating unnecessary closures. You probably don't need this optimization unless you're
* pushing a lot of functions.
*/
<TArgs extends any[], R>(
fn: (...args: TArgs) => PromiseLike<R> | R,
...args: TArgs
): Promise<R>;
/**
* The number of promises that are currently running.
*/
readonly activeCount: number;
/**
* The number of promises that are waiting to run (i.e. their internal `fn` was not called yet).
*/
readonly pendingCount: number;
}
}
+11 -23
View File
@@ -8,28 +8,16 @@ const input = [
limit(() => Promise.resolve(undefined)),
];
Promise.all(input).then(result => {
const str: string | undefined = result[0];
Promise.all(input); // $ExpectType Promise<(string | undefined)[]>
limit((a: string) => '', 'test').then(v => {
v; // $ExpectType string
});
limit((a: string, b: number) => Promise.resolve(''), 'test', 1).then(v => {
v; // $ExpectType string
});
let str: string;
declare function a(a: string): string;
declare function b(a: string, b: number): string;
declare function c(a: string, b: number, c: boolean): string;
declare function d(a: string, b: number, c: boolean, d: symbol): string;
declare function e(a: string, b: number, c: boolean, d: symbol, e: 'yes' | 'no'): string;
declare function f(a: string, b: number, c: boolean, d: symbol, e: 'yes' | 'no', f: 1 | 2): string;
declare function g(a: string, b: number, c: boolean, d: symbol, e: 'yes' | 'no', f: 1 | 2, g: true): string;
limit(a, 'test').then(v => { str = v; });
limit(b, 'test', 1).then(v => { str = v; });
limit(c, 'test', 1, false).then(v => { str = v; });
limit(d, 'test', 1, false, Symbol('test')).then(v => { str = v; });
limit(e, 'test', 1, false, Symbol('test'), 'no').then(v => { str = v; });
limit(f, 'test', 1, false, Symbol('test'), 'no', 2).then(v => { str = v; });
limit(g, 'test', 1, false, Symbol('test'), 'no', 2, true).then(v => { str = v; });
declare function add(...args: number[]): number;
limit(add, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13).then(v => (v === 91));
limit.activeCount; // $ExpectType number
limit.activeCount = 1; // $ExpectError
limit.pendingCount; // $ExpectType number
limit.pendingCount = 1; // $ExpectError