Skip to content

Incorrect "this" value in event handlers #899

Open
@tmillr

Description

@tmillr

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions