Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eb2be7c

Browse files
authored
Export Mitt types for TS consumers (developit#101)
* Export Mitt types for TS consumers * Add rudimentary tests for exported TS types * Run tests against the generated output instead of src
1 parent 59cc3d1 commit eb2be7c

File tree

5 files changed

+63
-11
lines changed

5 files changed

+63
-11
lines changed

‎package.json‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
"umd:main": "dist/mitt.umd.js",
1111
"typings": "dist/index.d.ts",
1212
"scripts": {
13-
"testonly": "mocha --require esm --require ts-node/register test/**/*.js",
13+
"test": "npm-run-all --silent typecheck lint testonly",
14+
"testonly": "mocha --require esm test/**/*.js",
1415
"lint": "eslint src test --ext ts --ext js",
15-
"test": "tsc src/index.ts --noEmit && npm run lint && npm run testonly",
16+
"typecheck": "tsc **/*.ts --noEmit",
1617
"bundle": "microbundle",
1718
"build": "npm-run-all --silent clean -p bundle -s docs",
1819
"clean": "rimraf dist",
@@ -23,6 +24,7 @@
2324
"keywords": [
2425
"events",
2526
"eventemitter",
27+
"emitter",
2628
"pubsub"
2729
],
2830
"homepage": "https://github.com/developit/mitt",
@@ -47,6 +49,7 @@
4749
"env": {
4850
"browser": true,
4951
"mocha": true,
52+
"jest": false,
5053
"es6": true
5154
},
5255
"globals": {
@@ -57,12 +60,16 @@
5760
2,
5861
"always"
5962
],
63+
"jest/valid-expect": 0,
6064
"@typescript-eslint/no-explicit-any": 0,
6165
"@typescript-eslint/explicit-function-return-type": 0,
6266
"@typescript-eslint/explicit-module-boundary-types": 0,
6367
"@typescript-eslint/no-empty-function": 0
6468
}
6569
},
70+
"eslintIgnore": [
71+
"dist"
72+
],
6673
"devDependencies": {
6774
"@types/chai": "^4.2.11",
6875
"@types/mocha": "^7.0.2",

‎src/index.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
type EventType = string | symbol;
1+
export type EventType = string | symbol;
22

33
// An event handler can take an optional event argument
44
// and should not return a value
5-
type Handler = (event?: any) => void;
6-
type WildcardHandler= (type: EventType, event?: any) => void
5+
export type Handler = (event?: any) => void;
6+
export type WildcardHandler= (type: EventType, event?: any) => void
77

88
// An array of all currently registered event handlers for a type
9-
type EventHandlerList = Array<Handler>;
10-
type WildCardEventHandlerList = Array<WildcardHandler>;
9+
export type EventHandlerList = Array<Handler>;
10+
export type WildCardEventHandlerList = Array<WildcardHandler>;
1111

1212
// A map of event types and their corresponding event handlers.
13-
type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>;
13+
export type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>;
1414

1515
export interface Emitter {
1616
on(type: EventType, handler: Handler): void;

‎test/index.js‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
import mitt from '../src';
1+
import mitt from '..';
22
import chai, { expect } from 'chai';
33
import { spy } from 'sinon';
44
import sinonChai from 'sinon-chai';
55
chai.use(sinonChai);
66

7-
it('should default export be a function', () => {
8-
expect(mitt).to.be.a('function');
7+
describe('mitt', () => {
8+
it('should default export be a function', () => {
9+
expect(mitt).to.be.a('function');
10+
});
11+
12+
it('should accept an optional event handler map', () => {
13+
expect(() => mitt(new Map())).not.to.throw;
14+
const map = new Map();
15+
const a = spy();
16+
const b = spy();
17+
map.set('foo', [a, b]);
18+
const events = mitt(map);
19+
events.emit('foo');
20+
expect(a).to.have.been.calledOnce;
21+
expect(b).to.have.been.calledOnce;
22+
});
923
});
1024

1125
describe('mitt#', () => {

‎test/types.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import mitt, { EventHandlerList, EventHandlerMap } from '..';
2+
3+
const events = mitt();
4+
function foo() {}
5+
events.on('foo', foo);
6+
events.emit('foo', 'hello');
7+
8+
// handler return type should be ignored:
9+
events.on('foo', async e => e * 42);
10+
11+
// event map type
12+
const map = new Map<string, EventHandlerList>([
13+
['foo', [foo]]
14+
]);
15+
const events2 = mitt(map);
16+
events2.emit('foo', 'hello');
17+
18+
// event map type & iterables
19+
const map2 : EventHandlerMap = new Map(Object.entries(({ foo: [foo] })));
20+
mitt(map2);

‎tsconfig.json‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compileOnSave": false,
3+
"compilerOptions": {
4+
"noEmit": true,
5+
"declaration": true,
6+
"moduleResolution": "node"
7+
},
8+
"exclude": [
9+
"test"
10+
]
11+
}

0 commit comments

Comments
 (0)