TypeScript Compiler API

LanguageService.getDefinitionAtPosition()

Finds definition locations corresponding to the symbol at a position, potentially across files. Results have fileName and textSpan; there can be more than one definition or none. Use the corresponding target snapshot to translate offsets to editor positions. Definitions may refer to declarations rather than source implementations. 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.getDefinitionAtPosition(fileName: string, position: number): readonly ts.DefinitionInfo[] | undefined

Parameters & Attributes

NameTypeDescription
fileNamestringAbsolute path identifying the document in the host.
positionnumberZero-based UTF-16 code-unit offset in that document snapshot.

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": "import { twice } from \"./dep\";\nconst n = twice(4);", "dep.ts": "export function twice(n: number) { return n * 2; }"});const file = w.key("main.ts");const service = ts.createLanguageService(w.host);try {  const defs = service.getDefinitionAtPosition(file, w.read(file).lastIndexOf("twice"));  assert(defs?.length);  const target = defs.find(d => d.fileName === w.key("dep.ts"));  assert(target);  assert.equal(w.read(target.fileName).slice(target.textSpan.start, target.textSpan.start + target.textSpan.length), "twice");  console.log(path.basename(target.fileName), target.name);} finally { service.dispose(); }
// Run: node example.cjs// Expected stdout: dep.ts twice
TypeScript Reference