Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between `ts.SyntaxKind.VariableStatement` and `ts.SyntaxKind.FirstStatement`

Tags:

typescript

They both share the same enum value e.g. from TypeScript 3.7.2:

VariableStatement = 224,
FirstStatement = 224,

Why are there two names for the same syntax kind?

like image 385
basarat Avatar asked Dec 24 '19 04:12

basarat


1 Answers

In the SyntaxKind enum, there are some entries which are used to denote not the concrete syntax kind, but a class of them. For statements, it is defined as following:

enum SyntaxKind {
    // snip
    FirstStatement = VariableStatement,
    LastStatement = DebuggerStatement,
    // snip
}

Then, to check if some syntax kind is one of the "statement" kinds, one can simply write kind >= SyntaxKind.FirstStatement && kind <= SyntaxKind.LastStatement. And, if the set of statement kinds will ever change, only the definition of FirstStatement or LastStatement should be changed, not these "range checks".

like image 164
Cerberus Avatar answered Sep 25 '22 04:09

Cerberus