aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Franklin <[email protected]>2020-05-27 18:39:01 +0100
committerGitHub <[email protected]>2020-05-27 13:39:01 -0400
commiteb2be7caf6c88a41a184ef5ea6e4675f0f372771 (patch)
tree1e17bf59398b1cff97c5e2711b484edd8f9f8c8c
parent59cc3d1bc3b85d347baa8883b6206ba88a6071c0 (diff)
Export Mitt types for TS consumers (#101)
* Export Mitt types for TS consumers * Add rudimentary tests for exported TS types * Run tests against the generated output instead of src
-rw-r--r--package.json11
-rw-r--r--src/index.ts12
-rw-r--r--test/index.js20
-rw-r--r--test/types.ts20
-rw-r--r--tsconfig.json11
5 files changed, 63 insertions, 11 deletions
diff --git a/package.json b/package.json
index 08b12d2..36fb941 100644
--- a/package.json
+++ b/package.json
@@ -10,9 +10,10 @@
"umd:main": "dist/mitt.umd.js",
"typings": "dist/index.d.ts",
"scripts": {
- "testonly": "mocha --require esm --require ts-node/register test/**/*.js",
+ "test": "npm-run-all --silent typecheck lint testonly",
+ "testonly": "mocha --require esm test/**/*.js",
"lint": "eslint src test --ext ts --ext js",
- "test": "tsc src/index.ts --noEmit && npm run lint && npm run testonly",
+ "typecheck": "tsc **/*.ts --noEmit",
"bundle": "microbundle",
"build": "npm-run-all --silent clean -p bundle -s docs",
"clean": "rimraf dist",
@@ -23,6 +24,7 @@
"keywords": [
"events",
"eventemitter",
+ "emitter",
"pubsub"
],
"homepage": "https://github.com/developit/mitt",
@@ -47,6 +49,7 @@
"env": {
"browser": true,
"mocha": true,
+ "jest": false,
"es6": true
},
"globals": {
@@ -57,12 +60,16 @@
2,
"always"
],
+ "jest/valid-expect": 0,
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/explicit-function-return-type": 0,
"@typescript-eslint/explicit-module-boundary-types": 0,
"@typescript-eslint/no-empty-function": 0
}
},
+ "eslintIgnore": [
+ "dist"
+ ],
"devDependencies": {
"@types/chai": "^4.2.11",
"@types/mocha": "^7.0.2",
diff --git a/src/index.ts b/src/index.ts
index f8e7e64..cfb1cf3 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,16 +1,16 @@
-type EventType = string | symbol;
+export type EventType = string | symbol;
// An event handler can take an optional event argument
// and should not return a value
-type Handler = (event?: any) => void;
-type WildcardHandler= (type: EventType, event?: any) => void
+export type Handler = (event?: any) => void;
+export type WildcardHandler= (type: EventType, event?: any) => void
// An array of all currently registered event handlers for a type
-type EventHandlerList = Array<Handler>;
-type WildCardEventHandlerList = Array<WildcardHandler>;
+export type EventHandlerList = Array<Handler>;
+export type WildCardEventHandlerList = Array<WildcardHandler>;
// A map of event types and their corresponding event handlers.
-type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>;
+export type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>;
export interface Emitter {
on(type: EventType, handler: Handler): void;
diff --git a/test/index.js b/test/index.js
index dceb4b7..a837d36 100644
--- a/test/index.js
+++ b/test/index.js
@@ -1,11 +1,25 @@
-import mitt from '../src';
+import mitt from '..';
import chai, { expect } from 'chai';
import { spy } from 'sinon';
import sinonChai from 'sinon-chai';
chai.use(sinonChai);
-it('should default export be a function', () => {
- expect(mitt).to.be.a('function');
+describe('mitt', () => {
+ it('should default export be a function', () => {
+ expect(mitt).to.be.a('function');
+ });
+
+ it('should accept an optional event handler map', () => {
+ expect(() => mitt(new Map())).not.to.throw;
+ const map = new Map();
+ const a = spy();
+ const b = spy();
+ map.set('foo', [a, b]);
+ const events = mitt(map);
+ events.emit('foo');
+ expect(a).to.have.been.calledOnce;
+ expect(b).to.have.been.calledOnce;
+ });
});
describe('mitt#', () => {
diff --git a/test/types.ts b/test/types.ts
new file mode 100644
index 0000000..23334bb
--- /dev/null
+++ b/test/types.ts
@@ -0,0 +1,20 @@
+import mitt, { EventHandlerList, EventHandlerMap } from '..';
+
+const events = mitt();
+function foo() {}
+events.on('foo', foo);
+events.emit('foo', 'hello');
+
+// handler return type should be ignored:
+events.on('foo', async e => e * 42);
+
+// event map type
+const map = new Map<string, EventHandlerList>([
+ ['foo', [foo]]
+]);
+const events2 = mitt(map);
+events2.emit('foo', 'hello');
+
+// event map type & iterables
+const map2 : EventHandlerMap = new Map(Object.entries(({ foo: [foo] })));
+mitt(map2);
diff --git a/tsconfig.json b/tsconfig.json
new file mode 100644
index 0000000..2610831
--- /dev/null
+++ b/tsconfig.json
@@ -0,0 +1,11 @@
+{
+ "compileOnSave": false,
+ "compilerOptions": {
+ "noEmit": true,
+ "declaration": true,
+ "moduleResolution": "node"
+ },
+ "exclude": [
+ "test"
+ ]
+}