Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make batch method generic, similiar to Promise.all #14

Merged

Commits on Dec 19, 2020

  1. 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<number | string>[]) {
      const result: Spex.IArrayExt<number | string> = 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).
    ChristopherChudzicki committed Dec 19, 2020
    Configuration menu
    Copy the full SHA
    169abe3 View commit details
    Browse the repository at this point in the history