Skip to content

How should I handle a type for sap/ui/base/Event? #4239

@sz3lbi

Description

@sz3lbi

Hello,
I see that between UI5 1.108 and 1.120 a rather big change has been introduced to the type of an Event (sap/ui/base/Event).

1.108:

export default class Event extends BaseObject implements Poolable

1.120:

export default class Event<
  ParamsType extends Record<string, any> = object,
  SourceType extends EventProvider = EventProvider
>
extends BaseObject
implements Poolable

Before, I could easily bind my function to an event and the types would match. Afterwards, in the handler, when I wanted to get a parameter, I would cast it to unknown and check the shape of and object before accessing properties like:

const vArguments = oEvent.getParameter("arguments") as unknown;
if (
  !vArguments ||
  typeof vArguments !== "object" ||
  !("parentPath" in vArguments) ||
  typeof vArguments.parentPath !== "string"
) {
  // Some error handling and logging.
  return;
}

Now, after the change which has been introduced, I am able to define the shape of an Event object in the function definition. However, then I face some problems when trying to attach this handler to an event:

private registerP13n(): void {
  // Some logic.
  // Engine.attachStateChange(fnStateEventHandler: (p1: Event) => void): Engine
  Engine.getInstance().attachStateChange((oEvent) => {
    this.stateChange(oEvent); // Argument of type 'Event<object, EventProvider>' is not assignable to parameter of type 'Event<{ control: Table; state: Record<string, SelectionController>; }, EventProvider>'.
  });
}

private stateChange(
    oEvent: Event<{
      control: Table;
      state: Record<string, SelectionController>;
    }>
  ): void {
    // Whatever.
  }

The attachStateChange method requires attaching a function that takes an Event type which basically means Event<object, EventProvider> and my type is not compatible there.
I could of course use a type like Event<any> in the handler and then do what I did before, but that would defeat the purpose of the change introduced.

How should I handle this? What am I not seeing here?
Thanks.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions