LanguageServiceHost
Concept and boundary
A language service host supplies file names, current versions, snapshots, compiler settings and filesystem operations. The service requests them on demand. A host that changes text without changing its version can leave stale results; dispose the service when done.
Local tooling setup
For each workshop use a fresh folder. Install the pinned compiler with npm install --save-dev typescript@5.8.3, then create the listed files and run node example.cjs with Node.js 22.16.0. Assertions fail if the expected behavior changes. These are local JavaScript tooling scripts, not browser CodeRunner tasks; they do not independently type-check the tooling source.
Workshop 1: language-service-host
Revisit reference fixture ts-api-language-service-host (historical V25, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const path = require("node:path");// Project documents live in memory; compiler libraries use the local filesystem.function workspace(initial) { const files = new Map(Object.entries(initial).map(([name, text]) => [path.resolve(name), { text, version: 0 }])); const options = { strict: true, target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, types: [], noEmit: true }; const key = name => path.resolve(name); const read = name => files.get(key(name))?.text ?? ts.sys.readFile(name); const host = { getCompilationSettings: () => options, getScriptFileNames: () => [...files.keys()], getScriptVersion: name => String(files.get(key(name))?.version ?? 0), getScriptSnapshot: name => { const text = read(name); return text === undefined ? undefined : ts.ScriptSnapshot.fromString(text); }, getCurrentDirectory: () => process.cwd(), getDefaultLibFileName: opts => ts.getDefaultLibFilePath(opts), fileExists: name => files.has(key(name)) || ts.sys.fileExists(name), readFile: read, readDirectory: ts.sys.readDirectory, directoryExists: ts.sys.directoryExists, getDirectories: ts.sys.getDirectories, useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames, getNewLine: () => "\n" }; function edit(name, text) { const file = files.get(key(name)); assert(file, "edit requires an existing file"); files.set(key(name), { text, version: file.version + 1 }); } return { host, edit, read, key };}const w = workspace({"main.ts": "export const n = 1;"});const file = w.key("main.ts");const service = ts.createLanguageService(w.host);try { const first = w.host.getScriptSnapshot(file); assert.equal(w.host.getScriptVersion(file), "0"); assert.equal(first.getText(0, first.getLength()), "export const n = 1;"); w.edit("main.ts", "export const n = 2;"); assert.equal(w.host.getScriptVersion(file), "1"); assert.equal(w.host.getScriptSnapshot(file).getText(0, 19), "export const n = 2;"); assert.equal(first.getText(0, first.getLength()), "export const n = 1;"); console.log("0 -> 1");} finally { service.dispose(); }Expected stdout (exit 0):
0 -> 1Workshop 2: create-language-service
Revisit reference fixture ts-api-create-language-service (historical V25, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const path = require("node:path");// Project documents live in memory; compiler libraries use the local filesystem.function workspace(initial) { const files = new Map(Object.entries(initial).map(([name, text]) => [path.resolve(name), { text, version: 0 }])); const options = { strict: true, target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, types: [], noEmit: true }; const key = name => path.resolve(name); const read = name => files.get(key(name))?.text ?? ts.sys.readFile(name); const host = { getCompilationSettings: () => options, getScriptFileNames: () => [...files.keys()], getScriptVersion: name => String(files.get(key(name))?.version ?? 0), getScriptSnapshot: name => { const text = read(name); return text === undefined ? undefined : ts.ScriptSnapshot.fromString(text); }, getCurrentDirectory: () => process.cwd(), getDefaultLibFileName: opts => ts.getDefaultLibFilePath(opts), fileExists: name => files.has(key(name)) || ts.sys.fileExists(name), readFile: read, readDirectory: ts.sys.readDirectory, directoryExists: ts.sys.directoryExists, getDirectories: ts.sys.getDirectories, useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames, getNewLine: () => "\n" }; function edit(name, text) { const file = files.get(key(name)); assert(file, "edit requires an existing file"); files.set(key(name), { text, version: file.version + 1 }); } return { host, edit, read, key };}const w = workspace({"main.ts": "const n: number = \"bad\";"});const file = w.key("main.ts");const service = ts.createLanguageService(w.host);try { assert.equal(service.getSemanticDiagnostics(file)[0].code, 2322); w.edit("main.ts", "const n: number = 7;"); assert.deepEqual(service.getSemanticDiagnostics(file), []); console.log("2322 -> clean");} finally { service.dispose(); }Expected stdout (exit 0):
2322 -> cleanPractice and recap
Add a second virtual file and ensure the host lists both roots.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
LanguageServiceHost — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes LanguageServiceHost?
What does workshop 1 (language-service-host) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const path = require("node:path");// Project documents live in memory; compiler libraries use the local filesystem.function workspace(initial) { const files = new Map(Object.entries(initial).map(([name, text]) => [path.resolve(name), { text, version: 0 }])); const options = { strict: true, target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, types: [], noEmit: true }; const key = name => path.resolve(name); const read = name => files.get(key(name))?.text ?? ts.sys.readFile(name); const host = { getCompilationSettings: () => options, getScriptFileNames: () => [...files.keys()], getScriptVersion: name => String(files.get(key(name))?.version ?? 0), getScriptSnapshot: name => { const text = read(name); return text === undefined ? undefined : ts.ScriptSnapshot.fromString(text); }, getCurrentDirectory: () => process.cwd(), getDefaultLibFileName: opts => ts.getDefaultLibFilePath(opts), fileExists: name => files.has(key(name)) || ts.sys.fileExists(name), readFile: read, readDirectory: ts.sys.readDirectory, directoryExists: ts.sys.directoryExists, getDirectories: ts.sys.getDirectories, useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames, getNewLine: () => "\n" }; function edit(name, text) { const file = files.get(key(name)); assert(file, "edit requires an existing file"); files.set(key(name), { text, version: file.version + 1 }); } return { host, edit, read, key };}const w = workspace({"main.ts": "export const n = 1;"});const file = w.key("main.ts");const service = ts.createLanguageService(w.host);try { const first = w.host.getScriptSnapshot(file); assert.equal(w.host.getScriptVersion(file), "0"); assert.equal(first.getText(0, first.getLength()), "export const n = 1;"); w.edit("main.ts", "export const n = 2;"); assert.equal(w.host.getScriptVersion(file), "1"); assert.equal(w.host.getScriptSnapshot(file).getText(0, 19), "export const n = 2;"); assert.equal(first.getText(0, first.getLength()), "export const n = 1;"); console.log("0 -> 1");} finally { service.dispose(); }What does workshop 2 (create-language-service) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const path = require("node:path");// Project documents live in memory; compiler libraries use the local filesystem.function workspace(initial) { const files = new Map(Object.entries(initial).map(([name, text]) => [path.resolve(name), { text, version: 0 }])); const options = { strict: true, target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, types: [], noEmit: true }; const key = name => path.resolve(name); const read = name => files.get(key(name))?.text ?? ts.sys.readFile(name); const host = { getCompilationSettings: () => options, getScriptFileNames: () => [...files.keys()], getScriptVersion: name => String(files.get(key(name))?.version ?? 0), getScriptSnapshot: name => { const text = read(name); return text === undefined ? undefined : ts.ScriptSnapshot.fromString(text); }, getCurrentDirectory: () => process.cwd(), getDefaultLibFileName: opts => ts.getDefaultLibFilePath(opts), fileExists: name => files.has(key(name)) || ts.sys.fileExists(name), readFile: read, readDirectory: ts.sys.readDirectory, directoryExists: ts.sys.directoryExists, getDirectories: ts.sys.getDirectories, useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames, getNewLine: () => "\n" }; function edit(name, text) { const file = files.get(key(name)); assert(file, "edit requires an existing file"); files.set(key(name), { text, version: file.version + 1 }); } return { host, edit, read, key };}const w = workspace({"main.ts": "const n: number = \"bad\";"});const file = w.key("main.ts");const service = ts.createLanguageService(w.host);try { assert.equal(service.getSemanticDiagnostics(file)[0].code, 2322); w.edit("main.ts", "const n: number = 7;"); assert.deepEqual(service.getSemanticDiagnostics(file), []); console.log("2322 -> clean");} finally { service.dispose(); }