@@ -251,6 +251,7 @@ emitter.on('click', function (message) {
251251
252252- ` TypedFastEventListener `
253253
254+ 根据` FastEvent ` 泛型参数自动推导出来的监听器类型。
254255
255256``` ts twoslash
256257import { FastEvent ,FastEventMessage } from ' fastevent' ;
@@ -264,17 +265,74 @@ type CustomEvents = {
264265const emitter = new FastEvent <CustomEvents >();
265266
266267type ClickListener = typeof emitter .types .listeners [' click' ]
268+ const listener: ClickListener = (message ) => {
269+ message .type
270+ message .payload
271+ }
267272
268273```
269274
270275- ` TypedFastEventAnyListener `
276+
277+ 根据` FastEvent ` 泛型参数自动推导出来的监听器类型。
278+
279+ ``` ts twoslash
280+ import { FastEvent ,FastEventMessage } from ' fastevent' ;
281+
282+ type CustomEvents = {
283+ click: { x: number ; y: number };
284+ mousemove: boolean ;
285+ scroll: number ;
286+ focus: string ;
287+ };
288+ const emitter = new FastEvent <CustomEvents >();
289+
290+ type anyListener = typeof emitter .types .anyListener
291+
292+ const listener: anyListener = (message ) => {
293+ if (message .type == ' click' ){
294+ message .payload
295+ }else if (message .type == ' mousemove' ){
296+ message .payload
297+ }else if (message .type == ' scroll' ){
298+ message .payload
299+ }
300+ }
301+
302+
303+ ```
304+
305+
271306- ` FastEventListener `
272307
308+ 通用的监听器类型。
273309
274- ## 检索类型
310+ ``` ts twoslash
311+ import { FastEvent ,FastEventMessage ,FastEventListener } from ' fastevent' ;
275312
276- ` FastEvent ` 和` FastEventScope ` 都提供了` types ` 对象用于检索事件类型。
313+ type CustomEvents = {
314+ click: { x: number ; y: number };
315+ mousemove: boolean ;
316+ scroll: number ;
317+ focus: string ;
318+ };
319+ const emitter = new FastEvent <CustomEvents >();
320+
321+ type anyListener = typeof emitter .types .anyListener
322+
323+ const listener: FastEventListener <number > = (message ) => {
324+ message .type
325+ message .payload
326+ }
327+
328+
329+ ```
330+
331+
332+ ## 检索类型
277333
334+ ` FastEvent ` 和` FastEventScope ` 支持泛型参数推导,为提供了` types ` 对象用于检索以下类型。
335+
278336``` ts twoslash
279337import { FastEvent } from ' fastevent' ;
280338
@@ -302,6 +360,9 @@ type EventType = typeof emitter.types.events;
302360type MetaType = typeof emitter .types .meta ;
303361type ContextType = typeof emitter .types .context ;
304362type MessageType = typeof emitter .types .message ;
363+ type ListenersType = typeof emitter .types .listeners ;
364+ type anylistenerType = typeof emitter .types .anyListener ;
365+
305366
306367```
307368
@@ -312,3 +373,113 @@ type MessageType = typeof emitter.types.message;
312373:::
313374
314375
376+ ## 全局扩展
377+
378+ ` FastEvent ` 和` FastEventScope ` 支持全局扩展,用于扩展` FastEvent ` 和` FastEventScope ` 的类型。
379+
380+ ### FastEvents
381+
382+ 扩展全局事件类型。
383+
384+ ``` ts twoslash
385+ import { FastEvent } from ' fastevent' ;
386+
387+ declare module " fastevent" {
388+ interface FastEvents {
389+ click: { x: number ; y: number };
390+ mousemove: boolean ;
391+ scroll: number ;
392+ focus: string ;
393+ }
394+ }
395+ const emitter = new FastEvent ();
396+
397+ type EventTypes = typeof emitter .types .events
398+
399+ ```
400+
401+
402+ ### FastEventMeta
403+
404+ 扩展全局元数据类型。
405+
406+ ``` ts twoslash
407+ import { FastEvent } from ' fastevent' ;
408+
409+ declare module " fastevent" {
410+ interface FastEventMeta {
411+ x: number ;
412+ y: number ;
413+ z? : number ;
414+ }
415+ }
416+
417+ const emitter = new FastEvent ();
418+
419+ type MetaType = typeof emitter .types .meta
420+
421+ emitter .onAny ((message ) => {
422+ message .meta .x // number
423+ message .meta .y // boolean
424+ message .meta .z // string
425+ })
426+ ```
427+
428+
429+ ### FastEventScopeMeta
430+
431+ 扩展全局事件作用域元数据类型。
432+
433+ ``` ts twoslash
434+ import { FastEvent } from ' fastevent' ;
435+
436+ declare module " fastevent" {
437+ interface FastEventMeta {
438+ root: number ;
439+ }
440+ interface FastEventScopeMeta {
441+ x: number ;
442+ y: boolean ;
443+ z? : string ;
444+ }
445+ }
446+
447+ const emitter = new FastEvent ();
448+
449+ const scope = emitter .scope (" a/b/c" );
450+
451+ type MetaType = typeof scope .types .meta
452+
453+ scope .onAny ((message ) => {
454+ message .meta .x // number
455+ message .meta .y // boolean
456+ message .meta .z // string
457+ message .meta .scope // string
458+ message .meta .root // number
459+ })
460+
461+ ```
462+
463+ ### FastEventMessageExtends
464+
465+ 扩展全局消息类型。
466+
467+ ``` ts twoslash
468+ import { FastEvent ,FastEventMessage } from ' fastevent' ;
469+
470+ declare module " fastevent" {
471+ interface FastEventMessageExtends {
472+ timestamp: number ;
473+ }
474+ }
475+
476+ const message: FastEventMessage = {
477+ type: " click" ,
478+ payload: {
479+ x: 1 ,
480+ y: 2
481+ },
482+ timestamp: 1629999999999
483+ }
484+
485+ ```
0 commit comments