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

Pass generic to HTMLAttributes in Link typings #445

Merged
merged 1 commit into from
May 14, 2023

Conversation

vpzomtrrfrt
Copy link
Contributor

Seems to fix type errors with event listeners

@rschristian
Copy link
Member

Can you show an example of what this fixes?

@vpzomtrrfrt
Copy link
Contributor Author

Here's an example:

function onClickTrackingLink(evt: preact.JSX.TargetedEvent<HTMLAnchorElement>) {
    console.log("Clicked on link with href", evt.currentTarget.href);
}

function TrackingLink(props: preact.JSX.HTMLAttributes) {
    return <Link {...props} onClick={onClickTrackingLink} />;
}

This fails with

Type '(evt: TargetedEvent<HTMLAnchorElement, Event>) => void' is not assignable to type 'EventHandler<TargetedMouseEvent<EventTarget>>'.
  Types of parameters 'evt' and 'event' are incompatible.
    Type 'TargetedMouseEvent<EventTarget>' is not assignable to type 'TargetedEvent<HTMLAnchorElement, Event>'.
      Type 'TargetedMouseEvent<EventTarget>' is not assignable to type '{ readonly currentTarget: HTMLAnchorElement; }'.
        Types of property 'currentTarget' are incompatible.
          Type 'EventTarget' is missing the following properties from type 'HTMLAnchorElement': charset, coords, download, hreflang, and 305 more.

because the type for onClick isn't specific enough. The event handler parameter needs to be this so that currentTarget is correctly typed as HTMLAnchorElement.

@rschristian rschristian merged commit baf4bf9 into preactjs:main May 14, 2023
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants