TMB
TMB

Reputation: 4703

Is this a typescript function?

I was reading the vue-next source code, and came across a line I had a little trouble with. I figure this is because I am new to typescript.

Is this a function? What is it doing?

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

Upvotes: 2

Views: 57

Answers (2)

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

node is Comment is a type predicate, where node must be the name of a parameter from the current function signature, each time isComment is called the Typescript narrows the Node type to the Comment one if the parameter is compatible with Node type

Upvotes: 1

Fardeen Panjwani
Fardeen Panjwani

Reputation: 407

Yes. The node is Comment syntax is used to relay the fact that the function's purpose is to check if the passed-in parameter is of a specific type, "Comment" in this case.

Upvotes: 3

Related Questions