Skip to content
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

MutationObserver API declarations missing #11305

Closed
AvraamMavridis opened this issue Oct 2, 2016 · 9 comments
Closed

MutationObserver API declarations missing #11305

AvraamMavridis opened this issue Oct 2, 2016 · 9 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@AvraamMavridis
Copy link

The MutationObserver related declarations are missing from the lib.d.ts file. Since this API is not browser specific I would expect the declarations to exist (as it happens with FileReader for example).

TypeScript Version: 2.0.3

Code

if( window && window.MutationObserver  )
{
   ...
}

Expected behavior:

To not arise an error.

Actual behavior:

Error : Property MutationObserver does not exist on type Window

I "solved" it by using the window["MutationObserver"] syntax. If that is an actual bug/issue I can provide a PR.

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Oct 8, 2016

I'm almost positive that the proposal for mutation observers was dropped from ECMAScript. I don't have a reference handy but I believe that's the case

@AvraamMavridis
Copy link
Author

@aluanhaddad This has nothing to do with Object.observe or ECMAScript. It is part of the window object, same as FileReader or Geolocation that are not part of ECMAScript but they are supported by Typescript.

@aluanhaddad
Copy link
Contributor

@AvraamMavridis thanks for correcting me.

@kitsonk
Copy link
Contributor

kitsonk commented Oct 10, 2016

@AvraamMavridis changes to the DOM part of the lib.d.ts come via TSJS-lib-generator.

They do appear to be declared though in the global interfaces and all the examples on MDN demonstrate it that way.

So your detection might be better if you just did something like this:

if (window && MutationObserver) {
  const mo = new MutationObserver();
}

You will find that they are properly typed that way.

@AvraamMavridis
Copy link
Author

@kitsonk Thx a lot. I will give it a try later and if works I will close the issue.

@kpreisser
Copy link
Contributor

kpreisser commented Oct 10, 2016

Hi,

if (window && MutationObserver) {
  const mo = new MutationObserver();
}

note that this can throw a ReferenceError when running in strict mode and MutationObserver (or window) is not defined. I think you might need to use the typeof operator:

if (typeof MutationObserver != "undefined") {
  const mo = new MutationObserver();
}

@AvraamMavridis
Copy link
Author

@kpreisser what you described is "feature detection" and has nothing to do with Typescript and the issue above (missing declarations), and it will probably throw Error even in a non strict mode.

@kpreisser
Copy link
Contributor

kpreisser commented Oct 10, 2016

@AvraamMavridis,

@kpreisser what you described is "feature detection" and has nothing to do with Typescript and the issue above (missing declarations)

That's correct. However in your original example, you used if( window && window.MutationObserver) to check for window.MutationObserver, which would not throw a ReferenceError when it is not defined (and running in strict mode) because you are accessing a property on the window object here.

But as @kitsonk noted, you should use the global MutationObserver variable instead of window.MutationObserver because that one has declarations in TypeScript. But trying to use that variable in if (MutationObserver) can throw a ReferenceError when it is not defined and running in strict mode.

To correctly feature-detect it, you can use if (typeof MutationObserver != "undefined") instead of if (MutationServer) as the former will not throw a ReferenceError when it is not defined. That's what I wanted to note.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 11, 2016

thanks @kpreisser

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Oct 11, 2016
@mhegazy mhegazy closed this as completed Apr 19, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants