这是打字稿功能吗?

时间:2021-04-03 13:07:07

标签: javascript typescript vue.js vuejs3

我在阅读 vue-next source code 时遇到了一条我遇到的问题。我想这是因为我是打字稿的新手。

这是一个函数吗?它在做什么?

const isComment = (node: Node): node is Comment =>
  node.nodeType === DOMNodeTypes.COMMENT

2 个答案:

答案 0 :(得分:3)

是的。 node is Comment 语法用于传达这样一个事实,即函数的目的是检查传入的参数是否属于特定类型,在本例中为“注释”。

答案 1 :(得分:1)

node is Comment 是一个 type predicate,其中 node 必须是当前函数签名中的参数名称,每次调用 isComment 时,Typescript 都会缩小 Node 类型如果参数与 Comment 类型兼容,则为 Node 一个