Question: 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](https://i.stack.imgur.com/RrcUL.jpg)](https://i.stack.imgur.com/RrcUL.jpg)
[![vscode open .d.ts](https://i.stack.imgur.com/wx7B0.png)](https://i.stack.imgur.com/wx7B0.png)

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

[![.ts file](https://i.stack.imgur.com/FNy2Y.png)](https://i.stack.imgur.com/FNy2Y.png)

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.

Answer: 
TypeScript 2.9 introduced a compilerOption called [declarationMap](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-9.html). 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](https://github.com/flolude/stackoverflow-typescript-go-to-definition/pull/1) on your example repo to enable this setting.
The newer VSCode version (April 2022 or later) supports this with a feature called "Go to Source Definition" (see [here](https://code.visualstudio.com/updates/v1_67#_typescript-47-support)). It is still a work in progress I believe (given [this](https://github.com/microsoft/TypeScript/issues/49003) discussion thread), but should be working properly in most cases.