Skip to content

Commit eabd31d

Browse files
authored
fix: type infer for plugin hooks (#4571)
1 parent 009ec5f commit eabd31d

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

packages/core/src/types/plugin.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ export type HookDescriptor<T extends (...args: any[]) => any> = {
4747
order: HookOrder;
4848
};
4949

50-
export type EnvironmentAsyncHook<Callback extends (...args: any[]) => any> = {
50+
export type EnvironmentAsyncHook<
51+
Callback extends (...args: any[]) => T,
52+
T = any,
53+
> = {
5154
/**
5255
* Registers a callback function to be executed when the hook is triggered.
5356
* The callback can be a plain function or a HookDescriptor that includes execution order.
@@ -90,7 +93,7 @@ export type EnvironmentAsyncHook<Callback extends (...args: any[]) => any> = {
9093
* Each callback receives the original parameters, and their results don't affect subsequent callbacks.
9194
* @returns A promise that resolves with an array containing the results of all callbacks
9295
*/
93-
callBatch: <T = unknown>(params: {
96+
callBatch: (params: {
9497
/**
9598
* Specify the environment for filtering callbacks.
9699
*/
@@ -99,10 +102,10 @@ export type EnvironmentAsyncHook<Callback extends (...args: any[]) => any> = {
99102
* The parameters to pass to each callback
100103
*/
101104
args: Parameters<Callback>;
102-
}) => Promise<T[]>;
105+
}) => Promise<Awaited<ReturnType<Callback>>[]>;
103106
};
104107

105-
export type AsyncHook<Callback extends (...args: any[]) => any> = {
108+
export type AsyncHook<Callback extends (...args: any[]) => T, T = any> = {
106109
/**
107110
* Registers a callback function to be executed when the hook is triggered.
108111
* The callback can be a plain function or a HookDescriptor that includes execution order.
@@ -123,7 +126,9 @@ export type AsyncHook<Callback extends (...args: any[]) => any> = {
123126
* @param params The parameters to pass to each callback
124127
* @returns A promise that resolves with an array containing the results of all callbacks
125128
*/
126-
callBatch: <T = unknown>(...args: Parameters<Callback>) => Promise<T[]>;
129+
callBatch: (
130+
...args: Parameters<Callback>
131+
) => Promise<Awaited<ReturnType<Callback>>[]>;
127132
};
128133

129134
export type ModifyRspackConfigFn = (

0 commit comments

Comments
 (0)