{"id":15365932,"url":"https://github.com/andywer/typed-emitter","last_synced_at":"2025-05-15T20:03:09.980Z","repository":{"id":33049371,"uuid":"150628539","full_name":"andywer/typed-emitter","owner":"andywer","description":"🔩 Type-safe event emitter interface for TypeScript","archived":false,"fork":false,"pushed_at":"2022-12-28T12:40:57.000Z","size":41,"stargazers_count":274,"open_issues_count":15,"forks_count":26,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-08T01:38:23.039Z","etag":null,"topics":["event-emitter","typescript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andywer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-27T18:07:29.000Z","updated_at":"2025-03-17T15:32:34.000Z","dependencies_parsed_at":"2022-08-07T19:30:32.982Z","dependency_job_id":null,"html_url":"https://github.com/andywer/typed-emitter","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andywer%2Ftyped-emitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andywer%2Ftyped-emitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andywer%2Ftyped-emitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andywer%2Ftyped-emitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andywer","download_url":"https://codeload.github.com/andywer/typed-emitter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254414493,"owners_count":22067271,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["event-emitter","typescript"],"created_at":"2024-10-01T13:16:44.699Z","updated_at":"2025-05-15T20:03:07.934Z","avatar_url":"https://github.com/andywer.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Typed-Emitter\n\n[![NPM Version](https://img.shields.io/npm/v/typed-emitter.svg)](https://www.npmjs.com/package/typed-emitter)\n\nStrictly typed event emitter interface for TypeScript.\n\nCode size: Zero bytes - Just the typings, no implementation. Use the default event emitter of the `events` module in node.js or bring your favorite implementation when writing code for the browser.\n\n\n## Installation\n\n```sh\n$ npm install --save-dev typed-emitter\n\n# Using yarn:\n$ yarn add --dev typed-emitter\n```\n\n\n## Usage\n\n```ts\nimport EventEmitter from \"events\"\nimport TypedEmitter from \"typed-emitter\"\n\n// Define your emitter's types like that:\n// Key: Event name; Value: Listener function signature\ntype MessageEvents = {\n  error: (error: Error) =\u003e void,\n  message: (body: string, from: string) =\u003e void\n}\n\nconst messageEmitter = new EventEmitter() as TypedEmitter\u003cMessageEvents\u003e\n\n// Good 👍\nmessageEmitter.emit(\"message\", \"Hi there!\", \"no-reply@test.com\")\n\n// TypeScript will catch those mistakes ✋\nmessageEmitter.emit(\"mail\", \"Hi there!\", \"no-reply@test.com\")\nmessageEmitter.emit(\"message\", \"Hi there!\", true)\n\n// Good 👍\nmessageEmitter.on(\"error\", (error: Error) =\u003e { /* ... */ })\n\n// TypeScript will catch those mistakes ✋\nmessageEmitter.on(\"error\", (error: string) =\u003e { /* ... */ })\nmessageEmitter.on(\"failure\", (error: Error) =\u003e { /* ... */ })\n```\n\n## Extending an emitter\n\nYou might find yourself in a situation where you need to extend an event emitter, but also want to strictly type its events. Here is how to.\n\n```ts\nclass MyEventEmitter extends (EventEmitter as new () =\u003e TypedEmitter\u003cMyEvents\u003e) {\n  // ...\n}\n```\n\nAs a generic class:\n\n```ts\nclass MyEventEmitter\u003cT\u003e extends (EventEmitter as { new\u003cT\u003e(): TypedEmitter\u003cT\u003e })\u003cT\u003e {\n  // ...\n}\n```\n\n## RxJS `fromEvent` types inference\n\nThe default `fromEvent` from RxJS will return an `Observable\u003cunknown\u003e` for our typed emitter.\n\nThis can be fixed by the following code, by replacing the `fromEvent` type with our enhanced one: `FromEvent`:\n\n```ts\nimport { fromEvent as rxFromEvent } from \"rxjs\"\nimport { default as TypedEmitter, FromEvent } from \"typed-emitter/rxjs\"\n\n// The `Observable` typing can be correctly inferenced\nconst fromEvent = rxFromEvent as FromEvent\n```\n\nLearn more from [rxjs fromEvent compatibility #9](https://github.com/andywer/typed-emitter/issues/9)\nfor the `fromEvent` compatibility discussions.\n\n## Why another package?\n\nThe interface that comes with `@types/node` is not type-safe at all. It does not even offer a way of specifying the events that the emitter will emit...\n\nThe `eventemitter3` package is a popular event emitter implementation that comes with TypeScript types out of the box. Unfortunately there is no way to declare the event arguments that the listeners have to expect.\n\nThere were a few other examples of type-safe event emitter interfaces out there as well. They were either not published to npm, had an inconsistent interface or other limitations.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandywer%2Ftyped-emitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandywer%2Ftyped-emitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandywer%2Ftyped-emitter/lists"}