TypeScript Compiler API

ts.ScriptSnapshot.fromString()

Creates a text snapshot implementing getText, getLength and getChangeRange. Treat a snapshot as the text at a particular version. fromString does not calculate an incremental change range; a custom snapshot can supply accurate change ranges to support incremental parsing. JavaScript string offsets use UTF-16 code units, not bytes or Unicode code points. Baseline: TypeScript 5.8.3 on Node.js. This is a tooling script, not an editor extension or a ByteMind CodeRunner example. Host documents are in memory with filesystem fallback for compiler libraries; the host is not a production project manager.

Formal Syntax & Structure

typescript syntaxtypescript
ts.ScriptSnapshot.fromString(text: string): ts.IScriptSnapshot

Parameters & Attributes

NameTypeDescription
textstringFull text at the document version.

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");const a = ts.ScriptSnapshot.fromString("A😀B");const b = ts.ScriptSnapshot.fromString("A😀C");assert.equal(a.getLength(), 4);assert.equal(a.getText(1, 3), "😀");assert.equal(b.getChangeRange(a), undefined);console.log(a.getLength(), a.getText(1, 3), b.getChangeRange(a));
// Run: node example.cjs// Expected stdout: 4 😀 undefined
TypeScript Reference