Skip to content

Commit 25a2864

Browse files
fix: support to pass return type for waterfall hooks (#191)
1 parent 2da9b21 commit 25a2864

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

lib/__tests__/AsyncSeriesHooks.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ describe("AsyncSeriesWaterfallHook", () => {
100100
hook.tap("undefined", () => null);
101101
return expect(hook.promise()).resolves.toBeNull();
102102
});
103+
104+
it("should work with different types", async () => {
105+
const hook = new AsyncSeriesWaterfallHook(["x"]);
106+
hook.tap("number", () => 42);
107+
hook.tap("string", () => "string");
108+
return expect(hook.promise()).resolves.toBe("string");
109+
});
103110
});
104111

105112
describe("AsyncSeriesLoopHook", () => {

lib/__tests__/SyncWaterfallHook.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ describe("SyncWaterfallHook", () => {
5959
return expect(hook.call()).toBeNull();
6060
});
6161

62+
it("should work with different types", async () => {
63+
const hook = new SyncWaterfallHook(["x"]);
64+
hook.tap("number", () => 42);
65+
hook.tap("string", () => "string");
66+
return expect(hook.call()).toBe("string");
67+
});
68+
6269
it("should allow to create sync hooks", async () => {
6370
const hook = new SyncWaterfallHook(["arg1", "arg2"]);
6471

tapable.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ export class SyncLoopHook<
9494
> extends SyncHook<T, void, AdditionalOptions> {}
9595
export class SyncWaterfallHook<
9696
T,
97+
R = AsArray<T>[0],
9798
AdditionalOptions = UnsetAdditionalOptions
98-
> extends SyncHook<T, AsArray<T>[0] | void, AdditionalOptions> {}
99+
> extends SyncHook<T, R, AdditionalOptions> {}
99100

100101
declare class AsyncHook<
101102
T,
@@ -136,8 +137,9 @@ export class AsyncSeriesLoopHook<
136137
> extends AsyncHook<T, void, AdditionalOptions> {}
137138
export class AsyncSeriesWaterfallHook<
138139
T,
140+
R = AsArray<T>[0],
139141
AdditionalOptions = UnsetAdditionalOptions
140-
> extends AsyncHook<T, AsArray<T>[0] | void, AdditionalOptions> {}
142+
> extends AsyncHook<T, R, AdditionalOptions> {}
141143

142144
type HookFactory<H> = (key: any, hook?: H) => H;
143145

0 commit comments

Comments
 (0)