{"name":"typed-emitter","version":"1.5.0-from-event","license":"MIT","description":"Strictly typed event emitter interface for TypeScript 3.","author":{"name":"Andy Wermke","url":"https://github.com/andywer"},"repository":{"type":"git","url":"git+https://github.com/andywer/typed-emitter.git"},"keywords":["event","emitter","typescript","interface"],"main":"./types.js","types":"./index.d.ts","optionalDependencies":{"rxjs":"*"},"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\ninterface MessageEvents {\n  error: (error: Error) => void,\n  message: (body: string, from: string) => void\n}\n\nconst messageEmitter = new EventEmitter() as TypedEmitter<MessageEvents>\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) => { /* ... */ })\n\n// TypeScript will catch those mistakes ✋\nmessageEmitter.on(\"error\", (error: string) => { /* ... */ })\nmessageEmitter.on(\"failure\", (error: Error) => { /* ... */ })\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 () => TypedEmitter<MyEvents>) {\n  // ...\n}\n```\n\nAs a generic class:\n\n```ts\nclass MyEventEmitter<T> extends (EventEmitter as { new<T>(): TypedEmitter<T> })<T> {\n  // ...\n}\n```\n\n## RxJS `fromEvent` types inference\n\nThe default `fromEvent` from RxJS will return an `Observable<unknown>` 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 { 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","readmeFilename":"README.md","gitHead":"9587c7c6cff64aaa10cc415361861e8a719a1e62","bugs":{"url":"https://github.com/andywer/typed-emitter/issues"},"homepage":"https://github.com/andywer/typed-emitter#readme","dependencies":{"rxjs":"*"},"_id":"typed-emitter@1.5.0-from-event","_nodeVersion":"16.13.0","_npmVersion":"8.1.0","dist":{"integrity":"sha512-KfCo0Zhj/5HSs5gxBINypaS/vMg/8KwyWeuD3YsBsIT95vRopuYWc5LE7sqpWydzNc6Zd/uQ1EHPZ/dpsmJSzw==","shasum":"ec5bcecbd1c7c702ca2965bdf60f0462875f60ef","tarball":"http://registry-npm.ywwl.com/typed-emitter/download/typed-emitter-1.5.0-from-event.tgz","fileCount":7,"unpackedSize":7220,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIH9Ax6D+4FhZaKgpKOEfZ8UfEFuU63zeKE1DFIki+R9OAiEAtIG+dXRTedDkjGMl8yPt8G3H91IRCKUbkq3xFhKx8kE="}],"size":3114,"noattachment":false,"key":"/typed-emitter/-/typed-emitter-1.5.0-from-event.tgz"},"_npmUser":{"name":"andywer","email":"github@andywer.com"},"directories":{},"maintainers":[{"name":"andywer","email":"github@andywer.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/typed-emitter_1.5.0-from-event_1636833383941_0.8538732069247874"},"_hasShrinkwrap":false,"publish_time":1636833384086,"_cnpm_publish_time":1636833384086,"dist-tags":{"latest":"2.1.0","testing":"1.5.0-from-event"}}