Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go to Typescript Source Definition instead of Compiled Definition in Visual Studio Code

I am working on a typescript project with Visual Studio Code including multiple npm packages structured like this:

  • Source code: /src/index.ts
  • Compiled code: /dist/...

When I right click on imported objects and choose "Go to Definition" or click F12 or by clicking on the object with holding down CTRL, Visual Studio Code opens the corresponding .d.ts file in /dist

vscode go to definition vscode open .d.ts

However, I want VSCode to open the corresponding .ts file in /src

.ts file

Is it possible to change this behavior, as it is really annoying to manually search for the source file.

I've created git repo, so that you can try it yourself: https://github.com/flolude/stackoverflow-typescript-go-to-definition

  • you just need to run yarn bootstrap in order to replicate the issue.
like image 396
Flo Avatar asked Aug 28 '19 17:08

Flo


People also ask

How are typescript files treated in Visual Studio Code?

File Scope: In this mode, TypeScript files opened in Visual Studio Code are treated as independent units. As long as a file a.ts doesn't reference a file b.ts explicitly (either using /// reference directives or external modules), there is no common project context between the two files.

How do I navigate a typescript project?

Code navigation lets you quickly navigate TypeScript projects. Go to Definition F12 - Go to the source code of a symbol definition. Peek Definition Alt+F12 - Bring up a Peek window that shows the definition of a symbol. Go to References Shift+F12 - Show all references to a symbol.

How to install typescript definition files for modules and packages?

Installing TypeScript Definition Files for Modules and Packages a. Find the .d.ts file for a library by name in the Typings registry. Note: run the commands below from the root of your project. b. The command to run depends on the type of package. To learn more, visit the Typings README.md. The “ — save” flag will add a file to typings.json. 4.

How does VS Code support JavaScript and typescript?

To summarize: VS Code's JavaScript and TypeScript support is powered by TypeScript behind the scenes. To understand the APIs of external modules, TypeScript uses d.ts files for performance and scalability reasons. This way it does not have to try to load or parse all of the module's code itself.


1 Answers

TypeScript 2.9 introduced a compilerOption called declarationMap. Per the release notes:

Enabling --declarationMap alongside --declaration causes the compiler to emit .d.ts.map files alongside the output .d.ts files. Language Services can also now understand these map files, and uses them to map declaration-file based definition locations to their original source, when available.

In other words, hitting go-to-definition on a declaration from a .d.ts file generated with --declarationMap will take you to the source file (.ts) location where that declaration was defined, and not to the .d.ts.

I submitted a PR on your example repo to enable this setting.

like image 175
Brady Holt Avatar answered Oct 06 '22 07:10

Brady Holt