{"name":"typed-emitter","version":"1.4.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"],"main":"./types.js","types":"./index.d.ts","gitHead":"d74555b38c8c891c46ace12fcbb84a7871b505f4","bugs":{"url":"https://github.com/andywer/typed-emitter/issues"},"homepage":"https://github.com/andywer/typed-emitter#readme","_id":"typed-emitter@1.4.0","_nodeVersion":"12.22.3","_npmVersion":"6.14.13","dist":{"integrity":"sha512-weBmoo3HhpKGgLBOYwe8EB31CzDFuaK7CCL+axXhUYhn4jo6DSkHnbefboCF5i4DQ2aMFe0C/FdTWcPdObgHyg==","shasum":"38c6bf1224e764906bb20cb0b458fa914100607c","tarball":"http://registry-npm.ywwl.com/typed-emitter/download/typed-emitter-1.4.0.tgz","fileCount":5,"unpackedSize":5437,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJh2+mYCRA9TVsSAnZWagAA8RcP/AlGft57p2T/xIw+HbSN\ndjzudJLY428UD6ttH/T0jH4iPH53lOSkuu8IEtESJztoW0/YelEV+SbrZxiV\nk1NhrCKtnl4fvbONNCCf+j2Dt5wLCH9xglqI6UNxr31vy0k8MPYhuk47uirt\nV3+0uZYgGATBNe9RCh4TroWacjeN+59KkZex514ZGH/kRqHIel4MiqLJACgP\nLRGKNE2QgtSA7VNFOcLyPx7LP0cfeCGfo62nFgt2d3LfiMYL8tW+EUICOq5C\nUzMYr/1LS0sJH5TD/ptx9q7qACFhbQBX6lHgOreU2ncLTaglv+9KMbK+QHoR\n+B4sTVSu3+PEusHk0Et1FfhlgZruIxkOSNRvy4L+vFnemTB/SDf/ZIriV5/c\nHh0SrwXWzIruoULgDXRNX2O3bYcOPwo3lJn9AzlJm6LVdnpIN6Y3JQjoRgji\nQuaiXt2nyprH0zGjXrgfbxlBBYJrpcpffshsvMVEp/cFuoC0qKkmITZmQCIg\nBuQIMwBuBoVt5k5Lcmoh8qRdgLviAsUKzIV4BHVnqy4Qa7Cq09lzDKglKQKY\nbmFnzJu0vwXHbWPVqCSbbeNjC8gjXwFYrBzTIWT2IDg7yVrAbrJW8aivNCYJ\nlIO9N3aMTYK52oO/hE5DiYEScG0AN7YRQ1lu4lexQOjVK1Ka1pgbtA3wYoCn\nP+fq\r\n=yAov\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCM/4HNn2MSGhnwm+vZvTvM/wIMcqnFXbKwTGf6lD/hHgIhAMMwEatlOr6VGZtaiVPzInYztrA5CSSiTMS4PH51qodk"}],"size":2469,"noattachment":false,"key":"/typed-emitter/-/typed-emitter-1.4.0.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.4.0_1634201029723_0.9554556113631574"},"_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":1634201029858,"_cnpm_publish_time":1634201029858,"dist-tags":{"latest":"2.1.0","testing":"1.5.0-from-event"}}