Skip to content

Incorrect "this" value in event handlers #899

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

Open
tmillr opened this issue Aug 16, 2020 · 0 comments
Open

Incorrect "this" value in event handlers #899

tmillr opened this issue Aug 16, 2020 · 0 comments

Comments

@tmillr
Copy link

tmillr commented Aug 16, 2020

In:

let el = document.createElement('p')
el.ontransitionend = function(){let r = this}

The this value of r is actually el or typeof el, but typescript tells me that r is "GlobalEventHandlers". This is very annoying because even if I try to replace it with:

el.ontransitionend = function(this: HTMLParagraphElement, ev: TransitionEvent){let r = this}

TypeScript still incorrectly throws an error saying that the "this" types are incompatible...

If TS cannot correctly assume/infer the this type, it should at least default to the "any" type or something so that I can specify my own this type without getting errors. I'm not sure if this happens with all events or just those that are "GlobalEventHandlers" related, but I did notice that in the following variations of my example, the this types are more or less correctly assumed/inferred:

ontransitionend = function(){let r = this} // r's type is "Window"
window.ontransitionend = function(){let r = this} // r's type is "Window & typeof globalThis"
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

1 participant