Repository navigation
fix: declare TypedHookMap as an interface - #291
Conversation
`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 detectedLatest commit: 1563b56 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merging this PR will not alter performance1 warning
Comparing |
|
No actionable comments were generated in the recent review. WalkthroughThe public TypeScript declaration changes Merge Risk:
|
The interface only reads `keyof M` and `M[K]`, so `M extends object` is enough and drops the `Hook<any, any>` record constraint.
Summary
tapable.d.tsdeclares a second copy ofHookMap:There is only one runtime class.
lib/index.jsexportsHookMapand noTypedHookMap, sorequire("tapable").TypedHookMapisundefined. 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 aHookMap: each keyKofMmaps to the hookM[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.HookMapstays the only class. Its members are unchanged:name,get,forandintercept, all typed bykeyof M.Its constraint is
M extends objectinstead ofRecord<any, Hook<any, any>>. The interface only readskeyof MandM[K], so it needs nothing about hooks. A tighter constraint withoutanydoesn't work:Hook<never, unknown>,Hook<any, unknown>andHook<unknown[], unknown>all reject real hooks.Record<PropertyKey, unknown>rejects aninterfaceused asM, which has no index signature.The now-unused
AnyHookalias is removed.What kind of change does this PR introduce?
fix (types)
Did you add tests for your changes?
No. Only
tapable.d.tschanges, and the repository has no type tests. I checked it two other ways:new HookMap(…)is assignable to aTypedHookMap<M>, whetherMis a type alias or an interface.for("a")getsM["a"], and an unknown key is rejected.intercept'sfactorygets the key askeyof M.new TypedHookMap(…)is now a type error.TypedHookMaponly as a type, and its full type suite (lint:types-all) passes with this change.npm run lintandnpm testpass.Does this PR introduce a breaking change?
No. Every
Mthe old constraint accepted still satisfiesobject, so type annotations usingTypedHookMapare 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
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 throughHookMap. This aligns the declaration with the available API and preventsTypedHookMapfrom appearing to have a constructor.