{"name":"typed-emitter","version":"1.0.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":"2942782ce940518e2e0841ac117ef178268ef493","bugs":{"url":"https://github.com/andywer/typed-emitter/issues"},"homepage":"https://github.com/andywer/typed-emitter#readme","_id":"typed-emitter@1.0.0","_nodeVersion":"10.16.3","_npmVersion":"6.14.2","dist":{"integrity":"sha512-bZS13Bz8kgF1kxJ370V7QMwzpqlrVJ4cu9bsBWRqDL2kzKLcx0vCDPkZ0NgJ31HZ4tXFSOxChXii6YLG6Rqz4A==","shasum":"e21a0151539fc6f61e1f59b0d3655f6879b59eb5","tarball":"http://registry-npm.ywwl.com/typed-emitter/download/typed-emitter-1.0.0.tgz","fileCount":4,"unpackedSize":4810,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJegNM1CRA9TVsSAnZWagAA8PYP/3zTThvqGw6crcPt4pxQ\nA56Rn97ch5he0E9xxLjajlN+j0e3M1hK29p7l//UZv+TPoqkdEzDyKd81CNm\nAa8MtcVu16A5iGBi3Gk71pZtxaVeIFykOaDfRlkifYkLzZ9GAm6t9Qo+SpY2\n0JBuCWh1mY2a2B/qJGPbYsMkUjcEIP4G1DlbM0GNVeh4zllMLPXwmuYSAK+i\ntgvgRbz4JPU/A+JfMtUCsuAoXunlFuWCc5kqSsUtqqYRlrBJWGtWunV/2Sv9\n3OYusONQ3FDVb5kN+obw7fWwEWNnqlW/hYxDEgHPp49MAIYUhRvmqFcfebZr\nUxwkDuKp5ZHZNV8KGn7tqxw09GhQ5BOFnwi1MTRWXzCht+vNt52S1M+CYezr\nt2AeC4nnUYDmPgV7JbQfb6Dly57PAwrW85VM1ICmS2IFLNh+cWdwRH7dDKwf\nD4utd1Pc9u7MxZGcr7xOtQAgPdw+ewkDUn/srThpVbMfHTYqnJpeK7HT3KY2\nVgl5VAk5sCv9ituU7MHQMta6rteNtf6kIjVfzz/5d0IcB+E20UZmZ3JoNnvn\nB/pGjegKMGdd81jIYrNtpZ8hB2BVHAp1eRNyzA8pnhdWyc0L82z4mX9hay6I\nF5+1p/8vlEeRNg4PoPgYEvrdgon+bDwMo7Bce3Y8vw4XlnABVSXM7AhO1WR7\ncx7z\r\n=8Edz\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDMOpIwFkzc+crwhIBk/G6lgld0eoowwxROMfACWeQNSAIhAPTmg3IHpZG8qGXcwtGThzKL9Uemfhkl5dRmRxiIsOYg"}],"size":2206,"noattachment":false,"key":"/typed-emitter/-/typed-emitter-1.0.0.tgz"},"maintainers":[{"name":"wyantb","email":"wyantb@gmail.com"}],"_npmUser":{"name":"andywer","email":"andy.dev@next-step-software.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/typed-emitter_1.0.0_1585500981308_0.3917390857291161"},"_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":1585500981470,"_cnpm_publish_time":1585500981470,"dist-tags":{"latest":"2.1.0","testing":"1.5.0-from-event"}}