{"name":"typed-emitter","version":"1.3.0","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"],"gitHead":"b6a1cd7c040e40ffbfa4a188785c019c084ce50a","bugs":{"url":"https://github.com/andywer/typed-emitter/issues"},"homepage":"https://github.com/andywer/typed-emitter#readme","_id":"typed-emitter@1.3.0","_nodeVersion":"12.16.3","_npmVersion":"6.14.4","dist":{"integrity":"sha512-sLA7oQv0OUgAbbWeadAm8OpzssufUxXof6O50JaBX6guZrRi2XbhCdKO321TleFN45oL/WdiR1UsKF6eUTwpZg==","shasum":"cb49ce288c9f066afc22ffe236a88879163eb0ee","tarball":"http://registry-npm.ywwl.com/typed-emitter/download/typed-emitter-1.3.0.tgz","fileCount":4,"unpackedSize":5312,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfOQUyCRA9TVsSAnZWagAAkcoP/2Hs39RLAHzVqtAiUFeJ\ng+Hnvarx/FccdRAzzA9TL5lVevbYU5Tn6Cg4547KY+tgUUHMc4m7/+OFfoZj\npMdczpHmSSU1xO2LEDqsmpUcZ0Wj7QpXoAjQxLeE0ngJLD6HXIPeb1ky9Tr1\n10B1WMMq9/R57jHLKq/PhP2vBuCyxzSK1neS3lA1B2FVU3uOzO8J2yotF9Go\nKleS9naVHK/eDES2SpzSEuNXFkUxuk/hjOAlkrPrJ/5LILL8JtB4OLTfpa0O\nRWChuSE+RSajeogTkr2ftTdk/RQ7z8sddim3Ej57He37YcAD7Kml/v8AVMv8\nC1Pc9s2NBQsNezxkYOXtvnW6o9ePF/b/xzZ/kUPrlQWNJuha/sVCOIKhmv43\nHZhsdvFsqp2LFh3FsPv7uB98hL6il4IAj6e6/DtgTpK0QJ/KrrKuS0HcJrSe\nGC0jrv3G8XNKeRd403gl5Rl76kohLy4UjTd2HKxprJg8ogQvXR3TWKp1jryy\nRRHoTfNmu4zXwu3PEbk1mjnzPmQBCsmDnlWKqiI/Ugn0JRzVP6ZiYFkmBHoD\nJnOHN+0cBhdp+INFs4iGNONndzjSEPHNDMcXAOZ2LHi+W+9234S3kcpTegrb\nQvTbWwLdqxK1uKxzF8omX7RBA9mKLpBdQ8VnLqLav2Qzcsp60Gso7EYQfuqO\ngtWi\r\n=Pl/h\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBPprhfZ3q7QRwpC3uqUoArsq3P6degmPPd+aXgVZ98KAiBEtLfwG7OE6fSoeByRf4YdJtQqzHGHOBjc44ob/vOibw=="}],"size":2381,"noattachment":false,"key":"/typed-emitter/-/typed-emitter-1.3.0.tgz"},"maintainers":[{"name":"wyantb","email":"wyantb@gmail.com"}],"_npmUser":{"name":"andywer","email":"github@andywer.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/typed-emitter_1.3.0_1597572401696_0.4017756981930143"},"_hasShrinkwrap":false,"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) => 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 { 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","publish_time":1597572401866,"_cnpm_publish_time":1597572401866,"dist-tags":{"latest":"2.1.0","testing":"1.5.0-from-event"}}