TypeScript Compiler API
Scanner.getTokenText()
Returns the source spelling of the current token, including delimiters and escape spelling when applicable. This is not necessarily its decoded token value. With skipTrivia enabled, preceding whitespace is excluded. Baseline: TypeScript 5.8.3; run locally with Node.js, not as a ByteMind CodeRunner exercise.
Formal Syntax & Structure
typescript syntaxtypescript
scanner.getTokenText(): stringWorking 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(' "a\\n"');assert.equal(s.scan(),ts.SyntaxKind.StringLiteral);assert.equal(s.getTokenText(),'"a\\n"'); assert.equal(s.getTokenValue(),"a\n");console.log(JSON.stringify(s.getTokenText()));
// Run: node example.cjs// Expected stdout: "\"a\\n\""