Skip to content

Request to change currentTarget in Event interface for lib.d.ts #299

Description

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).

Activity

  1. saschanaz commented on Aug 3, 2014

    @saschanaz
    Contributor

    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;
    }
  2. nathggns commented on Aug 3, 2014

    @nathggns
    Author

    Looks like it'll go a long way to improving it.

    Sent from my iPhone

    On 3 Aug 2014, at 13:34, SaschaNaz [email protected] wrote:

    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 {
    /* ... /
    currentTarget: T;
    /
    ... */
    }

    interface EventListener {
    (evt: Event): void;
    }

    interface HTMLElement {
    /* ... */
    addEventListener(type: string, listener: EventListener, useCapture?: boolean): void;
    }
    —
    Reply to this email directly or view it on GitHub.

  3. RyanCavanaugh commented on Aug 4, 2014

    @RyanCavanaugh
    Member

    Note that not every event target is an Element (http://www.w3.org/TR/DOM-Level-3-Events/#event-types), so the solution here would have to be more along the lines of Kagami Sascha Rosylight (@saschanaz)'s suggestion.

    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.

  4. saschanaz commented on Aug 20, 2014

    @saschanaz
    Contributor

    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
  5. danquirk commented on Aug 20, 2014

    @danquirk
    Member

    That functionality does not exist, it's something akin to what's suggested in #285 and #229

  6. rayshan commented on Jun 16, 2015

    @rayshan

    Hi all, pinging this thread. I ran into this issue with Event.target. I would imagine most of the time a target is an HTMLElement. It is a little odd that the default type assumes it isn't. I tried Kagami Sascha Rosylight (@saschanaz)'s suggestion and ran into the "duplicate identifier" issue (sorry I may be missing something, I'm pretty new to TypeScript).

    Edit: this worked to quiet the warnings: (<HTMLElement>event.target).tagName

  7. kitsonk commented on Jun 16, 2015

    @kitsonk
    Contributor

    Ray Shan (@rayshan) the suggestion was for what should go into the lib.d.ts file and the file is auto generated based directly on the implementation of Internet Explorer. So the error you encountered is accurate. Right now the only way around it is to build without the built in lib (--noLib) and use the workaround, or do what you have done, which is cast the target. There are currently a number of challenges of dealing with the return types of the DOM right now in TypeScript (mostly because the DOM isn't the most consistent/structured/type safe/consistently implemented set of APIs).

  8. saschanaz commented on Oct 4, 2015

    @saschanaz
    Contributor

    Now that #4910 is merged, can this be revisited?

  9. danquirk commented on Oct 5, 2015

    @danquirk
    Member

    Zhengbo Li (@zhengbli) might have some context on the status of lib.d.ts issues that require script based generation

  10. mhegazy commented on Dec 10, 2015

    @mhegazy
    Contributor

    PRs welcomed. here is some infromation on contributing lib.d.ts changes: https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes

  11. 17 remaining items

  12. HolgerJeromin commented on Apr 3, 2018

    @HolgerJeromin
    Contributor

    What about adding

        readonly currentTarget: Element | null;
        readonly target: Element | null;

    on UIEvent only? This is not very specific, but makes most code happy.
    Perhaps even without | null as on a UIEvent both are always set

  13. simonh1000 commented on Oct 8, 2018

    @simonh1000

    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

  14. gautamkrishnar commented on Mar 20, 2019

    @gautamkrishnar

    We got rid of the error by adding type any to our code:

    this.url = (<any>event).target.result;
  15. iamstiil commented on Apr 12, 2019

    @iamstiil

    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> {
      ...
    }
  16. adigourdi commented on Nov 7, 2019

    @adigourdi

    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);
  17. alexandercerutti commented on Feb 25, 2020

    @alexandercerutti

    Are there any updates about this topic? It would be useful to have it without force casting event.target every time or any-casting event (as Gautam krishna R (@gautamkrishnar) did above) . Also this is open since 2014. Thank you!

  18. jonmol commented on Apr 11, 2020

    @jonmol

    Indeed, would be nice with some kind of official response to this issue before its 6th birthday

  19. mindplay-dk commented on Apr 14, 2020

    @mindplay-dk

    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.

  20. Dean-NC commented on Apr 14, 2020

    @Dean-NC

    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.

  21. allejo commented on Jun 21, 2022

    @allejo

    This is blocked by performance issue caused by generics as introducing generics doubles memory usage 😭

    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?

  22. DaSchTour commented on Jun 21, 2022

    @DaSchTour

    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.

  23. minecrawler commented on Mar 2, 2023

    @minecrawler

    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.

  24. nscarcella commented on Oct 12, 2023

    @nscarcella

    For anyone still interested in this feature, I created a PR here and could use some feedback regarding the performance.

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

    Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions