TypeScript Compiler API
Scanner.resetTokenState() / setTextPos()
Resets scanning state to a supplied source offset. setTextPos is a deprecated alias; prefer resetTokenState. Scan again after resetting. Use known token boundaries; an arbitrary offset may begin in the middle of a token and need parser context. Baseline: TypeScript 5.8.3; run locally with Node.js, not as a ByteMind CodeRunner exercise.
Formal Syntax & Structure
typescript syntaxtypescript
scanner.resetTokenState(pos: number): void; scanner.setTextPos(textPos: number): voidParameters & Attributes
| Name | Type | Description |
|---|---|---|
| pos / textPos | number | Zero-based UTF-16 source offset. |
Working 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 + b"); s.scan(); s.scan();s.resetTokenState(0); assert.equal(s.scan(),ts.SyntaxKind.Identifier); assert.equal(s.getTokenText(),"a");s.setTextPos(4); assert.equal(s.scan(),ts.SyntaxKind.Identifier); assert.equal(s.getTokenText(),"b");console.log("reset to a; legacy reset to b");
// Run: node example.cjs// Expected stdout: reset to a; legacy reset to b