Skip to content

window.removeEventListener needs specialized signatures like addEventListener #3871

Closed

Description

The definition for addEventListener is on point. The definition for removeEventListener should essentially copy it.

Activity

  1. danquirk commented on Jul 15, 2015

    @danquirk
    Member

    Which version of lib.d.ts are you looking at? They appear the same in the latest bits

    interface EventTarget {
        addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void;
        dispatchEvent(evt: Event): boolean;
        removeEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void;
    }
    
    declare function addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void;
    declare function dispatchEvent(evt: Event): boolean;
    declare function removeEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void;
  2. carlpaten commented on Jul 15, 2015

    @carlpaten
    Author

    Version isn't clear, but there's this bit:

    addEventListener(type: "MSGestureChange", listener: (ev: MSGestureEvent) => any, useCapture?: boolean): void;
    addEventListener(type: "MSGestureDoubleTap", listener: (ev: MSGestureEvent) => any, useCapture?: boolean): void;
    addEventListener(type: "MSGestureEnd", listener: (ev: MSGestureEvent) => any, useCapture?: boolean): void;
    ...
    addEventListener(type: "waiting", listener: (ev: Event) => any, useCapture?: boolean): void;
    addEventListener(type: "wheel", listener: (ev: WheelEvent) => any, useCapture?: boolean): void;
    addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void;
    
  3. danquirk commented on Jul 15, 2015

    @danquirk
    Member

    Ah, I see, you mean you want all those specialized signatures for removeEventListener.

  4. changed the title [-]window.removeEventListener needs a more accurate type definition[/-] [+]window.removeEventListener needs specialized signatures like addEventListener[/+] on Jul 15, 2015
  5. mhegazy commented on Jul 16, 2015

    @mhegazy
    Contributor

    The reason addEventListener has these overloads is to allow for flowing contextual type to the event handler. but in the remove case, why would pass a lambda? LilRed can you share a sample where this is useful in removeEventListener

  6. carlpaten commented on Jul 18, 2015

    @carlpaten
    Author

    You call removeEventListener with a reference to the lambda you called
    addEventListener with.

    function foo(bar: StorageEvent) { console.log("foo!") };
    window.addEventListener("storage", foo); //event handler is registered
    window.removeEventListener("storage", foo); //back to normal
    
  7. mhegazy commented on Jul 20, 2015

    @mhegazy
    Contributor

    :) i understand. i was talking about the contextual type. contextual types are what allows you to get type of a lambda paramter e.g.:

    window.addEventListener("storage", (e) => {
        e; // <- e is StorageEvent becuase contextual type flowed from the definition of window.addEventListener("storage", ...);
    })

    so it seems the scenario you are talking about is:

    function foo(bar: StorageEvent) { console.log("foo!") };
    
    window.removeEventListener("click", foo); // Error MouseEventListener is not assignable to StorageEventListener

    is this correct? if so there is a different problem that would make this not work, once #1003 is fixed we can revisit this.

  8. mhegazy commented on Feb 22, 2016

    @mhegazy
    Contributor

    as mentioned earlier in my reply, this does not seem to be of much value as the event is usually defined in the addEventListener method. please reopen if this is not the case.

  9. MaxDaten commented on Nov 8, 2016

    @MaxDaten

    Hi,

    I'm using typescript with ionic 1.0 & cordova and bumping against this error:

    TypeScript error: typings/globals/InAppBrowser/index.d.ts(46,11): Error TS2430: Interface 'InAppBrowser' incorrectly extends interface 'Window'.
      Types of property 'addEventListener' are incompatible.
        Type '{ (type: "loadstart", callback: (event: InAppBrowserEvent) => void): void; (type: "loadstop", cal...' is not assignable to type '{ (type: "MSGestureChange", listener: (this: this, ev: MSGestureEvent) => any, useCapture?: boole...'.
          Types of parameters 'type' and 'type' are incompatible.
            Type '"MSGestureChange"' is not assignable to type '"loadstart"'.
    

    with:

    $ tsc --version
    Version 2.0.6
    

    What is the problem here and how to solve it? Thanks for any advice.

  10. mhegazy commented on Nov 8, 2016

    @mhegazy
    Contributor

    looks like InAppBrowser does not include all overloads for addEventListener which makes the class/interface not implement the Window interface correctly. the fix would be done on the declaration file i would say.

    //cc Michael Braude (@mbraude)

  11. locked and limited conversation to collaborators on Jun 19, 2018
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

    DeclinedThe issue was declined as something which matches the TypeScript visionDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptIn DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions