From 9d66cae3a3e9fc225e656d88ad905d36edda544b Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 19 Dec 2020 15:03:53 -0500 Subject: [PATCH] Make batch method generic, similiar to Promise.all The goal of this commit is to improve the type definitions for the `batch` method so that it behaves more like `Promise.all`. Currently, when `batch` resolves it always resolves to an array with type `any[]` which means we've lost all type information about the values sent in. This commit aims to preserve the type information for arrays and tuples of length 2 through 10. Now: ```ts import Spex from './typescript/spex'; async function testTuples(s: Spex.ISpexBase) { const result = await s.batch(['1',Promise.resolve(2)]); const [a, b]: [string, number, boolean] = result; # IArrayExt duration value still works const duration: number = result.duration; // the above should behave same for tuples of length 2 through 10. // With more than 10 values, the result array items would all have // type string | number | boolean } async function testArbitraryLength(s: Spex.ISpexBase, toResolve: Promise[]) { const result: Spex.IArrayExt = await s.batch(toResolve); const duration: number = result.duration; } ``` Hardcoding 9 type definitions for tuples of length 2 through 10 is not particularly elegant. I do not know if there is a better way, but this seems to follow the [typescript definitions of Promise.all](https://github.com/microsoft/TypeScript/blob/2428ade1a91248e847f3e1561e31a9426650efee/src/lib/es2015.promise.d.ts). --- typescript/spex.d.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/typescript/spex.d.ts b/typescript/spex.d.ts index 4e95c8c..88a3cfb 100644 --- a/typescript/spex.d.ts +++ b/typescript/spex.d.ts @@ -133,8 +133,16 @@ declare namespace spex { interface ISpexBase { // API: http://vitaly-t.github.io/spex/global.html#batch - batch(values: Array, options?: { cb?: (index: number, success: boolean, result: any, delay: number) => any }): Promise>; - + batch(values: (T | Promise)[], options?: { cb?: (index: number, success: boolean, result: any, delay: number) => any}): Promise>; + batch(values: [T1 | Promise, T2 | Promise], options?: { cb?: (index: number, success: boolean, result: any, delay: number) => any }): Promise<[T1, T2] & IArrayExt>; + batch(values: [T1 | Promise, T2 | Promise, T3 | Promise], options?: { cb?: (index: number, success: boolean, result: any, delay: number) => any }): Promise<[T1, T2, T3] & IArrayExt>; + batch(values: [T1 | Promise, T2 | Promise, T3 | Promise, T4 | Promise], options?: { cb?: (index: number, success: boolean, result: any, delay: number) => any }): Promise<[T1, T2, T3, T4] & IArrayExt>; + batch(values: [T1 | Promise, T2 | Promise, T3 | Promise, T4 | Promise, T5 | Promise], options?: { cb?: (index: number, success: boolean, result: any, delay: number) => any }): Promise<[T1, T2, T3, T4, T5] & IArrayExt>; + batch(values: [T1 | Promise, T2 | Promise, T3 | Promise, T4 | Promise, T5 | Promise, T6 | Promise], options?: { cb?: (index: number, success: boolean, result: any, delay: number) => any }): Promise<[T1, T2, T3, T4, T5, T6] & IArrayExt>; + batch(values: [T1 | Promise, T2 | Promise, T3 | Promise, T4 | Promise, T5 | Promise, T6 | Promise, T7 | Promise], options?: { cb?: (index: number, success: boolean, result: any, delay: number) => any }): Promise<[T1, T2, T3, T4, T5, T6, T7] & IArrayExt>; + batch(values: [T1 | Promise, T2 | Promise, T3 | Promise, T4 | Promise, T5 | Promise, T6 | Promise, T7 | Promise, T8 | Promise], options?: { cb?: (index: number, success: boolean, result: any, delay: number) => any }): Promise<[T1, T2, T3, T4, T5, T6, T7, T8] & IArrayExt>; + batch(values: [T1 | Promise, T2 | Promise, T3 | Promise, T4 | Promise, T5 | Promise, T6 | Promise, T7 | Promise, T8 | Promise, T9 | Promise], options?: { cb?: (index: number, success: boolean, result: any, delay: number) => any }): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9] & IArrayExt>; + batch(values: [T1 | Promise, T2 | Promise, T3 | Promise, T4 | Promise, T5 | Promise, T6 | Promise, T7 | Promise, T8 | Promise, T9 | Promise, T10 | Promise], options?: { cb?: (index: number, success: boolean, result: any, delay: number) => any }): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10] & IArrayExt>; // API: http://vitaly-t.github.io/spex/global.html#page page(source: (index: number, data: any, delay: number) => any, options?: { dest?: (index: number, data: any, delay: number) => any, limit?: number }): Promise;