You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MutationObserver回调函数中mutationList的TypeScript类型是什么?

问题解答:MutationObserver回调中mutationList的TypeScript类型

在TypeScript中,MutationObserver回调函数里的mutationList对应的类型是**MutationRecord[]**,即MutationRecord对象的数组。

每个mutation项的类型MutationRecord包含DOM变更的完整信息,比如变更类型(type)、被修改的属性名(attributeName)、新增/移除的节点集合等,完全匹配你代码中对mutation的判断逻辑。

给回调函数显式添加类型标注后,代码类型会更清晰,还能获得TypeScript的类型校验和代码提示:

const targetNode = document.getElementById("some-id");

const config = { attributes: true, childList: true, subtree: true };

const callback = (mutationList: MutationRecord[], observer: MutationObserver) => {
  for (const mutation of mutationList) {
    if (mutation.type === "childList") {
      console.log("A child node has been added or removed.");
    } else if (mutation.type === "attributes") {
      console.log(`The ${mutation.attributeName} attribute was modified.`);
    }
  }
};

const observer = new MutationObserver(callback);

observer.observe(targetNode, config);

内容的提问来源于stack exchange,提问作者Anjan Talatam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 17:22:00