Skip to content

Commit

Permalink
Typescript definitions (#25)
Browse files Browse the repository at this point in the history
* Added type definitions

* Handler only has exactly one or no argument

* Moved type defs to root folder
  • Loading branch information
seveves authored and developit committed Feb 25, 2017
1 parent e428f3a commit d63b350
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ emitter.on('foo', onFoo) // listen
emitter.off('foo', onFoo) // unlisten
```

### Typescript
```ts
import * as mitt from 'mitt';
let emitter: mitt.Emitter = new mitt();
```

## Examples & Demos

<a href="http://codepen.io/developit/pen/rjMEwW?editors=0110">
Expand All @@ -79,6 +85,8 @@ emitter.off('foo', onFoo) // unlisten
<img src="https://i.imgur.com/CjBgOfJ.png" width="278" alt="preact + mitt preview">
</a>

* * *

## API

### mitt
Expand Down
46 changes: 46 additions & 0 deletions mitt.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
declare var mitt: mitt.MittStatic;

declare module "mitt" {
export = mitt;
}

declare namespace mitt {
type Handler = (event?: any) => void;

interface MittStatic {
new(all?: {[key: string]: Handler}): Emitter;
}

interface Emitter {
/**
* Register an event handler for the given type.
*
* @param {string} type Type of event to listen for, or `"*"` for all events.
* @param {Handler} handler Function to call in response to the given event.
*
* @memberOf Mitt
*/
on(type: string, handler: Handler): void;

/**
* Function to call in response to the given event
*
* @param {string} type Type of event to unregister `handler` from, or `"*"`
* @param {Handler} handler Handler function to remove.
*
* @memberOf Mitt
*/
off(type: string, handler: Handler): void;

/**
* Invoke all handlers for the given type.
* If present, `"*"` handlers are invoked prior to type-matched handlers.
*
* @param {string} type The event type to invoke
* @param {any} [event] An event object, passed to each handler
*
* @memberOf Mitt
*/
emit(type: string, event?: any): void;
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"license": "MIT",
"files": [
"src",
"dist"
"dist",
"mitt.d.ts"
],
"eslintConfig": {
"parser": "babel-eslint",
Expand All @@ -48,6 +49,7 @@
"semi": [2, "always"]
}
},
"typings": "./mitt.d.ts",
"devDependencies": {
"babel-core": "^6.9.1",
"babel-eslint": "^7.1.1",
Expand Down
5 changes: 5 additions & 0 deletions typings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "mitt",
"main": "mitt.d.ts",
"version": false
}

0 comments on commit d63b350

Please sign in to comment.