{"name":"typed-emitter","version":"0.2.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":"1f9fda93c760c36b7f40a2658d184e5ee307170f","bugs":{"url":"https://github.com/andywer/typed-emitter/issues"},"homepage":"https://github.com/andywer/typed-emitter#readme","_id":"typed-emitter@0.2.0","_nodeVersion":"10.16.3","_npmVersion":"6.9.0","dist":{"integrity":"sha512-bq2aUggcZW+YM2wUHVLzGJ2Zvxr63oM6CJJXwioy3xIWyRWr7cmph4iSEZPWX67+qpMKETZQc0Bp55FwcNCmXg==","shasum":"ede9a20a3aa78cb853c2866ddad968a3925830e1","tarball":"http://registry-npm.ywwl.com/typed-emitter/download/typed-emitter-0.2.0.tgz","fileCount":4,"unpackedSize":4809,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdrgfsCRA9TVsSAnZWagAAtzAP/2n/I8zijJRLKMroLjMv\nQzOpdxEH+IwM1mtyuDgcr9Jrzr83uYyMuVe0ViSJn4sdxr61I6uazSzVMeiU\nxev0khRGa1LHdMIhYkfjyevhvuOv0234C1CkQhCrE4pXbEUC6nkPRuvysVb5\nEzLuuq/UEpFrZV4sE4ei8D2+tz9f71T1IsHQgdHG92mC9w4bisgUqDuqZCTq\nOxXX7TCWHd9CpDMa1NjcDB+npGQ3GRFknDMPt+LT2EYIBq3y6birTUEoaUuQ\nEbqUnS32VLrWKkmwJv3Np0nExLfrgCeVpu5ub9GdrVq/Faz25Ih+ot/lyV4o\n0bjfC3LKQ1hvcY4fgPi7AqGT/ef7j0BzhiP3mPA7vP21i9nm18+0bNic3xmE\nILJ6YM6oGkybT37ldk7mbv3T1b8buE2QOypMXLtmj3rSLtC3bCIztvDIJBc+\no9bB2N2yV9z+Zyqmgkguk5HmaucCeDvrdjsLFQy/dT3jFMXG4Ig+9Yr+SgZ1\nYDRoj2dIoNp9uQQ6hJenPf01fqor2OqzZYJ85auP6cZuiERMmB2Gx10fGZeU\nOm1LAmiYooZdJLcqgcCuPrhf30XIlX4ug97dx4HCfCMtLAIACk+A19cqvBme\nFGjtT9YpXBqazeSyUWdpZccBkhKM4pNYoUyS3XjAnL2oEHL0c3xUQP9fhB8l\nhUoH\r\n=VQNL\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIG9IyKDPeLWS/4AL+V6/zScsZrxEZQ8MO51s7i1cWNqRAiEAt1IhjOAe6yPeFWxia3F6z5u+ilmjoTRErM99iaYDomA="}],"size":2214,"noattachment":false,"key":"/typed-emitter/-/typed-emitter-0.2.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_0.2.0_1571686379613_0.2601200771621084"},"_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":1571686379749,"_cnpm_publish_time":1571686379749,"dist-tags":{"latest":"2.1.0","testing":"1.5.0-from-event"}}