Sub topic 12
Go to Definition and Editor Behavior Across Projects
Assume you know basic TypeScript types, tsconfig.json, ES modules, and the purpose of project references. This chapter focuses on what happens inside an editor when one TypeScript project uses another.
What project references change in the editor
Without project references, an editor may treat a large repository as one source tree. It must discover and analyze many files together. As the repository grows, this can increase memory use and slow features such as type checking, autocomplete, and Go to Definition.
A project reference gives the TypeScript language service a dependency boundary. The consuming project knows that it depends on another project and can use that project's generated declaration files.
A declaration file is a .d.ts file that describes the public types and exported values of a module without containing its implementation.
For example, core might expose this source:
export function greet(name: string): string {
return `Hello, ${name}!`;
}After building, TypeScript can generate a declaration file:
export declare function greet(name: string): string;The app project can type-check against this smaller public description instead of analyzing every implementation detail in core.
Go to Definition across a reference
When you select greet in the app project and choose Go to Definition, the editor follows the project relationship.
With declaration maps enabled, the editor can map the generated declaration back to the original TypeScript source:
{
"compilerOptions": {
"composite": true,
"declaration": true,
"declarationMap": true,
"rootDir": "src",
"outDir": "dist"
}
}The generated files usually include:
greet.d.ts, which describes the exported API.greet.d.ts.map, which connects the declaration to the source file.greet.js, which contains the compiled implementation.
The .d.ts.map file improves navigation and debugging. Without it, Go to Definition may open the declaration in dist instead of src. That view is still correct for type information, but it is less useful when you want to understand the implementation.
Source redirects and project boundaries
TypeScript can sometimes redirect an import from a built declaration back to the referenced project's source. This gives a smooth editing experience while preserving project boundaries for builds.
The behavior can vary with compiler options, TypeScript version, and whether the referenced project has been built. If the declaration output is missing, the editor may show errors or navigate to an unavailable generated file.
The disableSourceOfProjectReferenceRedirect option disables this source redirection:
{
"compilerOptions": {
"disableSourceOfProjectReferenceRedirect": true
}
}With this option enabled, the editor uses the referenced project's declaration output directly. This can more closely match CI or package-consumer behavior, but navigation may lead to .d.ts files instead of source files.
| Configuration | Typical navigation target | Main benefit |
|---|---|---|
| `declarationMap: true` | Original `.ts` source | Convenient implementation navigation |
| No declaration map | Generated `.d.ts` file | Works with only declaration output |
| `disableSourceOfProjectReferenceRedirect: true` | Generated `.d.ts` file | Tests the public project boundary |
Common editor problems
If Go to Definition opens the wrong file, check these items:
- The referenced project has
"composite": true. - The referenced project emits declarations.
declarationMapis enabled when source navigation is expected.- The output directory is not excluded or deleted unexpectedly.
- The app's
referencesentry points to the directory containing the referencedtsconfig.json. - The editor is using the workspace TypeScript version you expect.
Run a build from the solution root before diagnosing editor errors:
tsc --buildThis creates the declaration and build-information files that the language service may need.
Exercise
An editor opens core/dist/greet.d.ts instead of core/src/greet.ts when you use Go to Definition. What should you check first?
Show answer
Check that the core project emits declarationMap: true, then run tsc --build so the .d.ts.map file exists. Also confirm that the editor is using the intended workspace TypeScript version.