Skip to content

Commit cd4a992

Browse files
committed
Merge branch 'josh-typescript-current-target'
2 parents ca964a4 + b216843 commit cd4a992

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

index.d.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export function on<K extends keyof GlobalEventHandlersEventMap>(name: K, selector: string, listener: (this: GlobalEventHandlers, ev: GlobalEventHandlersEventMap[K]) => any, options?: EventListenerOptions): void;
2-
export function on(name: string, selector: string, listener: EventListener, options?: EventListenerOptions): void;
3-
export function off(name: string, selector: string, listener: EventListener, options?: EventListenerOptions): void;
4-
export function fire(target: EventTarget, name: string, detail?: any): boolean;
1+
type DelegatedEventListener = (this: Element, ev: CustomEvent & {currentTarget: Element}) => any
2+
3+
export function on<K extends keyof GlobalEventHandlersEventMap>(name: K, selector: string, listener: (this: GlobalEventHandlers & Element, ev: GlobalEventHandlersEventMap[K] & {currentTarget: Element}) => any, options?: EventListenerOptions): void;
4+
export function on(name: string, selector: string, listener: DelegatedEventListener, options?: EventListenerOptions): void;
5+
export function off(name: string, selector: string, listener: DelegatedEventListener, options?: EventListenerOptions): void;
6+
export function fire(target: Element, name: string, detail?: any): boolean;

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
"scripts": {
1111
"clean": "rm -rf build dist",
1212
"flow": "flow check",
13+
"ts": "tsc --noEmit test/test-ts.ts",
1314
"lint": "eslint delegated-events.js test",
1415
"bootstrap": "git submodule update --init && npm install",
15-
"prebuild": "npm run clean && npm run flow && npm run lint",
16+
"prebuild": "npm run clean && npm run flow && npm run ts && npm run lint",
1617
"build": "rollup -c && cp delegated-events.js.flow dist/index.esm.js.flow && cp delegated-events.js.flow dist/index.umd.js.flow",
17-
"pretest": "npm run clean && npm run flow && npm run lint && rollup -c rollup.config.test.js",
18+
"pretest": "npm run clean && npm run flow && npm run ts && npm run lint && rollup -c rollup.config.test.js",
1819
"test": "karma start --single-run --browsers ChromeHeadless karma.conf.js",
1920
"prebrowser": "npm run pretest",
2021
"browser": "open \"file://$(pwd)/test/test.html\"",
@@ -42,7 +43,8 @@
4243
"prettier": "^1.18.2",
4344
"rollup": "^1.20.3",
4445
"rollup-plugin-babel": "^4.3.3",
45-
"rollup-plugin-node-resolve": "^5.2.0"
46+
"rollup-plugin-node-resolve": "^5.2.0",
47+
"typescript": "^3.6.3"
4648
},
4749
"files": [
4850
"dist",

test/test-ts.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {on} from '../index';
2+
3+
on('click', '.hubot', function(event) {
4+
const e: MouseEvent = event;
5+
event.button;
6+
7+
const self: Element = this;
8+
this.matches;
9+
10+
const t: Element = event.currentTarget;
11+
event.currentTarget.matches;
12+
});
13+
14+
on('custom:event', '.hubot', function(event) {
15+
const e: CustomEvent = event;
16+
event.detail;
17+
18+
const self: Element = this;
19+
this.matches;
20+
21+
const t: Element = event.currentTarget;
22+
event.currentTarget.matches;
23+
});
24+
25+
document.addEventListener('click', handleEvent);
26+
on('click', '.hubot', handleEvent);
27+
28+
function handleEvent(event: MouseEvent) {
29+
event.button;
30+
event.target;
31+
}

0 commit comments

Comments
 (0)