Skip to content
Myr

Stronger typing (#114)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* improved typing * upd readme * upd readme * upd test & readme * removed magic 1 * removed ts-toolbelt dependency

authored by iyegoroff · committed by GitHub · parent 22c5dcb · Browse files · 7 files+138 added−65 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+138 added−65 removed

  • tests:+72 added−26 removed
  • other:+66 added−39 removed

7 files+138 added−65 removed

modified:src/index.ts+45 added-26 removed
modified:test/index_test.ts+17 added-6 removed
1import mitt, { Emitter } from '..';1import mitt, { Emitter, EventHandlerMap } from '..';2import chai, { expect } from 'chai';3import { spy } from 'sinon';4import sinonChai from 'sinon-chai';5chai.use(sinonChai);
8 unmodified lines
14 const map = new Map();15 const a = spy();16 const b = spy();17 map.set('foo', [a, b]);18 const events = mitt(map);18 const events = mitt<{ foo: undefined }>(map);19 events.emit('foo');20 expect(a).to.have.been.calledOnce;21 expect(b).to.have.been.calledOnce;22 });23});2425describe('mitt#', () => {26 let events, inst: Emitter;26 const eventType = Symbol('eventType');27 type Events = {28 foo: unknown;29 constructor: unknown;30 FOO: unknown;31 bar: unknown;32 Bar: unknown;33 'baz:bat!': unknown;34 'baz:baT!': unknown;35 Foo: unknown;36 [eventType]: unknown;37 };38 let events: EventHandlerMap<Events>, inst: Emitter<Events>;3928 beforeEach( () => {40 beforeEach(() => {41 events = new Map();42 inst = mitt(events);43 });44
49 unmodified lines
94 });9596 it('can take symbols for event types', () => {97 const foo = () => {};86 const eventType = Symbol('eventType');98 inst.on(eventType, foo);99 expect(events.get(eventType)).to.deep.equal([foo]);100 });101
59 unmodified lines
161162 it('should invoke handler for type', () => {163 const event = { a: 'b' };164154 inst.on('foo', (one, two?) => {165 inst.on('foo', (one, two?: unknown) => {166 expect(one).to.deep.equal(event);167 expect(two).to.be.an('undefined');168 });169
32 unmodified lines
modified:test/test-types-compilation.ts+55 added-20 removed
modified:.gitignore+1 added
4 unmodified lines
5/index.d.ts6package-lock.json7.DS_Store8.idea9.vscode
modified:package.json+5 added-4 removed
10 unmodified lines
11 "typings": "index.d.ts",12 "scripts": {13 "test": "npm-run-all --silent typecheck lint mocha test-types",14 "mocha": "mocha test",15 "test-types": "tsc test/test-types-compilation.ts --noEmit",15 "test-types": "tsc test/test-types-compilation.ts --noEmit --strict",16 "lint": "eslint src test --ext ts --ext js",17 "typecheck": "tsc --noEmit",18 "bundle": "microbundle",19 "build": "npm-run-all --silent clean -p bundle -s docs",
57 unmodified lines
77 "jest/valid-expect": 0,78 "@typescript-eslint/no-explicit-any": 0,79 "@typescript-eslint/explicit-function-return-type": 0,80 "@typescript-eslint/explicit-module-boundary-types": 0,81 "@typescript-eslint/no-empty-function": 081 "@typescript-eslint/no-empty-function": 0,82 "@typescript-eslint/no-non-null-assertion": 083 }84 },85 "eslintIgnore": [86 "dist",
17 unmodified lines
104 "rimraf": "^3.0.2",105 "sinon": "^9.0.2",106 "sinon-chai": "^3.5.0",107 "ts-node": "^8.10.2",107 "typescript": "^3.9.3"108 "typescript": "^3.9.7"109 }109}110}
modified:README.md+13 added-5 removed
78 unmodified lines
79```8081### Typescript8283Set `"strict": true` in your tsconfig.json to get improved type inference for `mitt` instance methods.8485```ts86import mitt from 'mitt';8788type Events = {89 foo: string90 bar?: number91}9285const emitter: mitt.Emitter = mitt();93const emitter: mitt.Emitter<Events> = mitt<Events>();94```9596## Examples & Demos97
35 unmodified lines
133Register an event handler for the given type.134135#### Parameters136129- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to listen for, or `"*"` for all events137- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to listen for, or `'*'` for all events138- `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to given event139140### off141142Remove an event handler for the given type.143144#### Parameters145138- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to unregister `handler` from, or `"*"`146- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to unregister `handler` from, or `'*'`147- `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove148149### emit150151Invoke all handlers for the given type.144If present, `"*"` handlers are invoked after type-matched handlers.152If present, `'*'` handlers are invoked after type-matched handlers.153146Note: Manually firing "\*" handlers is not supported.154Note: Manually firing '\*' handlers is not supported.155156#### Parameters157158- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** The event type to invoke
29 unmodified lines
modified:tsconfig.json+2 added-4 removed