TypeScript Compiler API
Scanner.getToken()
Reads the current token kind without advancing the scanner. Call scan first to establish the current token. Compare enum constants instead of assuming the numeric values or reverse enum names are stable. Baseline: TypeScript 5.8.3; run locally with Node.js, not as a ByteMind CodeRunner exercise.
Formal Syntax & Structure
typescript syntaxtypescript
scanner.getToken(): ts.SyntaxKindWorking Example
Example Snippettypescript
// Use a new empty directory. Install: npm install --save-exact typescript@5.8.3// File: example.cjs (omit this label)const ts = require("typescript");const assert = require("node:assert/strict");function scanner(text, skipTrivia = true) { return ts.createScanner(ts.ScriptTarget.ES2022, skipTrivia, ts.LanguageVariant.Standard, text);}const s = scanner("x + 1");assert.equal(s.scan(),ts.SyntaxKind.Identifier); const end=s.getTokenEnd();assert.equal(s.getToken(),ts.SyntaxKind.Identifier); assert.equal(s.getToken(),ts.SyntaxKind.Identifier);assert.equal(s.getTokenEnd(),end); assert.equal(s.scan(),ts.SyntaxKind.PlusToken);console.log("read without advancing");
// Run: node example.cjs// Expected stdout: read without advancing