Search code examples
javascripthtmlgoogle-closure-compilermutation-observers

What is the way to use MutationObserverInit with the Closure Compiler?


On the Mozilla Developer Network, there is an example on how to use a MutationObserver.

// select the target node
var target = document.querySelector('#some-id');

// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.log(mutation.type);
  });    
});

// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };

// pass in the target node, as well as the observer options
observer.observe(target, config);

// later, you can stop observing
observer.disconnect();

The config variable is an Object. But the closure compiler would get angry and

ERROR - actual parameter 2 of MutationObserver.prototype.observe does not match formal parameter
found : {attributes: boolean, characterData: boolean, childList: boolean}
required: (MutationObserverInit|null|undefined) observer.observe(node, config);

As it is set in the externs/html5.js.
However, I cannot find a way to instantiate a MutationObserverInit typed object because 'Uncaught ReferenceError: MutationObserverInit is not defined'.

What is the right thing to do here?


Solution

  • I think the extern file is wrong and should be fixed.

    This is work around:

    var config = /** @type {MutationObserverInit} */ ({ attributes: true, childList: true, characterData: true });