Skip to content

Commit ea9c277

Browse files
Improve TypeScript types by using variadic tuple instead of overloads (#9)
1 parent d464beb commit ea9c277

File tree

2 files changed

+8
-155
lines changed

2 files changed

+8
-155
lines changed

index.d.ts

Lines changed: 7 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ declare namespace pAll {
22
type Options = import('p-map').Options;
33

44
type PromiseFactory<T> = () => PromiseLike<T>;
5+
6+
// From: https://github.com/microsoft/TypeScript/blob/4f5b3299fee9a54b692aba9df7a9e894bd86e81d/src/lib/es2015.promise.d.ts#L1
7+
type Awaited<T> = T extends undefined ? T : T extends PromiseLike<infer U> ? U : T;
58
}
69

710
// TODO: Refactor the whole definition back to multiple overloaded functions
@@ -28,159 +31,9 @@ import got = require('got');
2831
})();
2932
```
3033
*/
31-
declare const pAll: {
32-
<
33-
Result1,
34-
Result2,
35-
Result3,
36-
Result4,
37-
Result5,
38-
Result6,
39-
Result7,
40-
Result8,
41-
Result9,
42-
Result10
43-
>(
44-
tasks: [
45-
pAll.PromiseFactory<Result1>,
46-
pAll.PromiseFactory<Result2>,
47-
pAll.PromiseFactory<Result3>,
48-
pAll.PromiseFactory<Result4>,
49-
pAll.PromiseFactory<Result5>,
50-
pAll.PromiseFactory<Result6>,
51-
pAll.PromiseFactory<Result7>,
52-
pAll.PromiseFactory<Result8>,
53-
pAll.PromiseFactory<Result9>,
54-
pAll.PromiseFactory<Result10>
55-
],
56-
options?: pAll.Options
57-
): Promise<
58-
[
59-
Result1,
60-
Result2,
61-
Result3,
62-
Result4,
63-
Result5,
64-
Result6,
65-
Result7,
66-
Result8,
67-
Result9,
68-
Result10
69-
]
70-
>;
71-
<
72-
Result1,
73-
Result2,
74-
Result3,
75-
Result4,
76-
Result5,
77-
Result6,
78-
Result7,
79-
Result8,
80-
Result9
81-
>(
82-
tasks: [
83-
pAll.PromiseFactory<Result1>,
84-
pAll.PromiseFactory<Result2>,
85-
pAll.PromiseFactory<Result3>,
86-
pAll.PromiseFactory<Result4>,
87-
pAll.PromiseFactory<Result5>,
88-
pAll.PromiseFactory<Result6>,
89-
pAll.PromiseFactory<Result7>,
90-
pAll.PromiseFactory<Result8>,
91-
pAll.PromiseFactory<Result9>
92-
],
93-
options?: pAll.Options
94-
): Promise<
95-
[
96-
Result1,
97-
Result2,
98-
Result3,
99-
Result4,
100-
Result5,
101-
Result6,
102-
Result7,
103-
Result8,
104-
Result9
105-
]
106-
>;
107-
<Result1, Result2, Result3, Result4, Result5, Result6, Result7, Result8>(
108-
tasks: [
109-
pAll.PromiseFactory<Result1>,
110-
pAll.PromiseFactory<Result2>,
111-
pAll.PromiseFactory<Result3>,
112-
pAll.PromiseFactory<Result4>,
113-
pAll.PromiseFactory<Result5>,
114-
pAll.PromiseFactory<Result6>,
115-
pAll.PromiseFactory<Result7>,
116-
pAll.PromiseFactory<Result8>
117-
],
118-
options?: pAll.Options
119-
): Promise<
120-
[Result1, Result2, Result3, Result4, Result5, Result6, Result7, Result8]
121-
>;
122-
<Result1, Result2, Result3, Result4, Result5, Result6, Result7>(
123-
tasks: [
124-
pAll.PromiseFactory<Result1>,
125-
pAll.PromiseFactory<Result2>,
126-
pAll.PromiseFactory<Result3>,
127-
pAll.PromiseFactory<Result4>,
128-
pAll.PromiseFactory<Result5>,
129-
pAll.PromiseFactory<Result6>,
130-
pAll.PromiseFactory<Result7>
131-
],
132-
options?: pAll.Options
133-
): Promise<[Result1, Result2, Result3, Result4, Result5, Result6, Result7]>;
134-
<Result1, Result2, Result3, Result4, Result5, Result6>(
135-
tasks: [
136-
pAll.PromiseFactory<Result1>,
137-
pAll.PromiseFactory<Result2>,
138-
pAll.PromiseFactory<Result3>,
139-
pAll.PromiseFactory<Result4>,
140-
pAll.PromiseFactory<Result5>,
141-
pAll.PromiseFactory<Result6>
142-
],
143-
options?: pAll.Options
144-
): Promise<[Result1, Result2, Result3, Result4, Result5, Result6]>;
145-
<Result1, Result2, Result3, Result4, Result5>(
146-
tasks: [
147-
pAll.PromiseFactory<Result1>,
148-
pAll.PromiseFactory<Result2>,
149-
pAll.PromiseFactory<Result3>,
150-
pAll.PromiseFactory<Result4>,
151-
pAll.PromiseFactory<Result5>
152-
],
153-
options?: pAll.Options
154-
): Promise<[Result1, Result2, Result3, Result4, Result5]>;
155-
<Result1, Result2, Result3, Result4>(
156-
tasks: [
157-
pAll.PromiseFactory<Result1>,
158-
pAll.PromiseFactory<Result2>,
159-
pAll.PromiseFactory<Result3>,
160-
pAll.PromiseFactory<Result4>
161-
],
162-
options?: pAll.Options
163-
): Promise<[Result1, Result2, Result3, Result4]>;
164-
<Result1, Result2, Result3>(
165-
tasks: [
166-
pAll.PromiseFactory<Result1>,
167-
pAll.PromiseFactory<Result2>,
168-
pAll.PromiseFactory<Result3>
169-
],
170-
options?: pAll.Options
171-
): Promise<[Result1, Result2, Result3]>;
172-
<Result1, Result2>(
173-
tasks: [pAll.PromiseFactory<Result1>, pAll.PromiseFactory<Result2>],
174-
options?: pAll.Options
175-
): Promise<[Result1, Result2]>;
176-
<Result1>(
177-
tasks: [pAll.PromiseFactory<Result1>],
178-
options?: pAll.Options
179-
): Promise<[Result1]>;
180-
<TAll>(
181-
tasks: Iterable<pAll.PromiseFactory<TAll>> | Array<pAll.PromiseFactory<TAll>>,
182-
options?: pAll.Options
183-
): Promise<TAll[]>;
184-
};
34+
declare const pAll: <Task extends Array<pAll.PromiseFactory<unknown>>>(
35+
tasks: readonly [...Task],
36+
options?: pAll.Options,
37+
) => Promise<{ [P in keyof Task]: Task[P] extends () => unknown ? pAll.Awaited<ReturnType<Task[P]>> : Task[P] }>;
18538

18639
export = pAll;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"devDependencies": {
5050
"ava": "^1.4.1",
5151
"delay": "^4.1.0",
52-
"tsd": "^0.11.0",
52+
"tsd": "^0.16.0",
5353
"xo": "^0.28.0"
5454
}
5555
}

0 commit comments

Comments
 (0)