Skip to content

Event type (name string) doesn't get properly checked or suggestions when using addEventListener() on an Union of multiple HTMLElement typesΒ #58497

Description

@LifeJustDLC

πŸ”Ž Search Terms

"event type", "addEventListener", "suggestion"

πŸ•— Version & Regression Information

  • This is a crash

⏯ Playground Link

No response

πŸ’» Code

type DivOrSpan = HTMLDivElement | HTMLSpanElement
const divOrSpan = document.querySelector<DivOrSpan>(".could-be-div-or-span")!
divOrSpan.addEventListener("typo", () => {}) // neither complain or suggest when first typing

const div = document.querySelector<HTMLDivElement>(".div")!
div.addEventListener("click", () => {}) // doesn't complain either but get suggestions automatically as well as pressing ctrl+i

πŸ™ Actual behavior

first one neither complain or suggest when first typing.

πŸ™‚ Expected behavior

get suggestions automatically as well as pressing ctrl+i like the second one.

Additional information about the issue

No response

Activity

  1. fatcerberus commented on May 10, 2024

    @fatcerberus

    for the record: this is not a crash

  2. LifeJustDLC commented on May 11, 2024

    @LifeJustDLC
    Author

    Bruce Pascoe (@fatcerberus) sorry, I didn't have the time to bisect the versions.

  3. fatcerberus commented on May 11, 2024

    @fatcerberus

    No, I just meant that you put

    This is a crash

    but it’s not - the compiler didn’t crash. It’s just not doing what you expect

  4. jcalz commented on May 11, 2024

    @jcalz
    Contributor

    I suspect this is just a caveat of #29011, you're calling a union of overloaded and generic methods. Feels like a design limitation to me (just a nosy bystander and not a TS team member)

  5. LifeJustDLC commented on May 12, 2024

    @LifeJustDLC
    Author

    Joe Calzaretta (@jcalz) Thanks for your insights.

    The caveats are that only one type in the list of union members is allowed to have overloads, and only one type within the list of union members is allowed to have type parameters.

    I didn't fully understand that post, but I think it's a little different:

    图片

    图片

    As Wesley Wigham (@weswigham) described, and as I understand, it will at least get an intersected signature (suggestion) between A and B? which is "2" in the above case. but in the similar case of HTMLDivElement and HTMLSpanElement, this seems doesn't work.

  6. RyanCavanaugh commented on Jun 17, 2024

    @RyanCavanaugh
    Member

    These signatures don't merge because the this value is different in the callbacks

  7. typescript-bot commented on Jun 20, 2024

    @typescript-bot
    Contributor

    This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions