You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Web API type definition issue] lib.dom options parameter for MutationObserver#observe should not be optional #2566
An object providing options that describe which DOM mutations should be reported to mutationObserver's callback. At a minimum, one of childList, attributes, and/or characterData must be true when you call observe(). Otherwise, a TypeError exception will be thrown.
TypeScript could express "at least one of" with a union, but I don't think it's worth it here. Either MutationObserverInit itself becomes a union, which breaks interface X extends MutationObserverInit and building the options object incrementally, or the parameter gets a separate union type, and then values typed as the existing all-optional MutationObserverInit are no longer assignable. It would also need to model the implicit rules, where attributeOldValue or attributeFilter enable attributes, and characterDataOldValue enables characterData.
A minimal and safe improvement would be to make options required. Per the DOM spec, observe() throws a TypeError whenever none of childList, attributes or characterData ends up true, so calling observe(target) without options always throws (this is also covered by WPT in dom/nodes/MutationObserver-sanity.html). Making the parameter required would flag that mistake at compile time without affecting code that works at runtime.
Would a PR doing just that be welcome? I'd be happy to open it.
Activity
saschanaz commentedon Oct 7, 2026
Looks like this is only marked optional because webidl can't represent complex member dependency.
saschanaz commentedon Oct 7, 2026
But I'm not sure TypeScript can represent "pass one member at least" kind of type either?
MartinFerret commentedon Nov 1, 2026
TypeScript could express "at least one of" with a union, but I don't think it's worth it here. Either
MutationObserverInititself becomes a union, which breaksinterface X extends MutationObserverInitand building the options object incrementally, or the parameter gets a separate union type, and then values typed as the existing all-optionalMutationObserverInitare no longer assignable. It would also need to model the implicit rules, whereattributeOldValueorattributeFilterenableattributes, andcharacterDataOldValueenablescharacterData.A minimal and safe improvement would be to make
optionsrequired. Per the DOM spec,observe()throws aTypeErrorwhenever none ofchildList,attributesorcharacterDataends up true, so callingobserve(target)without options always throws (this is also covered by WPT indom/nodes/MutationObserver-sanity.html). Making the parameter required would flag that mistake at compile time without affecting code that works at runtime.Would a PR doing just that be welcome? I'd be happy to open it.
saschanaz commentedon Nov 1, 2026
We can start with that, yes.