Skip to content

Commit

Permalink
Initial implementation, README and metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
andywer committed Sep 27, 2018
0 parents commit 33b993b
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
@@ -0,0 +1,6 @@
dist/*
!dist/*.d.ts
node_modules/
.DS_Store
Thumbs.db
*.log
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 Andy Wermke

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
59 changes: 59 additions & 0 deletions README.md
@@ -0,0 +1,59 @@
# Typed-Emitter

Strictly typed event emitter interface for **TypeScript 3**.

Code 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.


## Installation

```sh
$ npm install typed-emitter

# Using yarn:
$ yarn add typed-emitter
```


## Usage

```ts
import EventEmitter from "events"
import TypedEmitter from "typed-emitter"

// Define your emitter's types like that:
// Key: Event name; Value: Listener function signature
interface MessageEvents {
error: (error: Error) => void,
message: (body: string, from: string) => void
}

const messageEmitter = new EventEmitter() as TypedEmitter<MessageEvents>

// Good 👍
messageEmitter.emit("message", "Hi there!", "no-reply@test.com")

// TypeScript will catch those mistakes ✋
messageEmitter.emit("mail", "Hi there!", "no-reply@test.com")
messageEmitter.emit("message", "Hi there!", true)

// Good 👍
messageEmitter.on("error", (error: Error) => { /* ... */ })

// TypeScript will catch those mistakes ✋
messageEmitter.on("error", (error: string) => { /* ... */ })
messageEmitter.on("failure", (error: Error) => { /* ... */ })
```

## Why another package?

The 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...

The `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.

There 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.


## License

MIT
42 changes: 42 additions & 0 deletions index.d.ts
@@ -0,0 +1,42 @@
type Arguments<T> = [T] extends [(...args: infer U) => any]
? U
: [T] extends [void] ? [] : [T]

/**
* Type-safe event emitter.
*
* Use it like this:
*
* interface MyEvents {
* error: (error: Error) => void
* message: (from: string, content: string) => void
* }
*
* const myEmitter = new EventEmitter() as TypedEmitter<MyEvents>
*
* myEmitter.on("message", (from, content) => {
* // ...
* })
*
* myEmitter.emit("error", "x") // <- Will catch this type error
*/
interface TypedEventEmitter<Events> {
addListener<E extends keyof Events> (event: E, listener: Events[E]): this
on<E extends keyof Events> (event: E, listener: Events[E]): this
once<E extends keyof Events> (event: E, listener: Events[E]): this
prependListener<E extends keyof Events> (event: E, listener: Events[E]): this
prependOnceListener<E extends keyof Events> (event: E, listener: Events[E]): this

removeAllListeners<E extends keyof Events> (event: E): this
removeListener<E extends keyof Events> (event: E, listener: Events[E]): this

emit<E extends keyof Events> (event: E, ...args: Arguments<Events[E]>): boolean
eventNames (): (keyof Events)[]
listeners<E extends keyof Events> (event: E): Function[]
listenerCount<E extends keyof Events> (event: E): number

getMaxListeners (): number
setMaxListeners (maxListeners: number): this
}

export default TypedEventEmitter
17 changes: 17 additions & 0 deletions package.json
@@ -0,0 +1,17 @@
{
"name": "typed-emitter",
"version": "0.1.0",
"license": "MIT",
"description": "Strictly typed event emitter interface for TypeScript 3.",
"author": "Andy Wermke (https://github.com/andywer)",
"repository": "github:andywer/typed-emitter",
"keywords": [
"event",
"emitter",
"typescript",
"interface"
],
"peerDepedencies": {
"typescript": ">= 3.0"
}
}

0 comments on commit 33b993b

Please sign in to comment.