Angular 如何键入保护子节点?还是将ChildNode方法与Typescript一起使用?

Angular 如何键入保护子节点?还是将ChildNode方法与Typescript一起使用?,angular,typescript,dom,Angular,Typescript,Dom,我想使用ChildNode方法。如果是ex.I typeguard,则: const node = window.getSelection().getRangeAt(0).startContainer; if (this.isChildNode(node)) { this.node.replaceWith(...); } isChildNode(node: Node): node is ChildNode { return 'replaceWith' in node; } 抛出错误

我想使用
ChildNode
方法。如果是ex.I typeguard,则:

const node = window.getSelection().getRangeAt(0).startContainer;

if (this.isChildNode(node)) {
  this.node.replaceWith(...);
}

isChildNode(node: Node): node is ChildNode {
  return 'replaceWith' in node;
}
抛出错误:

类型谓词的类型必须可分配给其参数的类型。 类型“ChildNode”不可分配给类型“Node”。属性“baseURI” 类型“ChildNode”中缺少

如果我投下它:

const node = window.getSelection().getRangeAt(0).startContainer;
(node as ChildNode).replaceWith(...nodes)
抛出错误:

无法将类型“Node”转换为类型“ChildNode”

类型“Node”中缺少属性“remove”

只有
any
编译应用程序:

(node as any).replaceWith(...nodes)


让我困惑的是,声明ChildNode不继承任何属性或方法。那么它是如何在Typescript中扩展节点的呢

为什么没有人建议在typeguard检查后进行铸造?在那里做完全安全,它编译

const node = window.getSelection().getRangeAt(0).startContainer;

if (this.isChildNode(node)) {
  (this.node as ChildNode).replaceWith(...nodes)
}

isChildNode(node: Node): node is ChildNode {
  return 'replaceWith' in node;
}

为什么没有人建议在typeguard检查后铸造?在那里做完全安全,它编译

const node = window.getSelection().getRangeAt(0).startContainer;

if (this.isChildNode(node)) {
  (this.node as ChildNode).replaceWith(...nodes)
}

isChildNode(node: Node): node is ChildNode {
  return 'replaceWith' in node;
}

ChildNode不扩展节点。在MDN上,您提到了
没有继承的方法。
那么,是什么让您认为它在typescript中扩展了呢?这个-
接口ChildNode扩展了Node
。澄清一下,我的代码运行时,只有Typescript无法编译,我在规范中确实看到了。。不知怎的,它在本地对我有效。你的准确密码。也许您可以尝试更新到angular的最新版本,使用它,typescript?升级angular不是一个选项(当前版本6),尽管我怀疑它不会有帮助,因为在我发布的Stackblitz演示中,使用angular 8和TypScript~3.1.1。你可以在那里看到这些错误的类型脚本,我使用了更高的类型脚本版本。您可以在这里找到一些与此相关的问题,而ChildNode不扩展节点。在MDN上,您提到了
没有继承的方法。
那么,是什么让您认为它在typescript中扩展了呢?这个-
接口ChildNode扩展了Node
。澄清一下,我的代码运行时,只有Typescript无法编译,我在规范中确实看到了。。不知怎的,它在本地对我有效。你的准确密码。也许您可以尝试更新到angular的最新版本,使用它,typescript?升级angular不是一个选项(当前版本6),尽管我怀疑它不会有帮助,因为在我发布的Stackblitz演示中,使用angular 8和TypScript~3.1.1。你可以在那里看到这些错误的类型脚本,我使用了更高的类型脚本版本。你可以在这里找到一些与此相关的问题,然后