8

I just started to learn Express JS and I am someone who learns the library by clicking "Go to definition" of IDE's and tracing each method/function.

But when I click on "Go to definition" of javascript files, Visual Studio Code takes me to the index.d.ts file which is the typescript version. This is no help to me as this doesn't help me understand the underlying architecture of libraries.

How can I view the original library source code from VS Code? If not, is there any similar feature in Github (or any other source) to click a method call that will take me to the method definition?

Thanks

Kalesh Kaladharan
  • 1,038
  • 1
  • 8
  • 26
  • 1
    `.d.ts` contains the type declaration, not "the compiled typescript version". – Bergi Sep 23 '18 at 08:56
  • I am a beginner, so I have very little idea about node.js and javascript in general. – Kalesh Kaladharan Sep 23 '18 at 09:03
  • Are you clicking on "go to definition" or "go to type definition"? – mpm Sep 23 '18 at 11:25
  • @mpm I tried right clicking and selected "Go to definition" and Ctrl+Click. Both took me to the same index.d.ts file – Kalesh Kaladharan Sep 23 '18 at 18:56
  • Duplicate of https://stackoverflow.com/questions/46893661/ – Matt Bierner Dec 03 '19 at 02:34
  • 1
    Does this answer your question? [Go to the TypeScript source file instead of the type definition file in VS Code](https://stackoverflow.com/questions/48711065/go-to-the-typescript-source-file-instead-of-the-type-definition-file-in-vs-code) – leonheess May 06 '22 at 22:13
  • 1
    See https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_68.md#go-to-source-definition v1.68 is adding a `go to source definition` command. – Mark Jun 07 '22 at 00:58

1 Answers1

2

If your tsconfig.json has "allowJs": true and "maxNodeModuleJsDepth" set to a sufficiently large number, and you uninstall the type declarations, then clicking "Go to definition" will take you to TypeScript's best guess of the definition in the JavaScript, which may be no good for JavaScript code that was never designed to be statically analyzable (e.g., I didn't get very far with Express). For a JavaScript package that includes type declarations directly in the package instead of having a separate @types package that you can uninstall, you'll have to manually copy the JavaScript source code (and not the type declarations) into a subdirectory of your project and set up the baseUrl and paths options so that TypeScript finds it.

This issue is for "Go to definition" to take you to the JavaScript even when the type declarations are installed.

Matt McCutchen
  • 28,856
  • 2
  • 68
  • 75