Skip to content

MediaQueryList.prototype.addListener & removeListener are marked as deprecated #32210

@fabiancook

Description

@fabiancook

TypeScript Version: master

Search Terms:

MediaQueryList addListener
addListener
MediaQueryList

Code

const listener = window.matchMedia("(max-width: 1000px)");
const handler = () => {};
listener.addListener(handler);
listener.removeListener(handler);

Expected behavior:

addListener and removeListener are "valid" signatures to invoke

Actual behavior:

addListener and removeListener are deprecated

Playground Link:
https://www.typescriptlang.org/play/#code/MYewdgzgLgBANgS2gUzMgTjAvDA7gsAExFwDoBbAQymAAsBZZQhSgCgCJWqAPAWn0JRaALhgBGAAxSADtwCU7OQG4AUKEixalInAzYYrOdgB8MAN4BfVYhRp0pSoUIAZJFFQZWWnRmUqb7nak6MjkIABuyK62nt6EuujKQA

Related Issues:
None

Notes:
This document describes the MediaQueryList interface: https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-addlistener

It doesn't list addListener and removeListener as deprecated, does not give an indication that they will be deprecated in the future, it also does not say that you should use addEventListener and removeEventListener instead.

It did say that addListener and removeListener are direct aliases.

Source:
https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts#L10018

Why would I bring this up:

Because Safari and Internet Explorer do not support addEventListener and removeEventListener, but they do support addListener and removeListener.

Because addListener and removeListener are deprecated it means that people are more likely to use the alternatives without looking at the issues with doing so.

A lot of examples with the usage of MediaQueryList show addListener and removeListener usage, even the documentation on MDN: https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList#Examples

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions