TypeScript Compiler API
Scanner.scan()
Advances the mutable scanner to the next token, returning its kind. Stop at EndOfFileToken. Do not use raw scanning as a complete parser for context-sensitive tokens such as slash or angle-bracket sequences. Baseline: TypeScript 5.8.3; run locally with Node.js, not as a ByteMind CodeRunner exercise.
Formal Syntax & Structure
typescript syntaxtypescript
scanner.scan(): 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("let x = 2;");const kinds = [];for (let k = s.scan(); k !== ts.SyntaxKind.EndOfFileToken; k = s.scan()) kinds.push(k);assert.deepEqual(kinds,[ts.SyntaxKind.LetKeyword,ts.SyntaxKind.Identifier,ts.SyntaxKind.EqualsToken,ts.SyntaxKind.NumericLiteral,ts.SyntaxKind.SemicolonToken]);assert.equal(s.getToken(),ts.SyntaxKind.EndOfFileToken);console.log("five tokens then EOF");
// Run: node example.cjs// Expected stdout: five tokens then EOF