From 0720652b57a630437f2648fef56459ad9d17983e Mon Sep 17 00:00:00 2001 From: Hernan Rajchert Date: Wed, 14 Mar 2018 15:53:17 -0300 Subject: [PATCH] fix(task.all): fix typing to support arrays of multiple elements of same type --- package-lock.json | 2 +- package.json | 4 ++-- src/task.ts | 12 ++++++------ test/task.test.ts | 17 +++++++++++++++++ 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1a66521..b08303a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@acamica/task", - "version": "0.1.1", + "version": "0.2.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7437ed1..45bdc9d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@acamica/task", - "version": "0.2.0", + "version": "0.2.1", "description": "Promise replacement made with TypeScript more suitable for functional programming and error handling", "keywords": [ "async", @@ -104,7 +104,7 @@ "ts-node": "^3.0.6", "tslint": "^5.4.3", "tslint-config-acamica": "2.0.0", - "typedoc": "^0.9.0", + "typedoc": "0.11.1", "typescript": "^2.3.4", "validate-commit-msg": "^2.12.2" } diff --git a/src/task.ts b/src/task.ts index aa21a6d..6db800d 100644 --- a/src/task.ts +++ b/src/task.ts @@ -59,25 +59,25 @@ export class Task { static all (tasks: [Task, Task, Task, Task]): Task<[T1, T2, T3, T4], E1 | E2 | E3 | E4>; static all (tasks: [Task, Task, Task]): Task<[T1, T2, T3], E1 | E2 | E3>; static all (tasks: [Task, Task]): Task<[T1, T2], E1 | E2>; - static all (tasks: [Task]): Task<[T1], E1>; - static all (tasks: Task[]) { + static all (tasks: Array>): Task<[T], E>; + static all (tasks: any): any { // Flag to track if any Task has resolved let rejected = false; // Array that we'll fill with the resolved values, in order - const resolvedValues: T[] = []; + const resolvedValues: any[] = []; // Counter of resolved Tasks (we can't use resolvedValues.length since we add elements through index) let resolvedQty = 0; return new Task((outerResolve, outerReject) => { - tasks.forEach((aTask, index) => { + tasks.forEach((aTask: any, index: number) => { aTask - .fork(err => { + .fork((err: any) => { // We do only reject if there was no previous rejection if (!rejected) { rejected = true; outerReject(err); } - }, x => { + }, (x: any) => { // Shouldn't resolve if another Task has rejected if (rejected) { return; diff --git a/test/task.test.ts b/test/task.test.ts index fc78d5a..e7f17b7 100644 --- a/test/task.test.ts +++ b/test/task.test.ts @@ -318,6 +318,23 @@ describe('Task', () => { ); }); + it('should mantain types when given an array of elements of the same type', cb => { + // GIVEN: a resolved Task + const task = Task.resolve(5); + + const multipleTasks = [task, task, task, task, task, task, task, task, task, task, task]; + + // WHEN: we do a Task.all from the previous tasks + const tAll = Task.all(multipleTasks); + + + // THEN: the resulting Task is resolved with an array of the resolved value + tAll.fork( + jestAssertNever(cb), + assertFork(cb, x => expect(x).toEqual([5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5])) + ); + }); + it('should work with a single rejected Task', cb => { // GIVEN: a rejected Task const task = Task.reject('Buu!');