Skip to content

removeEventListener overloads should match the overloads provided for addEventListener #19386

Closed
@thebanjomatic

Description

@thebanjomatic

TypeScript Version: 2.6.0-rc

addEventListener has a bunch of handy overloads to map things like: 'touchmove' to (ev: TouchEvent) => any etc. Unfortunately, the same overloads are not provided for removeEventListener. Normally that isn't a problem, but with the new strictFunctionTypes option, valid code is now producing errors.

Code

Compiled with: --strictFunctionTypes

function handleTouchMove(event: TouchEvent) {

}

// Works fine because overloads are provided that map 'touchmove' to TouchEvent
window.addEventListener('touchmove', handleTouchMove, true);


// This produces an error 
window.removeEventListener('touchmove', handleTouchMove, true);

error:

error TS2345: Argument of type '(ev: TouchEvent) => any' is not assignable to parameter of type 'EventListenerOrEventListenerObject'.
  Type '(ev: TouchEvent) => any' is not assignable to type 'EventListenerObject'.
    Property 'handleEvent' is missing in type '(ev: TouchEvent) => any'.

You can use the following type assertion to defeat the error, but I don't believe that should be necessary:

window.removeEventListener('touchmove', <(ev: Event) => any>handleTouchMove, true);

Expected behavior:
Compiles without errors

Actual behavior:
Compiles with an error on the second parameter passed to window.removeEventListener

Related: #3871

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issueHelp WantedYou can do this

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions