diff options
| author | Severin <[email protected]> | 2017-02-25 22:24:28 +0100 |
|---|---|---|
| committer | Jason Miller <[email protected]> | 2017-02-25 16:24:28 -0500 |
| commit | d63b350e6de3360d05ed6360cfaf101017237a73 (patch) | |
| tree | 4ea94af3c01e2c444a00442e22832bd3532faa3c | |
| parent | e428f3a171a286ca136f6aa08ac84ea4a892282e (diff) | |
Typescript definitions (#25)
* Added type definitions
* Handler only has exactly one or no argument
* Moved type defs to root folder
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | mitt.d.ts | 46 | ||||
| -rw-r--r-- | package.json | 4 | ||||
| -rw-r--r-- | typings.json | 5 |
4 files changed, 62 insertions, 1 deletions
@@ -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"> @@ -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 diff --git a/mitt.d.ts b/mitt.d.ts new file mode 100644 index 0000000..41ea221 --- /dev/null +++ b/mitt.d.ts @@ -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; + } +} diff --git a/package.json b/package.json index bf13c22..5f84071 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ "license": "MIT", "files": [ "src", - "dist" + "dist", + "mitt.d.ts" ], "eslintConfig": { "parser": "babel-eslint", @@ -48,6 +49,7 @@ "semi": [2, "always"] } }, + "typings": "./mitt.d.ts", "devDependencies": { "babel-core": "^6.9.1", "babel-eslint": "^7.1.1", diff --git a/typings.json b/typings.json new file mode 100644 index 0000000..ad8ab5f --- /dev/null +++ b/typings.json @@ -0,0 +1,5 @@ +{ + "name": "mitt", + "main": "mitt.d.ts", + "version": false +}
\ No newline at end of file |
