Skip to content

Commit

Permalink
seriesAll support tuple type (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
betafcc authored Oct 22, 2024
1 parent 59f3f00 commit ed568e7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# deverything

## 1.8.1

### Patch Changes

- seriesAll support tuple type

## 1.8.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deverything",
"version": "1.8.0",
"version": "1.8.1",
"description": "Everything you need for Dev",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
32 changes: 18 additions & 14 deletions src/helpers/seriesAll.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { isFunction, isPromise } from "../validators";
import { isFunction, isPromise } from '../validators'

type SeriesResult<T extends readonly unknown[]> = {
[K in keyof T]: T[K] extends Promise<infer U>
? U
: T[K] extends () => Promise<infer U>
? U
: never
}

/**
*
Expand All @@ -11,20 +19,16 @@ import { isFunction, isPromise } from "../validators";
* async () => 4,
* ]); => [1, 2, 3, 4]
*/
export const seriesAll = async <T>(
series: (Promise<T> | (() => Promise<T>))[]
): Promise<T[]> => {
const results = [];
export const seriesAll = async <T extends readonly unknown[]>(
series: readonly [...T],
): Promise<SeriesResult<T>> => {
const results: unknown[] = []
for (const fn of series) {
if (isPromise(fn)) results.push(await fn);
else if (isFunction(fn)) results.push(await fn());
else throw new Error("seriesAll: invalid type");
if (isPromise(fn)) results.push(await fn)
else if (isFunction(fn)) results.push(await fn())
else throw new Error('seriesAll: invalid type')
}

// TODO: "as T[];" fix TS error
// error TS2345: Argument of type '(() => Promise<T>) | Awaited<T>' is not assignable to parameter of type 'T'.
// 'T' could be instantiated with an arbitrary type which could be unrelated to '(() => Promise<T>) | Awaited<T>'.
return results as T[];
};
return results as SeriesResult<T>
}

// TODO: rename to seriesAsync

0 comments on commit ed568e7

Please sign in to comment.