Skip to content

Commit

Permalink
TypedEventEmitter: update emit fuction signature
Browse files Browse the repository at this point in the history
Summary:
Somehow I cannot typecheck progress data with the union type:

    const onProgress = (progress: OperationProgress) => {
      // progressEmitter is TypedEventEmitter<'progress', OperationProgress>
      this.progressEmitter.emit('progress', progress);
                                ^^^^^^^^^^^^^^^^^^^^
    };

Error:

    Argument of type '["progress", OperationProgress]' is not assignable to parameter of type '[event: "error", data: Error] | [event: "progress", data: { id: string; kind: "queue"; queue: string[]; }] | [event: "progress", data: { id: string; kind: "spawn"; queue: string[]; }] | [event: "progress", data: { id: string; kind: "stderr"; message: string; }] | ... 5 more ... | [event: ...]'.
    Type '["progress", OperationProgress]' is not assignable to type '[event: "progress", data: { id: string; kind: "queue"; queue: string[]; }] | [event: "progress", data: { id: string; kind: "spawn"; queue: string[]; }] | [event: "progress", data: { id: string; kind: "stderr"; message: string; }] | [event: "progress", data: { ...; }] | ... 4 more ... | [event: ...]'.
    Type '["progress", OperationProgress]' is not assignable to type '[event: "progress", data: { id: string; kind: "forgot"; }]'.
      Type at position 1 in source is not compatible with type at position 1 in target.
        Type 'OperationProgress' is not assignable to type '{ id: string; kind: "forgot"; }'.
          Type '{ id: string; kind: "queue"; queue: string[]; }' is not assignable to type '{ id: string; kind: "forgot"; }'.
            Types of property 'kind' are incompatible.
              Type '"queue"' is not assignable to type '"forgot"'.ts(2345)

This diff changes the signature to make it easier for tsc to understand.

Reviewed By: evangrayk

Differential Revision: D54364589

fbshipit-source-id: 841dd82449d1b21a2287b3a36b755b081094280d
  • Loading branch information
quark-zju authored and facebook-github-bot committed Mar 1, 2024
1 parent 06758c8 commit f69a6ef
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions addons/shared/TypedEventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,11 @@ export class TypedEventEmitter<EventName extends string, EventType> {
return this;
}

emit(
...args:
| [event: 'error', data: Error]
| (EventType extends undefined
? [event: EventName] | [event: EventName, data: EventType]
: [event: EventName, data: EventType])
): boolean {
const [name, data] = args;
emit(event: EventName): EventType extends undefined ? boolean : never;
emit(event: EventName, data: EventType): boolean;
emit(event: 'error', data: Error): boolean;

emit(name: EventName | 'error', data?: EventType | Error): boolean {
const event = new EventWithPayload(name, data);
if (!this.target.dispatchEvent(event)) {
return false;
Expand Down

0 comments on commit f69a6ef

Please sign in to comment.