SyntaxKind and node guards
Concept and boundary
SyntaxKind identifies syntactic categories; supported isX guards narrow Node to a more specific syntax interface. A call and a new expression are different nodes even when they name the same function. Do not equate an Identifier with a resolved symbol.
Local tooling setup
For each workshop use a fresh folder. Install the pinned compiler with npm install --save-dev typescript@5.8.3, then create the listed files and run node example.cjs with Node.js 22.16.0. Assertions fail if the expected behavior changes. These are local JavaScript tooling scripts, not browser CodeRunner tasks; they do not independently type-check the tooling source.
Workshop 1: is-call-expression
Revisit reference fixture ts-api-is-call-expression (historical V30, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");function parse(text) { return ts.createSourceFile("input.ts", text, ts.ScriptTarget.ES2022, true, ts.ScriptKind.TS);}const f=parse("f(1); new F();"); const call=f.statements[0].expression;assert(ts.isCallExpression(call)); assert.equal(call.arguments.length,1); assert(!ts.isCallExpression(f.statements[1].expression));console.log("call expression; new expression excluded");Expected stdout (exit 0):
call expression; new expression excludedWorkshop 2: is-identifier
Revisit reference fixture ts-api-is-identifier (historical V30, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");function parse(text) { return ts.createSourceFile("input.ts", text, ts.ScriptTarget.ES2022, true, ts.ScriptKind.TS);}const f=parse("const x = 1;"); const d=f.statements[0].declarationList.declarations[0];assert(ts.isIdentifier(d.name)); assert.equal(d.name.text,"x"); assert(!ts.isIdentifier(d.initializer));console.log("x is identifier; 1 is not");Expected stdout (exit 0):
x is identifier; 1 is notPractice and recap
Inspect a property access and compare its name with the complete expression.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
SyntaxKind and node guards — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes SyntaxKind and node guards?
What does workshop 1 (is-call-expression) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");function parse(text) { return ts.createSourceFile("input.ts", text, ts.ScriptTarget.ES2022, true, ts.ScriptKind.TS);}const f=parse("f(1); new F();"); const call=f.statements[0].expression;assert(ts.isCallExpression(call)); assert.equal(call.arguments.length,1); assert(!ts.isCallExpression(f.statements[1].expression));console.log("call expression; new expression excluded");What does workshop 2 (is-identifier) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");function parse(text) { return ts.createSourceFile("input.ts", text, ts.ScriptTarget.ES2022, true, ts.ScriptKind.TS);}const f=parse("const x = 1;"); const d=f.statements[0].declarationList.declarations[0];assert(ts.isIdentifier(d.name)); assert.equal(d.name.text,"x"); assert(!ts.isIdentifier(d.initializer));console.log("x is identifier; 1 is not");