-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Request to change currentTarget in Event interface for lib.d.ts #299
Request to change currentTarget in Event interface for lib.d.ts #299
Comments
XMLHttpRequest object also can be currentTarget but it is not Element/HTMLElement. Maybe that's the reason behind this. Would generic type improve this? interface Event<T extends EventTarget> {
/* ... */
currentTarget: T;
/* ... */
}
interface EventListener<T extends EventTarget> {
(evt: Event<T>): void;
}
interface HTMLElement {
/* ... */
addEventListener(type: string, listener: EventListener<HTMLElement>, useCapture?: boolean): void;
} |
Looks like it'll go a long way to improving it. Sent from my iPhone
|
Note that not every event target is an The problem is that we currently generate lib.d.ts, using a script, based on a file which we can't make public at this time, and that file is being deprecated in favor of a new one. Because of that, we're not going to make improvements in the script at this time, but hopefully in the future we can take PRs on the lib.d.ts generation script/input. Tagging 'Revisit' for now - please ping us on this in a month and I can see where we're at. |
Is there any syntax to refer the current type? I think this can be solved more easily if we can do this kind of work: interface HTMLElement {
addEventListener(type: string, listener: EventListener<this>, useCapture?: boolean): void;
}
var image: HTMLImageElement;
var video: HTMLVideoElement;
image.addEventListener // receives EventListener<HTMLImageElement> type
video.addEventListener // receives EventListener<HTMLVideoElement> type |
Hi all, pinging this thread. I ran into this issue with Edit: this worked to quiet the warnings: |
@rayshan the suggestion was for what should go into the |
Now that #4910 is merged, can this be revisited? |
@zhengbli might have some context on the status of lib.d.ts issues that require script based generation |
PRs welcomed. here is some infromation on contributing lib.d.ts changes: https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes |
I ran into this My work-around, to suppress the horrid red squiggily lines;-) is the following:
Then in my app:
The generated JS codes looks good and works for me. Could this kind of solution, for unusual JS event types, be added to lib.d.ts? I'm guessing my solution is overly simplistic. But it would help to know why. |
I would love to fix this issue. But as I see the only reference to currentTarget is inside generated webworker.generated.d.ts files in TSJS generator. I have no idea how to fix this 😞 |
Well. I tried to start solve this issue. I create a PR microsoft/TypeScript-DOM-lib-generator#202 |
I'm getting |
Hope some progress on this issue. It is hard to manipulate dom with Typescript. |
This issue is four years old and the corresponding PR nearly one year I doubt this will be fixed any time soon. |
There is a fix available in microsoft/TypeScript-DOM-lib-generator#207 |
Casting party tonite at 11 - unload your browser tabs: 👯♂️ |
What about adding readonly currentTarget: Element | null;
readonly target: Element | null; on |
same issue here. I was trying to use Vue with Typescript and wanted to get the value of an input field as the user typed. But event.target.value would not pass the type checker even though - i think - a text inout must always produce such fields for such an event |
We got rid of the error by adding type any to our code: this.url = (<any>event).target.result; |
What about typing Event and EventTarget? interface Event<C = any, S = any, T = any> {
...
currentTarget: EventTarget<C> | null;
srcElement: EventTarget<S> | null;
target: EventTarget<T> | null;
...
}
interface EventTarget<T = any> {
...
} |
I encountered this issue while using the FileReader, a simple cast to the correct type fixes it const fileReader = new FileReader();
fileReader.onload = $ev => {
console.log($ev); // type ProgressEvent
console.log($ev.target); // type FileReader
// console.log($ev.target.result); // editor and autocomplete doesn't show any error
console.log(($ev.target as FileReader).result); // casting compiles fine
};
fileReader.readAsText(file); |
Are there any updates about this topic? It would be useful to have it without force casting |
Indeed, would be nice with some kind of official response to this issue before its 6th birthday |
Yep, this is probably the Number 1 day-long nuisance when working with DOM, JSX, etc. In fact, it's the only daily nuisance I have - and I imagine a million people have this, all day long. It's really difficult to understand why this doesn't get any priority. |
Same for event.target. A very common scenario is adding a click handler to a parent element when children don't exist yet, and when there will be many children. A simple test for event.target.tagName or even classList is a common thing I do. It works....have used it for many years. I don't get intellisense for tagName/classList since EventTarget is not classified as HTMLElement, but it works. |
Going to bump this thread since the last post was in 2020. Are the performance issues with generics still there? Is there a concern about introducing performance issues to users with old versions of TS if modern TS can handle things well? Is there anything the community can do? Would a PR be welcome? |
The PRs are there, but blocked by performance issues. I would not expect this issue to be solved any time soon. I guess everybody got used to this problem. |
What I can see is that there is interest in fixing this issue, and there was a PR. The only reason the PR was not merged is because its performance was bad (because of generics). Then the PR was closed, because it wasn't updated anymore. Now, people are asking for this feature, but there is no new PR with new performance numbers. I don't feel comfortable to implement this feature since I never worked on tsc, though is there anyone else who is able to do so? From the old PR, it doesn't look like it would be a lot of work. |
For anyone still interested in this feature, I created a PR here and could use some feedback regarding the performance. |
When using the Event interface from lib.d.ts, and attaching a listener, the callback will get an object of type Event. However, the Event's currentTarget property is of type EventTarget (whereas it's should be of type Element/HTMLElement).
The text was updated successfully, but these errors were encountered: