Added type definition for delegated-events (#35006)

This commit is contained in:
Christopher Parsons
2019-04-26 15:16:53 -07:00
committed by Pranav Senthilnathan
parent c9e7cb88b0
commit 90d4ba0a85
4 changed files with 81 additions and 0 deletions
@@ -0,0 +1,29 @@
// Tests are re-used based on `delegated-events`'s flow tests.
// https://github.com/dgraham/delegated-events/blob/master/test/test-flow.js
import { on, off, fire } from "delegated-events";
function onButtonClick(event: Event) {
event.target;
}
on("click"); // $ExpectError
on("click", ".js-button"); // $ExpectError
on("click", ".js-button", onButtonClick); // $ExpectType void
off("click"); // $ExpectError
off("click", ".js-button"); // $ExpectError
off("click", ".js-button", onButtonClick); // $ExpectType void
// $ExpectType void
on("robot:singularity", ".js-robot-image", (event: Event) => {
event.target;
});
const image = document.querySelector(".js-button");
if (image) {
fire(image); // $ExpectError
fire(image, "robot:singularity"); // $ExpectType boolean
fire(image, "robot:singularity", { name: "Hubot" }); // $ExpectType boolean
}
+27
View File
@@ -0,0 +1,27 @@
// Type definitions for delegated-events 1.0
// Project: https://github.com/dgraham/delegated-events#readme
// Definitions by: Christopher Parsons <https://github.com/cwparsons>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Definitions based on the projects' flow definitions.
// https://github.com/dgraham/delegated-events/blob/master/delegated-events.js.flow
export interface EventListenerOptions {
capture?: boolean;
}
export function on(
name: string,
selector: string,
handler: EventListenerOrEventListenerObject,
options?: EventListenerOptions
): void;
export function off(
name: string,
selector: string,
handler: EventListenerOrEventListenerObject,
options?: EventListenerOptions
): void;
export function fire(target: EventTarget, name: string, detail?: any): boolean;
+24
View File
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"delegated-events-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }