TypeScript Compiler API
ts.System
The System interface abstracts file I/O, directory access, path conventions, process interaction and optional watcher/timer capabilities. A custom implementation must maintain consistent file identity, timestamps and content. Override selected operations on a real system when appropriate; avoid claiming a partial mock is a complete virtual filesystem. Baseline: TypeScript 5.8.3, Node.js. Run as a local tooling script in a disposable directory; examples create files. Not a ByteMind CodeRunner example.
Formal Syntax & Structure
typescript syntaxtypescript
interface ts.System; readFile(path: string, encoding?: string): string | undefined; writeFile(path: string, data: string, writeByteOrderMark?: boolean): voidWorking 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 fs = require("node:fs");const path = require("node:path");function put(name, text) { fs.mkdirSync(path.dirname(name), { recursive: true }); fs.writeFileSync(name, text);}const seen = [];const system = { ...ts.sys, readFile(name, encoding) { seen.push(path.basename(name)); return ts.sys.readFile(name, encoding);} };put("main.ts", "export const n = 7;");const opts = { types: [], noEmit: true, strict: true };const host = ts.createIncrementalCompilerHost(opts, system);const builder = ts.createIncrementalProgram({ rootNames: [path.resolve("main.ts")], options: opts, host });assert.deepEqual(builder.getSemanticDiagnostics(), []);assert(seen.includes("main.ts"));console.log("custom system read observed");
// Run: node example.cjs// Expected stdout: custom system read observed