-
Notifications
You must be signed in to change notification settings - Fork 1.2k
How should I handle a type for sap/ui/base/Event
?
#4239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hello @sz3lbi, Can you give us more context on the things you have tried? Have you tried to cast explicitly oEvent object to the desired type? Kind regards, |
Okay, let me explain what my concerns and wonders are here. For other components and their events, such as With the old version of UI5, the
The shape of the bCancelBubble: false
bPreventDefault: false
mParameters:
control: c {bAllowTextSelection: true, mEventRegistry: {…}, sId: 'container-com.mycompany.myapp---MyView--myTable', mProperties: t, mAggregations: {…}, …}
state:
Columns: (2) [{…}, {…}]
Sorter: []
[[Prototype]]: Object
[[Prototype]]: Object
oSource: constructor {mEventRegistry: {…}}
sId: "stateChange"
The I hope I have made my concerns and questions clear enough. Thank you! |
Hello @sz3lbi , Thank you for clarifying ! I've created an internal incident DINC0460651. The status will be updated here in GitHub. |
Hi @sz3lbi
Well, that's exactly what we wanted to get rid of with that event generics change. In TypeScript things should be typed and you shouldn't have to do such casts and checks where we can avoid it.
This makes the root cause here clear: most events are declared in the metadata and the attach/detach methods and the generics are generated automatically. But this attach method is coded and documented explicitly: https://github.com/SAP/openui5/blob/master/src/sap.m/src/sap/m/p13n/Engine.js#L246-L249
Basically, this is only required where this change we did does not have any effect. For the vast majority of events, the change is beneficial, as it brings type safety and avoids the checking and casting. But your point is of course valid that there are events which do not get this improvement. For some of the non-metadata-declared events, the JSDoc has been adapted to also get the event parameters typed, e.g. for Does this clarify things? |
Hi @akudev, Everything is clear now. Thanks for the explanation and willingness to improve the remaining events. |
Small correction: sap/base/i18n/Localization shows how it actually should be done: openui5/src/sap.ui.core/src/sap/base/i18n/Localization.js Lines 217 to 260 in d73b87b
The one linked above is different and not that good, and not public, and also other ones in the code don't define an event type and use it for typing the function. |
Hello,
I see that between UI5
1.108
and1.120
a rather big change has been introduced to the type of anEvent
(sap/ui/base/Event
).1.108
:1.120
: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: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:The
attachStateChange
method requires attaching a function that takes anEvent
type which basically meansEvent<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.
The text was updated successfully, but these errors were encountered: