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

fix: type infer for plugin hooks #4571

Merged
merged 1 commit into from
Feb 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export type HookDescriptor<T extends (...args: any[]) => any> = {
order: HookOrder;
};

export type EnvironmentAsyncHook<Callback extends (...args: any[]) => any> = {
export type EnvironmentAsyncHook<
Callback extends (...args: any[]) => T,
T = any,
> = {
/**
* Registers a callback function to be executed when the hook is triggered.
* The callback can be a plain function or a HookDescriptor that includes execution order.
Expand Down Expand Up @@ -90,7 +93,7 @@ export type EnvironmentAsyncHook<Callback extends (...args: any[]) => any> = {
* Each callback receives the original parameters, and their results don't affect subsequent callbacks.
* @returns A promise that resolves with an array containing the results of all callbacks
*/
callBatch: <T = unknown>(params: {
callBatch: (params: {
/**
* Specify the environment for filtering callbacks.
*/
Expand All @@ -99,10 +102,10 @@ export type EnvironmentAsyncHook<Callback extends (...args: any[]) => any> = {
* The parameters to pass to each callback
*/
args: Parameters<Callback>;
}) => Promise<T[]>;
}) => Promise<Awaited<ReturnType<Callback>>[]>;
};

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

export type ModifyRspackConfigFn = (
Expand Down
Loading