Definition and references
Concept and boundary
Definition navigation resolves targets with file names and spans, while findReferences groups semantic uses. Apply UTF-16 offsets to the exact snapshot used for the query. A textual search can incorrectly include a shadowed name or omit an alias.
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: get-definition-at-position
Revisit reference fixture ts-api-get-definition-at-position (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": "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(); }Expected stdout (exit 0):
dep.ts twiceWorkshop 2: find-references
Revisit reference fixture ts-api-find-references (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 count = 1;\nconst total = count + count;\nfunction other() { const count = 9; return count; }"});const file = w.key("main.ts");const service = ts.createLanguageService(w.host);try { const groups = service.findReferences(file, w.read(file).indexOf("count")); assert.equal(groups.length, 1); assert.equal(groups[0].references.length, 3); assert.equal(groups[0].definition.fileName, file); console.log(groups.length, groups[0].references.length);} finally { service.dispose(); }Expected stdout (exit 0):
1 3Practice and recap
Add a shadowed local and confirm references to the original declaration do not include it.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Definition and references — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Definition and references?
What does workshop 1 (get-definition-at-position) 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": "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(); }What does workshop 2 (find-references) 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 count = 1;\nconst total = count + count;\nfunction other() { const count = 9; return count; }"});const file = w.key("main.ts");const service = ts.createLanguageService(w.host);try { const groups = service.findReferences(file, w.read(file).indexOf("count")); assert.equal(groups.length, 1); assert.equal(groups[0].references.length, 3); assert.equal(groups[0].definition.fileName, file); console.log(groups.length, groups[0].references.length);} finally { service.dispose(); }