我正在与parse5
和@types/parse5
_v5.0.2_结合使用TypeScript一起玩耍。
入口函数parse
的函数签名为:
export function parse(html: string, options?: ParserOptions): Document;
但是,当我想使用Document
时,无法访问childNodes
属性。事实证明Document
是这样定义的:
/**
* Generic Document interface.
* Cast to the actual AST interface (e.g. {@link parse5.DefaultTreeDocument}) to get access to the properties.
*/
export type Document = DefaultTreeDocument | object;
我很迷糊!为什么这样定义?
如果我总是需要演员,那有什么意义?如果我并不总是需要强制转换,但是在某些情况下可以期望Document
不成为DefaultTreeDocument
,那我能期待什么?