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

HTMLElement onchange should use `this: this`

Open ghost opened this issue 8 years ago • 8 comments

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?

ghost avatar Feb 15 '17 21:02 ghost

Duplicate of https://github.com/Microsoft/TypeScript/issues/12490

I broke this with my change https://github.com/Microsoft/TSJS-lib-generator/pull/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.

mhegazy avatar Feb 17 '17 18:02 mhegazy

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.

ghost avatar Feb 17 '17 18:02 ghost

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.

mhegazy avatar Feb 18 '17 01:02 mhegazy

Given that https://github.com/Microsoft/TypeScript/pull/14141 has now been merged, what is left to do to fix this issue and https://github.com/Microsoft/TypeScript/issues/12490?

I note that with with 2.3.0-dev.20170315 and noImplicitThis the following still types incorrectly (per https://github.com/Microsoft/TypeScript/issues/12490):

let i = new Image();
i.onload; // HTMLElement.onload: (this: HTMLElement, ev: Event) => any

myitcv avatar Mar 15 '17 12:03 myitcv

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.

saschanaz avatar May 04 '17 03:05 saschanaz

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

Bashamega avatar Jun 28 '25 10:06 Bashamega

Please take a look of https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/195#issuecomment-280722380 and https://github.com/microsoft/TypeScript/issues/12490, it was done and reverted because of performance regression.

saschanaz avatar Jun 28 '25 12:06 saschanaz

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?

Bashamega avatar Jun 29 '25 04:06 Bashamega