From eadbc0cd3badb6f402d66311b171e57d860f0b84 Mon Sep 17 00:00:00 2001 From: Erik Krogh Kristensen Date: Mon, 6 Mar 2017 15:32:29 +0100 Subject: [PATCH 1/6] async: Various fixes, mostly related to strict-nulls. Also some places where the error type was wrong. --- async/index.d.ts | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/async/index.d.ts b/async/index.d.ts index e3fbe7e5129bce..7f71f3b9bc9479 100644 --- a/async/index.d.ts +++ b/async/index.d.ts @@ -6,17 +6,16 @@ interface Dictionary { [key: string]: T; } interface ErrorCallback { (err?: T): void; } -interface AsyncWaterfallCallback { (err: E, ...args: any[]): void; } -interface AsyncBooleanResultCallback { (err: E, truthValue: boolean): void; } -interface AsyncResultCallback { (err: E, result: T): void; } -interface AsyncResultArrayCallback { (err: E, results: T[]): void; } -interface AsyncResultObjectCallback { (err: E, results: Dictionary): void; } +interface AsyncBooleanResultCallback { (err?: E, truthValue?: boolean): void; } +interface AsyncResultCallback { (err?: E, result?: T): void; } +interface AsyncResultArrayCallback { (err?: E, results?: (T | undefined)[]): void; } +interface AsyncResultObjectCallback { (err: E | undefined, results: Dictionary): void; } -interface AsyncFunction { (callback: (err?: E, result?: T) => void): void; } +interface AsyncFunction { (callback: (err?: E, result?: T) => void): any; } interface AsyncIterator { (item: T, callback: ErrorCallback): void; } interface AsyncForEachOfIterator { (item: T, key: number|string, callback: ErrorCallback): void; } interface AsyncResultIterator { (item: T, callback: AsyncResultCallback): void; } -interface AsyncMemoIterator { (memo: R, item: T, callback: AsyncResultCallback): void; } +interface AsyncMemoIterator { (memo: R | undefined, item: T, callback: AsyncResultCallback): void; } interface AsyncBooleanIterator { (item: T, callback: AsyncBooleanResultCallback): void; } interface AsyncWorker { (task: T, callback: ErrorCallback): void; } @@ -76,7 +75,7 @@ interface AsyncPriorityQueue { interface AsyncCargo { length(): number; - payload: number; + payload?: number; push(task: any, callback? : Function): void; push(task: any[], callback? : Function): void; saturated(): void; @@ -186,25 +185,25 @@ interface Async { cargo(worker : (tasks: any[], callback : ErrorCallback) => void, payload? : number) : AsyncCargo; auto(tasks: any, concurrency?: number, callback?: AsyncResultCallback): void; autoInject(tasks: any, callback?: AsyncResultCallback): void; - retry(opts: number, task: (callback : AsyncResultCallback, results: any) => void, callback: AsyncResultCallback): void; - retry(opts: { times: number, interval: number|((retryCount: number) => number) }, task: (callback: AsyncResultCallback, results : any) => void, callback: AsyncResultCallback): void; - retryable(opts: number | {times: number, interval: number}, task: AsyncFunction): AsyncFunction; - apply(fn: Function, ...arguments: any[]): AsyncFunction; + retry(opts: number, task: (callback : AsyncResultCallback, results: any) => void, callback: AsyncResultCallback): void; + retry(opts: { times: number, interval: number|((retryCount: number) => number) }, task: (callback: AsyncResultCallback, results : any) => void, callback: AsyncResultCallback): void; + retryable(opts: number | {times: number, interval: number}, task: AsyncFunction): AsyncFunction; + apply(fn: Function, ...arguments: any[]): AsyncFunction; nextTick(callback: Function, ...args: any[]): void; setImmediate: typeof async.nextTick; - reflect(fn: AsyncFunction) : (callback: (err: void, result: {error?: Error, value?: T}) => void) => void; - reflectAll(tasks: AsyncFunction[]): ((callback: (err: void, result: {error?: Error, value?: T}) => void) => void)[]; + reflect(fn: AsyncFunction) : (callback: (err: null, result: {error?: E, value?: T}) => void) => void; + reflectAll(tasks: AsyncFunction[]): ((callback: (err: undefined, result: {error?: E, value?: T}) => void) => void)[]; - timeout(fn: AsyncFunction, milliseconds: number, info?: any): AsyncFunction; - timeout(fn: AsyncResultIterator, milliseconds: number, info?: any): AsyncResultIterator; + timeout(fn: AsyncFunction, milliseconds: number, info?: any): AsyncFunction; + timeout(fn: AsyncResultIterator, milliseconds: number, info?: any): AsyncResultIterator; times (n: number, iterator: AsyncResultIterator, callback: AsyncResultArrayCallback): void; timesSeries(n: number, iterator: AsyncResultIterator, callback: AsyncResultArrayCallback): void; timesLimit(n: number, limit: number, iterator: AsyncResultIterator, callback: AsyncResultArrayCallback): void; transform(arr: T[], iteratee: (acc: R[], item: T, key: string, callback: (error?: E) => void) => void): void; - transform(arr: T[], acc: R[], iteratee: (acc: R[], item: T, key: string, callback: (error?: E) => void) => void): void; + transform(arr: T[], acc: R[], iteratee: (acc: R[] | null, item: T | undefined[], key?: string, callback?: (error?: E) => void) => void): void; transform(arr: {[key: string] : T}, iteratee: (acc: {[key: string] : R}, item: T, key: string, callback: (error?: E) => void) => void): void; transform(arr: {[key: string] : T}, acc: {[key: string] : R}, iteratee: (acc: {[key: string] : R}, item: T, key: string, callback: (error?: E) => void) => void): void; @@ -226,4 +225,3 @@ declare var async: Async; declare module "async" { export = async; } - From 10b1dd1ba5964dbbc380a96782d110edbe099f3c Mon Sep 17 00:00:00 2001 From: Erik Krogh Kristensen Date: Mon, 13 Mar 2017 10:20:33 +0100 Subject: [PATCH 2/6] Async: made AsyncFunction return void again. --- async/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/async/index.d.ts b/async/index.d.ts index 7f71f3b9bc9479..b46002d9717066 100644 --- a/async/index.d.ts +++ b/async/index.d.ts @@ -11,7 +11,7 @@ interface AsyncResultCallback { (err?: E, result?: T): void; } interface AsyncResultArrayCallback { (err?: E, results?: (T | undefined)[]): void; } interface AsyncResultObjectCallback { (err: E | undefined, results: Dictionary): void; } -interface AsyncFunction { (callback: (err?: E, result?: T) => void): any; } +interface AsyncFunction { (callback: (err?: E, result?: T) => void): void; } interface AsyncIterator { (item: T, callback: ErrorCallback): void; } interface AsyncForEachOfIterator { (item: T, key: number|string, callback: ErrorCallback): void; } interface AsyncResultIterator { (item: T, callback: AsyncResultCallback): void; } From aebfd7772bc582ed8d6f646f0ecfcb183e5a02c5 Mon Sep 17 00:00:00 2001 From: Erik Krogh Kristensen Date: Mon, 13 Mar 2017 12:53:33 +0100 Subject: [PATCH 3/6] async: reflectAll err is always null --- async/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/async/index.d.ts b/async/index.d.ts index b46002d9717066..88d4cf900b0172 100644 --- a/async/index.d.ts +++ b/async/index.d.ts @@ -193,7 +193,7 @@ interface Async { setImmediate: typeof async.nextTick; reflect(fn: AsyncFunction) : (callback: (err: null, result: {error?: E, value?: T}) => void) => void; - reflectAll(tasks: AsyncFunction[]): ((callback: (err: undefined, result: {error?: E, value?: T}) => void) => void)[]; + reflectAll(tasks: AsyncFunction[]): ((callback: (err: null, result: {error?: E, value?: T}) => void) => void)[]; timeout(fn: AsyncFunction, milliseconds: number, info?: any): AsyncFunction; timeout(fn: AsyncResultIterator, milliseconds: number, info?: any): AsyncResultIterator; From 8875c23774e1fea4f4e6293c6a29d57172c3b5a8 Mon Sep 17 00:00:00 2001 From: Erik Krogh Kristensen Date: Mon, 13 Mar 2017 14:20:12 +0100 Subject: [PATCH 4/6] async: 3 more small errors i found --- async/index.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/async/index.d.ts b/async/index.d.ts index 88d4cf900b0172..a17735eb6ecd5f 100644 --- a/async/index.d.ts +++ b/async/index.d.ts @@ -9,7 +9,7 @@ interface ErrorCallback { (err?: T): void; } interface AsyncBooleanResultCallback { (err?: E, truthValue?: boolean): void; } interface AsyncResultCallback { (err?: E, result?: T): void; } interface AsyncResultArrayCallback { (err?: E, results?: (T | undefined)[]): void; } -interface AsyncResultObjectCallback { (err: E | undefined, results: Dictionary): void; } +interface AsyncResultObjectCallback { (err: E | undefined, results: Dictionary): void; } interface AsyncFunction { (callback: (err?: E, result?: T) => void): void; } interface AsyncIterator { (item: T, callback: ErrorCallback): void; } @@ -174,7 +174,7 @@ interface Async { during(test: (testCallback : AsyncBooleanResultCallback) => void, fn: AsyncVoidFunction, callback: ErrorCallback): void; doDuring(fn: AsyncVoidFunction, test: (testCallback: AsyncBooleanResultCallback) => void, callback: ErrorCallback): void; forever(next: (next : ErrorCallback) => void, errBack: ErrorCallback) : void; - waterfall(tasks: Function[], callback?: AsyncResultCallback): void; + waterfall(tasks: Function[], callback?: AsyncResultCallback): void; compose(...fns: Function[]): Function; seq(...fns: Function[]): Function; applyEach(fns: Function[], argsAndCallback: any[]): void; // applyEach(fns, args..., callback). TS does not support ... for a middle argument. Callback is optional. @@ -207,7 +207,7 @@ interface Async { transform(arr: {[key: string] : T}, iteratee: (acc: {[key: string] : R}, item: T, key: string, callback: (error?: E) => void) => void): void; transform(arr: {[key: string] : T}, acc: {[key: string] : R}, iteratee: (acc: {[key: string] : R}, item: T, key: string, callback: (error?: E) => void) => void): void; - race(tasks: (AsyncFunction)[], callback: AsyncResultCallback) : void; + race(tasks: (AsyncFunction)[], callback: AsyncResultCallback) : void; // Utils memoize(fn: Function, hasher?: Function): Function; From 04573ae491126ace626edc4866c73502d1881dbb Mon Sep 17 00:00:00 2001 From: Erik Krogh Kristensen Date: Mon, 13 Mar 2017 14:27:09 +0100 Subject: [PATCH 5/6] async: Changed strictNullChecks to true, and fixed the tests --- async/test/explicit.ts | 54 ++++++++++++++++++++++++++++++++---------- async/test/index.ts | 16 ++++++------- async/tsconfig.json | 4 ++-- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/async/test/explicit.ts b/async/test/explicit.ts index 80343cf6b16c25..b9ea9c84ed173e 100644 --- a/async/test/explicit.ts +++ b/async/test/explicit.ts @@ -1,44 +1,74 @@ -interface StringCallback { (err: Error, result: string): void; } +interface StringCallback { (err?: Error, result?: string): void; } interface AsyncStringGetter { (callback: StringCallback): void; } var taskArray: AsyncStringGetter[] = [ function (callback) { setTimeout(function () { - callback(null, 'one'); + callback(undefined, 'one'); }, 200); }, function (callback) { setTimeout(function () { - callback(null, 'two'); + callback(undefined, 'two'); }, 100); }, ]; -async.series(taskArray, function (err, results) { console.log(results[0].match(/o/)) }); -async.parallel(taskArray, function (err, results) { console.log(results[0].match(/o/)) }); -async.parallelLimit(taskArray, 3, function (err, results) { console.log(results[0].match(/o/)) }); +async.series(taskArray, function (err, results) { + if (results) { + let first = results[0]; + if (first) { + console.log(first.match(/o/)) + } + } +}); +async.parallel(taskArray, function (err, results) { + if (results) { + let first = results[0]; + if (first) { + console.log(first.match(/o/)) + } + } +}); +async.parallelLimit(taskArray, 3, function (err, results) { + if (results) { + let first = results[0]; + if (first) { + console.log(first.match(/o/)) + } + } +}); interface Lookup { [key: string]: T; } -interface NumberCallback { (err: Error, result: number): void; } +interface NumberCallback { (err?: Error, result?: number): void; } interface AsyncNumberGetter { (callback: NumberCallback): void; } var taskDict: Lookup = { one: function(callback){ setTimeout(function(){ - callback(null, 1); + callback(undefined, 1); }, 200); }, two: function(callback){ setTimeout(function(){ - callback(null, 2); + callback(undefined, 2); }, 100); } } -async.series(taskDict, function(err, results) { console.log(results['one'].toFixed(1)) }); -async.parallel(taskDict, function(err, results) { console.log(results['one'].toFixed(1)) }); -async.parallelLimit(taskDict, 3, function(err, results) { console.log(results['one'].toFixed(1)) }); +async.series(taskDict, function(err, results) { + let one = results['one']; + console.log(one && one.toFixed(1)) +}); +async.parallel(taskDict, function(err, results) { + let one = results['one']; + console.log(one && one.toFixed(1)) +}); +async.parallelLimit(taskDict, 3, function(err, results) { + let one = results['one']; + console.log(one && one.toFixed(1)) +}); diff --git a/async/test/index.ts b/async/test/index.ts index 8c6be303e4e8ea..b30ca4c7ba9302 100644 --- a/async/test/index.ts +++ b/async/test/index.ts @@ -350,9 +350,9 @@ q2.unshift(['task3', 'task4', 'task5'], function (error) { }); -var aq = async.queue(function (level: number, callback: (error : Error, newLevel: number) => void) { +var aq = async.queue(function (level: number, callback: (error?: Error, newLevel?: number) => void) { console.log('hello ' + level); - callback(null, level+1); + callback(undefined, level+1); }); aq.push(1, function (err : Error, newLevel : number) { @@ -807,9 +807,9 @@ async.some({ // timeout -function myFunction1(foo : any, callback: (err : Error, result : any) => void ) : void { +function myFunction1(foo : any, callback: (err?: Error, result?: any) => void ) : void { console.log(`async.timeout 1 ${foo}`); - return callback(null, foo); + return callback(undefined, foo); } var wrapped1 = async.timeout(myFunction1, 1000); wrapped1({ bar: 'bar' }, function(err : Error, data : any) { @@ -817,9 +817,9 @@ wrapped1({ bar: 'bar' }, function(err : Error, data : any) { }); -function myFunction2(callback: (err : Error, result : any) => void ) : void { +function myFunction2(callback: (err?: Error, result?: any) => void ) : void { console.log(`async.timeout 2`); - return callback(null, { bar: 'bar' }); + return callback(undefined, { bar: 'bar' }); } var wrapped2 = async.timeout(myFunction2, 1000); @@ -827,9 +827,9 @@ wrapped2( function(err : Error, data : any) { console.log(`async.timeout 2 end ${data}`); }); -function myFunction3(callback: (err : Error, result : any) => void ) : void { +function myFunction3(callback: (err?: Error, result?: any) => void ) : void { console.log(`async.timeout 3`); - return callback(null, { bar: 'bar' }); + return callback(undefined, { bar: 'bar' }); } var wrapped3 = async.timeout(myFunction3, 1000, { bar: 'bar' }); diff --git a/async/tsconfig.json b/async/tsconfig.json index 851293fccffac2..5b8f42c6d29a64 100644 --- a/async/tsconfig.json +++ b/async/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" @@ -20,4 +20,4 @@ "test/index.ts", "test/explicit.ts" ] -} \ No newline at end of file +} From dda288d3f6f1995d667cd05cc39fbe0af1f9596c Mon Sep 17 00:00:00 2001 From: Erik Krogh Kristensen Date: Tue, 14 Mar 2017 15:37:32 +0100 Subject: [PATCH 6/6] async: fixed the types of async.transform --- async/index.d.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/async/index.d.ts b/async/index.d.ts index a17735eb6ecd5f..62e047b2095d74 100644 --- a/async/index.d.ts +++ b/async/index.d.ts @@ -202,10 +202,11 @@ interface Async { timesSeries(n: number, iterator: AsyncResultIterator, callback: AsyncResultArrayCallback): void; timesLimit(n: number, limit: number, iterator: AsyncResultIterator, callback: AsyncResultArrayCallback): void; - transform(arr: T[], iteratee: (acc: R[], item: T, key: string, callback: (error?: E) => void) => void): void; - transform(arr: T[], acc: R[], iteratee: (acc: R[] | null, item: T | undefined[], key?: string, callback?: (error?: E) => void) => void): void; - transform(arr: {[key: string] : T}, iteratee: (acc: {[key: string] : R}, item: T, key: string, callback: (error?: E) => void) => void): void; - transform(arr: {[key: string] : T}, acc: {[key: string] : R}, iteratee: (acc: {[key: string] : R}, item: T, key: string, callback: (error?: E) => void) => void): void; + transform(arr: T[], iteratee: (acc: R[], item: T, key: number, callback: (error?: E) => void) => void, callback?: AsyncResultArrayCallback): void; + transform(arr: T[], acc: R[], iteratee: (acc: R[], item: T, key: number, callback: (error?: E) => void) => void, callback?: AsyncResultArrayCallback): void; + + transform(arr: {[key: string] : T}, iteratee: (acc: {[key: string] : R}, item: T, key: string, callback: (error?: E) => void) => void, callback?: AsyncResultObjectCallback): void; + transform(arr: {[key: string] : T}, acc: {[key: string] : R}, iteratee: (acc: {[key: string] : R}, item: T, key: string, callback: (error?: E) => void) => void, callback?: AsyncResultObjectCallback): void; race(tasks: (AsyncFunction)[], callback: AsyncResultCallback) : void;