Does TypeScript support events on classes?

后端 未结 8 733
小鲜肉
小鲜肉 2021-01-30 01:53

I am just wondering if in TypeScript you can define custom events on your classes or interfaces?

What would this look like?

8条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-30 02:33

    If you are looking to get intelli-sense type checking using the standard emitter pattern you can now do the following:

    type DataEventType = "data";
    type ErrorEventType = "error";
    declare interface IDataStore extends Emitter {
        on(name: DataEventType, handler : (data: TResponse) => void);   
        on(name: ErrorEventType, handler: (error: any) => void);    
    }
    

提交回复
热议问题