Skip to content

HTMLElement onchange should use this: this #195

Open

Description

This example worked in 2.0, but not in 2.1:

const input = document.createElement('input');
input.value; // OK
input.onchange = function() {
    this.value; // Error
}

The current definition for onchange in HTMLElement is: onchange: (this: HTMLElement, ev: Event) => any;. Maybe it should use this: this?

Activity

  1. mhegazy commented on Feb 17, 2017

    @mhegazy
    Contributor

    Duplicate of microsoft/TypeScript#12490

    I broke this with my change #166 to reduce instantiations in the library, by making less types generic (types using this are implicitly generic with respect to this).

    The fix here is to generate onChange events for the type hierarchy with the correct type, instead of putting in the parent with type this.

  2. mhegazy commented on Feb 18, 2017

    @mhegazy
    Contributor

    If I'm reading it right, microsoft/TypeScript#14141 may fix this by setting the this type of the function to the type of input.

    correct.

  3. myitcv commented on Mar 15, 2017

    @myitcv
    Contributor

    Given that microsoft/TypeScript#14141 has now been merged, what is left to do to fix this issue and microsoft/TypeScript#12490?

    I note that with with 2.3.0-dev.20170315 and noImplicitThis the following still types incorrectly (per microsoft/TypeScript#12490):

    let i = new Image();
    i.onload; // HTMLElement.onload: (this: HTMLElement, ev: Event) => any
  4. saschanaz commented on May 4, 2017

    @saschanaz
    Contributor

    The fix here is to generate onChange events for the type hierarchy with the correct type, instead of putting in the parent with type this.

    This won't help custom elements, and probably will generate much longer library file. (60+ lines per one HTMLElement interface, 80+ HTMLElements = 4800+ more lines)

    Given that microsoft/TypeScript#14141 has now been merged, what is left to do to fix this issue and microsoft/TypeScript#12490?

    The PR explicitly says that it works only on --noImplicitAny so I think it's not applicable to this issue.

  5. Bashamega commented on Jun 28, 2025

    @Bashamega
    Contributor

    Hello Kagami Sascha Rosylight (@saschanaz)
    I have created a solution for this.
    The solution is to specifically update in the emitter to replace GlobalEventHandlers to this. I have already tested my solution, and it worked.
    I can open a pr for it

  6. saschanaz commented on Jun 28, 2025

    @saschanaz
    Contributor

    Please take a look of #195 (comment) and microsoft/TypeScript#12490, it was done and reverted because of performance regression.

  7. Bashamega commented on Jun 29, 2025

    @Bashamega
    Contributor

    Please take a look of #195 (comment) and microsoft/TypeScript#12490, it was done and reverted because of performance regression.

    Do you have another solution to propose?

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions