Skip to content

fix: declare TypedHookMap as an interface - #291

Merged
alexander-akait merged 2 commits into
mainfrom
fix/typed-hook-map-interface
Nov 13, 2026
Merged

alexander-akait merged 2 commits into
mainfrom
fix/typed-hook-map-interface

Conversation

@alexander-akait

@alexander-akait alexander-akait commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Summary

tapable.d.ts declares a second copy of HookMap:

export class HookMap<H> { … }                                  // keys: any
export class TypedHookMap<M extends Record<any, Hook<any, any>>> { constructor(…); … } // keys: keyof M

There is only one runtime class. lib/index.js exports HookMap and no TypedHookMap, so require("tapable").TypedHookMap is undefined. Because it's declared as a class, new TypedHookMap(…) type-checks and then throws at runtime. What it really is is a typed view of a HookMap: each key K of M maps to the hook M[K]. That's how webpack uses it, e.g. /** @type {TypedHookMap<…>} */ (new HookMap(…)).

Two changes:

  • It's now an interface, with a doc comment saying so. HookMap stays the only class. Its members are unchanged: name, get, for and intercept, all typed by keyof M.

  • Its constraint is M extends object instead of Record<any, Hook<any, any>>. The interface only reads keyof M and M[K], so it needs nothing about hooks. A tighter constraint without any doesn't work:

    • Hooks are invariant in their argument and result types, so Hook<never, unknown>, Hook<any, unknown> and Hook<unknown[], unknown> all reject real hooks.
    • Record<PropertyKey, unknown> rejects an interface used as M, which has no index signature.

    The now-unused AnyHook alias is removed.

What kind of change does this PR introduce?

fix (types)

Did you add tests for your changes?

No. Only tapable.d.ts changes, and the repository has no type tests. I checked it two other ways:

  • A consumer file type-checked against the declarations.
    • new HookMap(…) is assignable to a TypedHookMap<M>, whether M is a type alias or an interface.
    • for("a") gets M["a"], and an unknown key is rejected.
    • intercept's factory gets the key as keyof M.
    • new TypedHookMap(…) is now a type error.
  • webpack type-checked against it. webpack uses TypedHookMap only as a type, and its full type suite (lint:types-all) passes with this change.

npm run lint and npm test pass.

Does this PR introduce a breaking change?

No. Every M the old constraint accepted still satisfies object, so type annotations using TypedHookMap are unchanged. Only uses of it as a value (new TypedHookMap(), extends TypedHookMap, instanceof TypedHookMap) stop type-checking, and those already threw at runtime because no such value is exported.

If relevant, what needs to be documented once your changes are merged or what have you already documented?

n/a

Use of AI

Written with Claude Code (investigation, implementation, verification against webpack and this description), directed and reviewed by me.

🤖 Generated with Claude Code

https://claude.ai/code/session_01HArJAXZagrHVVNLZZk9soh

Summary by CodeRabbit

  • Bug Fixes
    • Corrected the public TypeScript declaration for TypedHookMap: it is now represented as an interface rather than a constructible class. Its typed lookup methods and keyed interceptor signature remain available, and instances are created through HookMap. This aligns the declaration with the available API and prevents TypedHookMap from appearing to have a constructor.

`tapable.d.ts` declared `TypedHookMap` as an exported class, but `lib/index.js`
exports no such value, so `new TypedHookMap()` type-checked and then threw. It is
a typed view of `HookMap`, so it is now an interface; a map is still created
with `new HookMap()` and annotated as one.
@changeset-bot

changeset-bot Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1563b56

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
tapable Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.25%. Comparing base (9ada8a4) to head (1563b56).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #291   +/-   ##
=======================================
  Coverage   99.25%   99.25%           
=======================================
  Files          15       15           
  Lines         805      805           
  Branches      140      140           
=======================================
  Hits          799      799           
  Misses          6        6           
Flag Coverage Δ
integration 99.25% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codspeed

codspeed Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 6 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 92 untouched benchmarks


Comparing fix/typed-hook-map-interface (1563b56) with main (25d4f1b)

Open in CodSpeed

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The public TypeScript declaration changes TypedHookMap from a class to an interface. Its generic constraint changes from Record<any, AnyHook> to object, and its constructor declaration is removed. The documentation describes its mapping and notes that instances are created with new HookMap(). A patch changeset is added.

Merge Risk: Minimal · up to 1563b

This change fixes a TypeScript declaration so that TypedHookMap is no longer presented as a constructible class that does not exist at runtime. Type annotations keep working, and only code that already failed at runtime will now fail at compile time. The merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "fix: declare TypedHookMap as an interface" matches the main change. The PR changes TypedHookMap in tapable.d.ts from an exported class declaration to a type-only interface, and the changese…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title "fix: declare TypedHookMap as an interface" matches the main change. The PR changes TypedHookMap in tapable.d.ts from an exported class declaration to a type-only interface, and the changeset records a patch release for this fix. The title is specific and short, and it does not describe anything absent from the changeset.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4f9a4fb6-6e7a-47d7-9488-7e38bf24751d
Commits

Reviewing files that changed from the base of the PR and between 7a43a08 and 1563b56.

Files selected for processing: 1

  • tapable.d.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

The interface only reads `keyof M` and `M[K]`, so `M extends object` is
enough and drops the `Hook<any, any>` record constraint.
@alexander-akait
alexander-akait merged commit 036de48 into main Nov 13, 2026
36 checks passed
@alexander-akait
alexander-akait deleted the fix/typed-hook-map-interface branch November 13, 2026 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant