[英]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:我正在使用 Visual Studio Code 开发一个打字稿项目,其中包括多个结构如下的 npm 包:
/src/index.ts
/src/index.ts
/dist/...
/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
当我右键单击导入的对象并选择“转到定义”或单击
F12
或按住CTRL
单击对象时,Visual Studio Code 在/dist
中打开相应的.d.ts
文件
However, I want VSCode to open the corresponding .ts
file in /src
但是,我希望 VSCode 在
/src
中打开相应的.ts
文件
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我已经创建了 git repo,所以你可以自己尝试一下: https ://github.com/flolude/stackoverflow-typescript-go-to-definition
yarn bootstrap
in order to replicate the issue.yarn bootstrap
即可复制问题。 TypeScript 2.9 introduced a compilerOption called declarationMap . TypeScript 2.9引入了一个名为declarationMap的compilerOption。 Per the release notes:
根据发行说明:
Enabling --declarationMap alongside --declaration causes the compiler to emit .d.ts.map files alongside the output .d.ts files.
启用--declarationMap和--declaration会导致编译器在输出.d.ts文件旁边发出.d.ts.map文件。 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.
换句话说,在使用--declarationMap生成的.d.ts文件中对声明进行定义将转到将定义该声明的源文件(.ts)位置,而不是.d。 TS。
I submitted a PR on your example repo to enable this setting. 我在您的示例回购中提交了PR以启用此设置。
The newer VSCode version (April 2022 or later) supports this with a feature called "Go to Source Definition" (see here ).较新的 VSCode 版本(2022 年 4 月或更高版本)通过名为“转到源代码定义”的功能支持此功能(请参阅此处)。 It is still a work in progress I believe (given this discussion thread), but should be working properly in most cases.
我相信它仍在进行中(鉴于此讨论线程),但在大多数情况下应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.