You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
interfaceTodoEventEmitter{emit(name: 'create',payload: string): boolean;emit(name: 'updateText',payload: {id: string;text: string;}): boolean;emit(name: string,payload: any): boolean;on(name: 'craete',listener: (payload: string)=>any): this;// typo 'craete', but no erroron(name: 'updateText',listener: (payload: {id: number;text: string;})=>any): this;// id type incorrect, but no erroron(name: string,listener: (payload: any)=>any): this;// similar definitions for other methods again and again and again}consteventEmitter: TodoEventEmitter=newEventEmitter();
Overload definitions would be further reusable when used in combination with generic types.
Well, this is a suggestion.
It's common that multiple methods should share the definition of the specialized signatures, such as the methods of EventEmitter as I described above, as well as addEventListener/removeEventListener of HTMLElement, and createElement/getElementsByTagName of Document.
Unfortunately however, currently we have to repeat the same definition for each method in such situations (AFAIK).
I think it'd be really nice if the functionality of string literal types could be extended to resolve this.
In your example, the event "create" is not linked to the specific type of payload.
So one can emit the "create" event with a payload of either string or { id: string; text: string }, which is not safe.
Inspired by #1003 and #6028.
Isn't it possible to use string literal types for sharing specialized signatures among several methods?
i.e., I would like to write:
instead of:
Overload definitions would be further reusable when used in combination with generic types.
The text was updated successfully, but these errors were encountered: