TypeScript: specialized signatures for EventEmitter subclass events

我的梦境 提交于 2019-12-04 03:28:15

what is the intended way to specify the events my class can emit?

Instead of using a single event stream that contains all the types its easier to have a separate event stream for each type.

This is a concept I call TypedEvent. One example project that uses it is http://alm.tools/

Implementation : https://github.com/alm-tools/alm/blob/55a8eb0f8ee411a506572abce92085235658b980/src/common/events.ts#L20-L72

Here is an example usage : https://github.com/alm-tools/alm/blob/55a8eb0f8ee411a506572abce92085235658b980/src/server/lang/errorsCache.ts#L10

export let errorsUpdated = new TypedEvent<ErrorsUpdate>();
// emit: 
errorsUpdated.emit({} /* this will be type checked */);
// consume:
errorsUpdated.on((x)=>null); // x has the correct inferred type
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!