我有这个:
const node = window.getSelection().getRangeAt(0).startContainer;
(node as ChildNode).replaceWith(...nodes)
以上在构建时抛出错误:
Type 'Node' cannot be converted to type 'ChildNode'
Property 'remove' is missing in type 'Node'
但是如果我:
(node as any).replaceWith(...nodes)
编译得很好。谁能解释一下吗?
编辑:
ritaj 的回答得到了支持没有解决我的问题
最佳答案
制作类型保护:
function isChildNode(node: Node): node is ChildNode {
// Might want some other property checks as well
return 'replaceWith' in node;
}
并保护类型:
const node: Node = window.getSelection().getRangeAt(0).startContainer;
if (isChildNode(node)) {
node.replaceWith();
}
关于angular - 有什么特别的吗?类型 'Node' 无法转换为类型 'ChildNode',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56807577/