为什么需要强制转换@ types / parse5类型?

时间:2019-10-22 17:45:28

标签: typescript parse5

我正在与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,那我能期待什么?

0 个答案:

没有答案