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

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

Open
LifeJustDLC opened this issue May 10, 2024 · 5 comments

Comments

@LifeJustDLC
Copy link

LifeJustDLC commented May 10, 2024

πŸ”Ž 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

@fatcerberus
Copy link

for the record: this is not a crash

@LifeJustDLC
Copy link
Author

@fatcerberus sorry, I didn't have the time to bisect the versions.

@fatcerberus
Copy link

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

@jcalz
Copy link
Contributor

jcalz commented May 11, 2024

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)

@LifeJustDLC
Copy link
Author

LifeJustDLC commented May 12, 2024

@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 @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.

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

No branches or pull requests

3 participants