TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Enable Go to Implementation in Typescript Editors

Open doliver3 opened this issue 10 years ago • 133 comments

Could you provide a way for Typescript Editors to list the implementations of a type? This would definitely help developer productivity to be able to find implementations of types faster.

For example, in the Atom Editor atom-typescript plugin you can press F12 to 'Go to Declaration' which as currently implemented you often arrive at the typescript .d.ts file which has the function signature but not the actual implementation.

Since the Typescript parser often generates the d.ts files from the .ts implementation files. Could that association between the d.ts and .ts files be preserved (perhaps output with a typescript compiler option) so that Typescript editors could take developers to a list of the implementing classes or functions?

I opened a similar request for the Atom-typescript editor, but they indicate that they cannot implement this without Typescript parser support: TypeStrong/atom-typescript#790

doliver3 avatar Dec 22 '15 20:12 doliver3

@wtfzambo - my understanding is that when sourcemaps are involved (as they will usually be for imports from node_modules) then declaration maps are also needed in addition to sourcemaps. A "declaration map" enables connecting a TS declaration to the implementation, even if the implementation is in JS which is sourcemapped back to TS.

See https://stackoverflow.com/questions/58459988/what-is-the-use-case-of-enabling-declaration-and-declarationmap-in-tsconfig-json for details.

Relatively few npm packages have declaration maps enabled. ;-(

There might be other issues going on in your case besides missing declaration maps, but that's one place to start.

justingrant avatar Apr 22 '21 20:04 justingrant

@andrewmartin cmd+f12 (Go to implementations) does not work for me, it still takes me to the type definition file or just says "No implementations available" . Is there another setting that needs to be configured to make this work?

gaurav5430 avatar Jul 19 '21 18:07 gaurav5430

So, this is the reason why this works when a file is named source.js (it brings me to the definition)

image

And does not when it is called source.ts?

Really, after 7 years!

christophe-g avatar Jul 20 '21 14:07 christophe-g

So, this is the reason why this works when a file is named source.js (it brings me to the definition)

image

And does not when it is called source.ts?

Really, after 7 years!

what is the reason exactly?

gaurav5430 avatar Jul 20 '21 15:07 gaurav5430

But I guess I was expecting a python-like behavior that in JS is not really a thing, as one of my colleagues explained to me.

It should be a thing. If it's not a thing (it usually isn't) then that's because of things not working like intended. I had a js project once where it all worked and it was glorious.

ThaJay avatar Jul 29 '21 09:07 ThaJay

Can confirm this is very annoying. "Go to definition" in VSCode works same as "Go to type definition"

mankey-ru avatar Aug 03 '21 10:08 mankey-ru

I'm not sure if this case is about jumping to the compiled JS or the original TS file, the former I see as being less useful. Usually I want to jump to the TS source file instead of the generated d.ts.

Robula avatar Aug 04 '21 10:08 Robula

I'm not sure if this case is about jumping to the compiled JS or the original TS file, the former I see as being less useful. Usually I want to jump to the TS source file instead of the generated d.ts.

The ideal behavior would be for the IDE to navigate to the "original source", meaning the file pointed to by the sourcemap of the transpiled JS. Sometimes this will be a TS file, sometimes a JS file that's been transpiled, or could even be some other language that's been transpiled down to JS.

I agree that showing the transpiled JS is not as helpful, and could be actively confusing for some transpilations (e.g. of async/await) which are more-or-less incomprehensible for newbies... and sometimes for experts too!

justingrant avatar Aug 04 '21 18:08 justingrant

Is there anything blocking a community extension to get around this issue?

sabetAI avatar Aug 23 '21 17:08 sabetAI

I'm not sure if this case is about jumping to the compiled JS or the original TS file, the former I see as being less useful. Usually I want to jump to the TS source file instead of the generated d.ts.

I just want to go to exactly the place where the import points. Nothing else is even remotely interesting compared to this dead simple link. If the one file says import {Foo} from ../Stuff and I follow Foo, I want to go one folder up, into the file Stuff and highlight the line that says export Foo.

It seems quite simple and I rather not have typescript at all than this not working. And above all, it has been 6 YEARS! I would even consider completely dropping vscode by this point.

ThaJay avatar Aug 24 '21 12:08 ThaJay

GitHub UI supports click to jump to implementation OR type definition. I wonder what trickery they are using...

Screenshot 2021-09-21 at 12 50 52

mifi avatar Sep 21 '21 05:09 mifi

I might soon ditch VSCode in favor of Github UI then😄

On Tue, 21 Sep 2021 at 10:53 Mikael Finstad @.***> wrote:

GitHub UI supports click to jump to implementation OR type definition. I wonder what trickery they are using...

[image: Screenshot 2021-09-21 at 12 50 52] https://user-images.githubusercontent.com/402547/134118734-3dd264fc-1542-4ed1-b191-ac3342af21ea.png

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/microsoft/TypeScript/issues/6209#issuecomment-923650713, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGZIA5D4ONECWWXGDBKXSQ3UDAMVTANCNFSM4BXI277A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

sb-bilal-dev avatar Sep 21 '21 06:09 sb-bilal-dev

Is this ticket not in the right place? Is everyone else happy manually trawling through node_modules every time they want to look at an implementation or is there something I'm missing?

publicvirtualvoid avatar Oct 31 '21 00:10 publicvirtualvoid

Is this ticket not in the right place? Is everyone else happy manually trawling through node_modules every time they want to look at an implementation or is there something I'm missing?

No, you're not missing anything, everybody is indeed very unhappy with the current situation (there's 100 hidden items above)

phil294 avatar Oct 31 '21 00:10 phil294

Every project has tickets like this @publicvirtualvoid that is absolutely normal, I can name 10 tickets like this right away right now in other projects 😂, maybe not 10 right now, but you know what I mean 🙂

silverqx avatar Oct 31 '21 06:10 silverqx

@silverqx at least maintainers in other projects have better outreach (ie Flutter, even with all its issues). If they could explain their rationale and all the technical roadblocks, I'm sure the community could help in some way. It's been six years already for such a fundamental feature though, so a solution is probably never coming. It's a pretty embarrassing situation for their team to be in tbh.

sabetAI avatar Nov 01 '21 03:11 sabetAI

@sabetAI I think the reasonable explanation is that go to implementation has nothing to do with typescript, Actually if you delete all the type declaration files go to implementation works.


second edit

@justingrant this problem is a purely technical issue, remove my wild guess

kevinwang930 avatar Nov 01 '21 05:11 kevinwang930

I'm happy to see continued enthusiasm for solving this problem. Keep the upvotes and (especially) constructive suggestions and PRs coming! Like many hard things, eventually I'm sure we'll help to get a solution built for this annoying gap in the TS developer experience.

@sabetAI I think the reasonable explanation is that go to implementation has nothing to do with typescript, and as a typescript team they definitely want everyone use typescript, so they has less willingness to improve the javascript user experience. Actually if you delete all the type declaration files go to implementation works.

As far as I can tell, this impression is not correct. Almost all* packages that are written in TS, transpiled to JS, and published to npm don't work with "Go To Implementation" any more than packages originally written in JS.

I said "almost all" above because there is a way to make it work: using the declarationMap tsconfig compiler option. But:

  1. This setting is not enabled by default. IMHO it should be enabled by default whenever sourceMap is enabled. (Maybe also when inlineSourceMap is enabled?)
  2. There is AFAICT no evangelism from the TS team (or anyone else!) about how package authors should enable this setting and include declaration map files when publishing their packages to npm.
  3. There's also no push to get declaration map files into existing DefinitelyTyped packages. Could this be automated instead of relying on 1000s of type-package maintainers to do it?

Anyone who wants to see this problem get better could:

  1. Send a PR to try to change the default of declarationMap to be true if the sourceMap or inlineSourceMap option is true. This minor change would probably have the biggest impact on this problem over time.

  2. Send a PR to update the TS docs to be more vehement that declarationMap should be enabled for all library packages, and explaining why ("because "Go To Implementation" in VS Code relies on declaration map files"). These documentation changes will be crucial if (1) is not approved.

  3. Send a PR that adds or extends the TS docs with specific guidance for package authors, including "publish your sourcemaps" and "publish declaration maps".

  4. Write a tool that will automatically add declaration map files to all compatible DefinitelyTyped packages, and convince the DefinitelyTyped maintainers to merge the output of that tool into every compatible DefinitelyTyped package. Part of the work would be figuring out what "compatible" means and what packages' declaration maps can't be automated. Note that @weswigham above was pessimistic that this automation would be possible. But I wonder if things have improved since then to make automation possible? Or if it'd be possible for a large subset of packages? Here's what he said:

There's no easy way to navigate to the JS or TS implementation of a TS type if that type is declared in a DefinitelyTyped package. (or other cases where .d.ts isn't colocated with the implementation)

Technically if you wanted to hand spin some sourcemaps to go with a DT package, it could kinda work out already. But there's definitely no automated solution here. Any solution we do have is based on improving scenarios involving TS compiler output, at those are the only times when we can otuput the additional metadata required to make the correct associations between files. Some packages support this today, I believe - I was using the azure js sdk the other day and debugging some stuff, and was pleasantly surprised when I went to some definitions and discovered they were in the original source. So yeah, it works with TS sources that have up-to-date --declarationMaps output bundled with them.

  1. Send a PR to change the build actions for DefinitelyTyped to warn users if they try to add a new package that lacks a declaration map. Not sure if this is something that can be done outside the DT maintainer team, depending on how the existing DT toolchain works. Someone who knows DT better than me could answer this.

The community has agency! If we want things to get better, any of the items above can make things better.

There's also IMHO another confusion with "Go to Definition" vs. "Go To Type Definition" are implemented. My preference would be for "Go to Type Definition" to navigate to the types and "Go to Definition" to navigate to the implementation. Not sure this is worth changing community expectations, though, given how long the current behavior has been in place. For more details, see: https://github.com/microsoft/TypeScript/issues/6209#issuecomment-525911553

justingrant avatar Nov 01 '21 20:11 justingrant

Go To Type Definition doesn't work when the selection is itself a type. You'll get a "no type definition found" error.

I think y'all should open a new issue on that one. That's just a failure of exepections/bug, imo. Minimally, the error could be made better - something like "selection is only a type and so has no type definition associated with its value".

@weswigham - sorry for delayed reply, I just opened #46627 for this.

justingrant avatar Nov 01 '21 20:11 justingrant

Thanks @justingrant! I want to make a few observations.

If I understand correctly, the thing in highest demand here is to be able to jump (whether as an explicit action or automatically under the hood as part of an existing UI action) from a location in a .d.ts file to a location in a .js file (and perhaps a .ts file is also an acceptable target for some people if it exists). The declaration maps and source maps generated by the TypeScript compiler allow you to jump from a location in an output file (.js or .d.ts) to a location in an input file (typically .ts, but could be .js).

On top of this, I’ll add some assertions that I think most people would agree with:

  • Most npm libraries written in TypeScript do not publish their .ts sources to npm; they publish only the .js and .d.ts outputs.
  • DefinitelyTyped packages often do not track versions 1:1 with their implementation libraries; DT is updated asynchronously by volunteers, and furthermore, many patch version bumps of the implementation libraries do not require updates to DT as these patches typically don't change the public API surface.

Putting all this together, I can draw a few intermediate conclusions:

  • Even if it were possible to auto-generate declaration maps for DefinitelyTyped, it would not be practical because the chances are extremely low that any user who obtains that declaration map would have the exact version of the implementation library that it was generated against. Every time the implementation library changes anything, even just adding a newline somewhere, the DT package would have to be regenerated against that source, and users would have to update them in lockstep or else they’d get sent to an inaccurate file / line number. This is a total non-starter.
  • Turning our attention away from the DefinitelyTyped problem and toward libraries written in TypeScript: enabling and shipping declaration maps alone would not change anything; those libraries would need to ship the declaration maps and the .ts source files in order for us to use the declaration maps. If libraries started doing that, we could easily jump from the .d.ts to the .ts (or skip the .d.ts altogether—maybe this is already what happens?). Even in that case, though, we would have no way of getting to the .js output. I think some people would be happy just to see the .ts source, but I can also imagine pushback at the idea of making the world download TypeScript source files from npm just to light up an editor feature. I think more people would be just as happy or happier to be able to go to the .js and leave the .ts out of their node_modules. But nothing we’ve discussed so far is remotely useful in making this happen.

Because of these problems, I’m actually a little confused at the focus of this issue on declaration maps. It seems like they would help only in edge case scenarios, and I think I can categorically reject the idea that we could use them to map from DefinitelyTyped to another source. The only solution I can think of that could solve the problem both for DefinitelyTyped and for bundled type definitions is, at the time of the Go-To-Implementation request, for us to make an effort to find, parse, and bind the corresponding JS file and make an effort to find the symbol in question. This would surely fail in the face of highly dynamic or unusual JS patterns, or where .d.ts files don't cleanly map 1:1 to .js files. It could also be expensive in the face of huge bundled and minified files. This would have to be understood as a heuristic-based best effort, and perhaps a worst-case fallback would be to take you to the top of the package’s "main" (or relevant "exports") file. But I also suspect that this process would work perfectly for a lot of libraries, and personally, I think just opening up the "main" entrypoint would save me time over scrolling through hundreds of node_modules folders when I really need to track something down in the source.

andrewbranch avatar Nov 02 '21 21:11 andrewbranch