TypeScript-DOM-lib-generator icon indicating copy to clipboard operation
TypeScript-DOM-lib-generator copied to clipboard

Incorrect "this" value in event handlers

Open tmillr opened this issue 5 years ago • 0 comments

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"

tmillr avatar Aug 16 '20 09:08 tmillr