简体   繁体   English

转到 Typescript 源定义而不是 Visual Studio Code 中的编译定义

[英]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 包:

  • Source code: /src/index.ts源代码: /src/index.ts
  • Compiled code: /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文件

vscode 去定义 vscode 打开.d.ts

However, I want VSCode to open the corresponding .ts file in /src但是,我希望 VSCode 在/src中打开相应的.ts文件

.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

  • you just need to run 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.

相关问题 HTML 中的 Visual Studio 代码“转到定义”不起作用 - Visual Studio Code “Go To Definition” in HTML does not work 使用Javascript和转到定义(F12)功能在Visual Studio代码中无法访问代码 - Code not reachable in Visual Studio Code with Javascript and Go To Definition (F12) functionality 使用项目名称指定导入时,Visual Studio Code预览/转到定义不起作用 - Visual Studio Code peek/go to definition not working when imports are specified with project name 是否可以在AngularJS项目的Visual Studio代码中获得方法定义? - Is it possible to get to the method definition in visual studio code for AngularJS project? 转到定义在Visual Studio 2015中不起作用(特别是在Javascript用户定义的函数中) - Go to definition is not working in visual studio 2015 (Specially in Javascript user defined functions) 是否有一个高级的javascript编辑器可以执行类似于Visual Studio的“去定义”这样的操作? - Is there an advanced javascript editor out there that will do things like 'go to definition' similar to Visual Studio? VS Code:“转到定义”不适用于导出的类 - VS Code: “go to definition” not working on exported class 在Visual Studio代码打字稿调试中找不到源映射 - Source maps not found in Visual Studio Code typescript debugging Chromium源代码中的showmodaldialog定义 - showmodaldialog definition from Chromium source code Typescript导入定义文件的路径与源路径不同 - Typescript import definition file with a different path than the source
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM