Skip to content

Commit c9181b0

Browse files
feat(types): added TypedHookMap type (#195)
1 parent 3fc6a06 commit c9181b0

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

tapable.d.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ export class AsyncSeriesWaterfallHook<
141141
AdditionalOptions = UnsetAdditionalOptions
142142
> extends AsyncHook<T, R, AdditionalOptions> {}
143143

144-
type HookFactory<H> = (key: any, hook?: H) => H;
144+
type HookFactory<H, K = any> = (key: K) => H;
145145

146-
interface HookMapInterceptor<H> {
147-
factory?: HookFactory<H>;
146+
interface HookMapInterceptor<H, K = any> {
147+
factory?: (key: K, hook: H) => H;
148148
}
149149

150150
export class HookMap<H> {
@@ -155,6 +155,16 @@ export class HookMap<H> {
155155
intercept(interceptor: HookMapInterceptor<H>): void;
156156
}
157157

158+
type AnyHook = Hook<any, any>;
159+
160+
export class TypedHookMap<M extends Record<any, AnyHook>> {
161+
constructor(factory: HookFactory<M[keyof M], keyof M>, name?: string);
162+
name: string | undefined;
163+
get<K extends keyof M>(key: K): M[K] | undefined;
164+
for<K extends keyof M>(key: K): M[K];
165+
intercept(interceptor: HookMapInterceptor<M[keyof M], keyof M>): void;
166+
}
167+
158168
export class MultiHook<H> {
159169
constructor(hooks: H[], name?: string);
160170
name: string | undefined;

0 commit comments

Comments
 (0)