unmatched-event
A listener for a name nothing in the program emits on that emitter, and an emit nobody listens for. When one of each differ by a typo (one or two letters, case, a separator), both findings name the other site: that is the bug the two usually are. On by default, as an error. Reported by safe-ts check only: the two ends are in different files.
Why: an event's name is a string at both ends, and neither end knows the other. Rename it at one end, or mistype it, and everything still compiles and runs: the listener never fires, and the emit reaches no one. A typed event map (new EventEmitter<Events>() from node:events) types what each event carries, but @types/node lets any other name through at both ends, so it doesn't catch this.
✅ Accepted: one name, written once, used by both ends
ts
import { EventEmitter } from "node:events";
export const UserCreated = "user:created";
export type Events = { [UserCreated]: [id: string] };
export class Users {
readonly events = new EventEmitter<Events>();
create(id: string): void {
this.events.emit(UserCreated, id);
}
}
export function wire(users: Users): () => void {
const log = (id: string): void => console.log(id);
users.events.on(UserCreated, log);
return () => users.events.off(UserCreated, log);
}❌ Rejected: the names differ by one letter; neither end works
ts
import { EventEmitter } from "node:events";
const bus = new EventEmitter();
export function wire(): void {
bus.on("user:created", (id: string) => console.log(id));
}
export function create(id: string): void {
bus.emit("user:create", id);
}text
Nothing in the program emits `user:created` on `bus`: this listener never runs. `bus.emit(...)`
at src/events.ts:9:12 emits `user:create`, a near miss: one of the two names is wrong
Nothing in the program listens for `user:create` on `bus`: this event reaches no one. The
listener at src/events.ts:5:10 waits for `user:created`, a near miss: one of the two names is wrongWhich emitter is "the same"
What the source writes down, followed across files:
| The receiver | is the emitter |
|---|---|
a module-level instance of a library emitter (export const bus = new EventEmitter()), through every import | that binding |
this in a class whose chain ends in EventEmitter or EventTarget, new Store(), a value or field declared Store | the topmost project class of the chain (a subclass's listeners meet its parent's emits) |
a field holding a library emitter (private readonly progress = new EventEmitter()), on any instance (this.progress, upload.progress) | that field of that class |
a DOM node, document, window | the document: events bubble, so a custom event is heard on an ancestor |
A name is a literal, a const holding one, or a member of a const object (Topics.Opened), in the file or imported. Both ends are reported only where the program is all that can emit and listen there: a plain EventEmitter or EventTarget, or a class of the program whose chain adds nothing else, that no code the checker can't see gets hold of. Not reported:
- an emitter handed on: a binding or a local passed to a function, returned or stored (in its own file or one importing it), a field read as a value anywhere (
keep(channel.events)), an emitter exported by a file the outside uses (an entry, a package's API; aprivateor#field isn't reachable from there): that code may listen, or emit; - on instances of a class (which go anywhere), a name some site with no key uses at the other end: a receiver nothing writes down (a parameter declared
EventEmitter, what a library returns) may be one of them. A binding, local or field that isn't handed on is reached only by its name, so no such site can be it; - an emitter's own events (
error,newListener,removeListener); - on the document, a dispatch of an event the platform has (
input,change: dispatched by hand for a framework to hear).
A computed name at either end (emit(name)) stops the reports of the other direction: any name may be meant, on that emitter (and, at a site with no key, on every class's instances). A class whose chain ends in a library class other than EventEmitter and EventTarget (a stream, a socket, a server) emits events of its own: unknown-event checks names against its event list instead. A near miss is looked for among every name the other end uses, not only the unmatched ones (once("itm") next to an emit("item") that a listener hears).
What it can't see
- An emitter passed around untyped, or kept in a collection: its sites have no key, and only count as "some site uses this name".
- Listeners and emitters outside the program (another process, a library's own
emit): a library class's events need its event list: the browser and Node packs have the platform's (seeunknown-event), other libraries' are not known. - Handing on through a path it doesn't follow: an emitter put in an object literal, a closure returned without the emitter itself being a value (
return () => bus.emit("x")is fine), or reached by a computed member. - Events of
EventTargets made by a helper (makeTarget()), andonce(emitter, name)/on(emitter, name)fromnode:events(functions, not methods).