add type definition file for node-observer (#40450)

* add type definition file for node-observer

* remove unnecessary comment in index.d.ts

* fix test errors

* fix test errors
This commit is contained in:
WenJun Chi 2019-11-19 05:48:27 +08:00 committed by Sheetal Nandi
parent 5cc9d512c6
commit f149f60a57
4 changed files with 75 additions and 0 deletions

32
types/node-observer/index.d.ts vendored Normal file
View File

@ -0,0 +1,32 @@
// Type definitions for node-observer 0.4
// Project: https://github.com/hormander/node-observer
// Definitions by: wjchi <https://github.com/CwjXFH>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export = observer;
declare class Observer {
private subscribers: object[];
/**
* subscirpt event
* @param targetObj objects that subscribe to events
* @param eventName the event name
* @param callback callback function executed when an event is fired
*/
subscribe(targetObj: object, eventName: string, callback: any): void;
/**
* unsubscript event
* @param targetObj objects that subscribe to events
* @param eventName the event name
*/
unsubscribe(targetObj: object, eventName: string): void;
/**
* triggering event
* @param targetObj objects that subscribe to events
* @param eventName the event name
* @param data passed to the callback function when the event is fired
*/
send(targetObj: object, eventName: string, data: any): void;
}
declare let observer: Observer;

View File

@ -0,0 +1,16 @@
import observer = require("node-observer");
class Demo {
sayHello(msg: string) {
console.log(`hello ${msg}`);
}
}
const obj = new Demo();
for (let i = 0; i < 3; i++) {
observer.subscribe(obj, "hello", obj.sayHello);
}
observer.unsubscribe(obj, "hello");
observer.send(obj, "hello", "sent event");

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",
"node-observer-tests.ts"
]
}

View File

@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}