TypeScript Compiler API
LanguageService.getCodeFixesAtPosition()
Requests candidate code actions for diagnostic codes and a source range. Actions may include edits to multiple files and commands; requesting a fix does not apply it. Present choices, verify snapshot freshness, respect new-file edits and commands when supported, and recheck the result. This example chooses a single-file spelling correction and applies only its text changes. 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
service.getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: readonly number[], formatOptions: ts.FormatCodeSettings, preferences: ts.UserPreferences): readonly ts.CodeFixAction[]Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| fileName | string | Document path. |
| start | number | Start UTF-16 offset. |
| end | number | Exclusive end offset. |
| errorCodes | readonly number[] | Diagnostic codes to address. |
| formatOptions | ts.FormatCodeSettings | Formatting preferences. |
| preferences | ts.UserPreferences | User preferences. |
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 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 item = { count: 1 };\nitem.coutn;"});const file = w.key("main.ts");const service = ts.createLanguageService(w.host);try { const ds = service.getSemanticDiagnostics(file); assert.deepEqual(ds.map(d=>d.code), [2551]); const d = ds[0]; const fixes = service.getCodeFixesAtPosition(file, d.start, d.start+d.length, [d.code], {}, {}); const fix = fixes.find(f=>f.changes.some(c=>c.textChanges.some(t=>t.newText === "count"))); assert(fix); assert.equal(fix.changes.length, 1); assert.equal(fix.changes[0].fileName, file); let text = w.read(file); for (const c of [...fix.changes[0].textChanges].sort((a,b)=>b.span.start-a.span.start)) { text = text.slice(0,c.span.start)+c.newText+text.slice(c.span.start+c.span.length); } assert(text.includes("item.count;")); w.edit("main.ts", text); assert.deepEqual(service.getSemanticDiagnostics(file), []); console.log("2551 -> clean");} finally { service.dispose(); }
// Run: node example.cjs// Expected stdout: 2551 -> clean