Skip to content
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

feat(framework): add .currentTarget to the type of event handler in TSX and UI5CustomEvent #10957

Merged
merged 3 commits into from
Mar 4, 2025

Conversation

pskelin
Copy link
Contributor

@pskelin pskelin commented Feb 26, 2025

The generated event handlers for events described in eventDetails now have the correct type for the e.currentTarget parameter

<Input onClick={(e => e.currentTarget)} />
                     // ^^^^^^^^^^^^^
                     // `ui5-input` element now, was `EventTarget` before

Additionally, the UI5CustomEvent helper also adds the correct type to the event parameter e.currentTarget

An event handler no longer has to use type assertions

Before:

onInput(e: CustomEvent) {
  const value: string = (e.target as Input).value;

This works, but the same event handler is also attached to a ui5-slider and the e.target.value is a number in that case which goes unnoticed because of the as Input type assertion.

After

onInput(e: UI5CustomEvent<Input, "input">) {
  const value: string = e.currentTarget.value;
//                        ^^^^^^^^^^^^^^^^^^^
//                        this is checked now, adding the same handler to a slider will result in an error

Implementation note

Event handlers are contravariant (an event handler that takes a subclass as a parameter cannot be assigned to a property in the base class that takes the base class as a parameter).

To circumvent this, the generated event handlers are using the method notation so that typescript handles them as bivariant

More info in the strictFunctionTypes compiler option here:
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-6.html#strict-function-types

@pskelin pskelin merged commit f56753c into main Mar 4, 2025
12 checks passed
@pskelin pskelin deleted the feat-current-target branch March 4, 2025 07:48
@ui5-webcomponents-bot
Copy link
Collaborator

🎉 This PR is included in version v2.8.0 🎉

The release is available on v2.8.0

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants