-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathevent.ts
42 lines (34 loc) · 958 Bytes
/
event.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
interface myEvents {
on(name: String, fn: Function): void;
emit(name: String): void;
}
class myEvent implements myEvents {
private list: Array<{ name; fn }> = [];
on(name: String, fn: Function): void {
this.list.push({ name, fn });
console.log(this.list);
}
emit(name: String, ...arg: Array<Object>): void {
this.list.map(data => {
if (name === data.name) {
data.fn(arg);
}
});
}
}
let temps = new myEvent();
temps.on('test', d => {
console.log('test', d);
});
temps.emit('test', { a: 1 }, { b: 2 });
interface API {
'/user': { name: string };
'/menu': { foods: Array<any> };
}
const get = <URL extends keyof API>(url: URL): Promise<API[URL]> => { // keyof 应用
return fetch(url).then(res => res.json());
};
get('/menu').then(data => data.foods);
type DeepReadonly<T> = { readonly [P in keyof T]: DeepReadonly<T[P]> };
const a = { foo: { bar: 22 } };
const b = a as DeepReadonly<typeof a>;