Skip to content

Commit 84b32d5

Browse files
committed
Fix typings
Fixes #175 Signed-off-by: Richie Bendall <richiebendall@gmail.com>
1 parent 2c860a3 commit 84b32d5

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"scripts": {
1414
"build": "del-cli dist && tsc",
15-
"test": "xo && ava",
15+
"test": "xo && ava && del-cli dist && tsc && tsd",
1616
"bench": "node --loader=ts-node/esm bench.ts",
1717
"prepublishOnly": "del-cli dist && tsc"
1818
},
@@ -59,6 +59,7 @@
5959
"random-int": "^3.0.0",
6060
"time-span": "^5.0.0",
6161
"ts-node": "^10.9.1",
62+
"tsd": "^0.25.0",
6263
"typescript": "^4.8.4",
6364
"xo": "^0.44.0"
6465
},

source/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
5555
*/
5656
timeout?: number;
5757

58+
// TODO: The `throwOnTimeout` option should affect the return types of `add()` and `addAll()`
5859
constructor(options?: Options<QueueType, EnqueueOptionsType>) {
5960
super();
6061

@@ -237,8 +238,8 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
237238
/**
238239
Adds a sync or async task to the queue. Always returns a promise.
239240
*/
240-
async add<TaskResultType>(function_: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType | void>;
241241
async add<TaskResultType>(function_: Task<TaskResultType>, options: {throwOnTimeout: true} & Exclude<EnqueueOptionsType, 'throwOnTimeout'>): Promise<TaskResultType>;
242+
async add<TaskResultType>(function_: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType | void>;
242243
async add<TaskResultType>(function_: Task<TaskResultType>, options: Partial<EnqueueOptionsType> = {}): Promise<TaskResultType | void> {
243244
options = {
244245
timeout: this.timeout,
@@ -295,14 +296,14 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
295296
296297
@returns A promise that resolves when all functions are resolved.
297298
*/
298-
async addAll<TaskResultsType>(
299-
functions: ReadonlyArray<Task<TaskResultsType>>,
300-
options?: Partial<EnqueueOptionsType>,
301-
): Promise<Array<TaskResultsType | void>>;
302299
async addAll<TaskResultsType>(
303300
functions: ReadonlyArray<Task<TaskResultsType>>,
304301
options?: {throwOnTimeout: true} & Partial<Exclude<EnqueueOptionsType, 'throwOnTimeout'>>,
305302
): Promise<TaskResultsType[]>
303+
async addAll<TaskResultsType>(
304+
functions: ReadonlyArray<Task<TaskResultsType>>,
305+
options?: Partial<EnqueueOptionsType>,
306+
): Promise<Array<TaskResultsType | void>>;
306307
async addAll<TaskResultsType>(
307308
functions: ReadonlyArray<Task<TaskResultsType>>,
308309
options?: Partial<EnqueueOptionsType>,

test-d/index.test-d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {expectType} from 'tsd';
2+
import PQueue from '../source/index.js';
3+
4+
const queue = new PQueue();
5+
6+
expectType<Promise<string | void>>(queue.add(async () => '🦄'));
7+
expectType<Promise<string>>(queue.add(async () => '🦄', {throwOnTimeout: true}));

0 commit comments

Comments
 (0)