Skip to content

fix(types): allow interfaces as event maps (fixes #209) - #225

Closed
Ultroncn wants to merge 1 commit into
developit:mainfrom
Ultroncn:fix/allow-interface-event-maps
Closed

Ultroncn wants to merge 1 commit into
developit:mainfrom
Ultroncn:fix/allow-interface-event-maps

Conversation

@Ultroncn

Copy link
Copy Markdown

What is the purpose of this pull request? (put an "X" next to an item)

  • Bug fix

What changes did you make? (Give an overview)

Fixes #209: an event map declared as an interface was 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 a type alias, which works, but an interface with the exact same shape does not.

  • Relaxed the Events constraint from Record<EventType, unknown> to object in EventHandlerMap, Emitter and mitt() (src/index.ts). Type aliases and interfaces are now both accepted; primitives such as string and number are still rejected (verified with tsc --strict).
  • Added an interface event map to test/test-types-compilation.ts as 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.
  • Noted in the README TypeScript section that event maps can be declared with either type or interface.

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 tried Record<keyof Events, unknown> first: it still rejects optional properties (Property 'bar' is optional ... but required in type 'Record<...>') and it also lets primitives through, so object is 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 --strict runs (the repo's own tsc --noEmit fails on a clean tree too: the pinned @types/node@26.6.6 uses 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 old esm loader crashes identically on a clean tree — pre-existing, unrelated).

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.
@Ultroncn

Copy link
Copy Markdown
Author

Closing this in favor of #223, which already covers interface event maps. Sorry for the overlap.

@Ultroncn Ultroncn closed this Nov 19, 2026
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.

TypeScript complains when trying the provided typescript example

1 participant