Skip to content

[Web API type definition issue] lib.dom options parameter for MutationObserver#observe should not be optional #2566

Open

Description

@trusktr

Summary

Not passing options, or passing options without minimum requirements, is an error (type error at runtime)

Expected vs. Actual Behavior

Expected:

Calling new MutationObserver(fn).observe(el) without a second options argument should show a type error because options are required.

Actual:

There is no type error. We discover the error at runtime.

Playground Link

https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAtiGBeGYCmB3GBZArlAQygEtwB5AIwjQCcA3WgCkYEpkA+GAbwF8WAofgHohMAAJQIAWjQAPAA5pgUGTRogaMKAAs0MaqDA0AJjAI0A5rjhowsYhBg00AR1zFnpgsFAniYCy1ECEVgADoYADkQWHkCCAh-QOJYHXUMRwIYABUAT0UAUTUNfgQwkCpaBkZjEGBrWygwihBjXJYgA

Browser Support

  • This API is supported in at least two major browser engines (not two Chromium-based browsers).

Have Tried The Latest Releases

  • This issue applies to the latest release of TypeScript.
  • This issue applies to the latest release of @types/web.

Additional Context

From MDN docs:

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.

Activity

  1. saschanaz commented on Oct 7, 2026

    @saschanaz
    Contributor

    Looks like this is only marked optional because webidl can't represent complex member dependency.

  2. saschanaz commented on Oct 7, 2026

    @saschanaz
    Contributor

    But I'm not sure TypeScript can represent "pass one member at least" kind of type either?

  3. MartinFerret commented on Nov 1, 2026

    @MartinFerret

    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.

  4. saschanaz commented on Nov 1, 2026

    @saschanaz
    Contributor

    We can start with that, yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions