Repository navigation
Conversation
Interfaces have no implicit index signature, so they did not satisfy the `Record<EventType, unknown>` constraint on the Events type parameter (TS2344). Relax the constraint to `object`: type aliases and interfaces are both accepted, while primitives such as string and number are still rejected. Adds an interface case to the type-compilation tests and notes in the README that either `type` or `interface` works.
Author
|
Closing this in favor of #223, which already covers interface event maps. Sorry for the overlap. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is the purpose of this pull request? (put an "X" next to an item)
What changes did you make? (Give an overview)
Fixes #209: an event map declared as an
interfacewas rejected with TS2344 (Type 'Events' does not satisfy the constraint 'Record<EventType, unknown>'. Index signature for type 'string' is missing), because interfaces have no implicit index signature. The README example uses atypealias, which works, but aninterfacewith the exact same shape does not.Eventsconstraint fromRecord<EventType, unknown>toobjectinEventHandlerMap,Emitterandmitt()(src/index.ts). Type aliases and interfaces are now both accepted; primitives such asstringandnumberare still rejected (verified withtsc --strict).test/test-types-compilation.tsas a regression test: it fails with exactly the TypeScript complains when trying the provided typescript example #209 error on the old constraint and passes with this fix.typeorinterface.Is there anything you'd like reviewers to focus on?
Whether relaxing the constraint to
object(rather than e.g. a mapped-type constraint) is the direction you prefer. I triedRecord<keyof Events, unknown>first: it still rejects optional properties (Property 'bar' is optional ... but required in type 'Record<...>') and it also lets primitives through, soobjectis the tighter of the two working options.Does this PR introduce a breaking change?
No. It only accepts event-map types that were previously rejected; all previously valid code still compiles. Runtime code is untouched (built output is byte-identical: 190 B gz).
Validation: scoped
tsc --strictruns (the repo's owntsc --noEmitfails on a clean tree too: the pinned@types/node@26.6.6uses syntax TS 4.9.5 cannot parse — pre-existing, unrelated); type-compilation tests pass with the fix and fail without it; built CJS runtime smoke-tested (on/emit/off/*); mocha suite cannot run on Node 24 (the oldesmloader crashes identically on a clean tree — pre-existing, unrelated).