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

Could someone from typescript team suggest where in code this can be potentially fixed?

sheerun avatar Jun 09 '19 12:06 sheerun

A funny thing is that if some package doesn't have types available "Go To Type Definition" actually works better for it. It means that not using typings is better if you like "Go To" features 😄

Here's screencast that shows that I'm able to go to definition of device packages that doesn't have typings, but compression leads me to some random typings file:

https://giphy.com/gifs/j3VCp0LVKr5LsMUh11

@RyanCavanaugh I must agree with other people, this should be labeled as bug, not suggestion..

sheerun avatar Jun 09 '19 15:06 sheerun

in my case, if I have something like this defined in my own code

const Foo: <T> = () => {
...
}

if the T comes from a npm module, when I try to go to definition, it will show me 2 definitions. My own code and the typing definition. If I go to type definition it goes straight to the definition file. In the first case, it should go straight to the definition. To work around this, I changed the editor --> goto location: multiple to goto instead of peek

tafelito avatar Jul 02 '19 13:07 tafelito

I need this feature. How can I help with its implementation?

yurylavrukhin avatar Jul 08 '19 09:07 yurylavrukhin

+1 for this please!

rjmacarthy avatar Aug 20 '19 18:08 rjmacarthy

since 2015 and no solutions :(

nathanredblur avatar Aug 28 '19 16:08 nathanredblur

The OP was asking if we could go to .ts files when we find .d.ts files - if you ship the .ts files with the .d.ts files and the declaration maps generated by --declarationMaps, that is our current behavior (so that's pretty done). The second request in this thread, which is what we now track here, is an option to go to the output .js file (which could be useful if, for example, the .ts sources aren't shipped). Implementation-wise, that's just a matter of combining the declaration source maps and javascript source maps we already emit, however what we haven't decided on is how such a feature should be surfaced. Go to implementation in ts code already has a useful meaning for most code (which is go to definition, but prefer implementation/value declarations)... do we amend that in some way (say, if we resolve to a declaration file, try to resolve to a js file instead), or do we add a new endpoint? If the later, we need to talk about how we want to surface it with the vscode team.

@DanielRosenwasser honestly, what we really need here is just a more fleshed out description of exactly how we wanna expose a thing like this - the tools to do it are now in place.

weswigham avatar Aug 28 '19 18:08 weswigham

Coders from vscode team can propose ideas on how this should be implemented, eg Go to js implementation or something like that, a first good step would be to create a ticket on vscode issue tracker and cross-reference this tickets so that communication can begin.

silverqx avatar Aug 28 '19 19:08 silverqx

My understanding is that there are several related problems here. (1) is the big one, but we should keep the others in mind too:

  1. 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)
  2. Go To Type Definition doesn't work when the selection is itself a type. You'll get a "no type definition found" error.
  3. Go To Definition on a non-type identifier behaves inconsistently. Sometimes it will navigate to the implementation, sometimes it won't. In theory this is the same as (1), but it's also possible to solve (1) while leaving this inconsistency in place.

I think it would be simpler for users to solve all three problems without adding a third "Go To" menu option. Instead, my suggestion would be to retain only two menu items but make them behave more consistently:

  • Go To Definition should always navigate to the implementation, regardless of whether it's local code, a node_modules package with integrated .d.ts files, or a DefinitelyTyped definition that's separate from the JS package.
  • Go To Type Definition should always navigate to the TS declaration, regardless of whether the selection is a type or a non-type identifier.

Are there other common TS use-cases that require a third choice?

BTW, I guess we could rename "Go To Definition" to "Go To Implementation" if we wanted to make the distinction clearer, but IMHO this seems like an optional change. Consistency (regardless of the name) is the high-order bit.

justingrant avatar Aug 28 '19 20:08 justingrant

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.

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".

Go To Definition on a non-type identifier behaves inconsistently. Sometimes it will navigate to the implementation, sometimes it won't. In theory this is the same as (1), but it's also possible to solve (1) while leaving this inconsistency in place.

It goes to the first definition, where first is pretty arbitrary, and based on things like the order files were loaded in. The peek window is admittedly more useful, since it shows all definition sites. "Implementations" don't come into play.

weswigham avatar Aug 28 '19 21:08 weswigham

As a user, I will be satisfied, if it worked like this:

Ctrl+Click on a symbol (I mostly use only this to navigate the code) brings currently to either an implementation (if it is within the package being edited in VS Code) or to the declaration (if it is a package from the dependencies). The first does not require consideration. The second, when it goes to the declaration, I would like to Ctrl+Click on the declaration and it navigates me to the implementation, if it can locate, download and open it for me. Otherwise, it shows a message what problem prevented the sources to be found / opened. If it worked like this, it would be awesome.

I believe there are the following scenarios with locating the original source code:

  1. A package is written in typescript and original source code in external repository and either a) distribution includes d.ts and js files OR b) distribution includes d.ts, js and ts files
  2. A package is written in typescript and original source code in the current repository (mono repo case) and a) distribution includes d.ts and js files
  3. A package is written in javascript and original source code in external repository and either a) distribution includes it's own d.ts and js files OR a) distribution includes only js files and external @types definitions are used
  4. A package is written in javascript and original source code in the current repository (mono repo case) and a) distribution includes it's own d.ts and js files

1b) - is rarely used, and I do not like this approach as resources like bundlephobia and npm trends will report huge package sizes, which are not huge in runtime really, but people judge the size based on these resources. So, relying on the maintainers to include ts files with the distribution is not a good idea, but certainly if ts files are included, the case is solved.

1a) - (most?) frequent case. The package should some how declare (via package.json or source maps?) where the original sources were located when compiled (like microsoft pdb symbol files declare) OR where can be downloaded from, for example git+revision+path. VS code would navigate to the local file or git fetch it to the local cache and open it on navigation from declaration to the implementation

2a) - if it is possible to detect the case of mono repo automatically (using some heuristics), the original Ctrl+Click navigation could open the implementation directly. If it is impossible, fallback to 1a) is good enough

I care less about 3, but it would be nice if 3a) could open JS implementation on the second Ctrl+Click navigation. In case of 3b) the @types package should declare what JS packages it annotates, so VS code could have this information to navigate to the JS implementation in the external package.

I care even less about 4, but it could have the same behaviour as 2a) - fallback for 4a) is 3a)

Hope it helps.

avkonst avatar Aug 28 '19 21:08 avkonst

1a) - (most?) frequent case.

Yeah, that's what I'm saying we have all the tools in place to support now. We can combine .js.map and .d.ts.map files to produce a mapping from .d.ts to .js (without the intermediate .ts). But, hm. Really, you'd prefer to go to the declaration file, and then interact again to go to the associated js? You'd not want to jump straight to the js?

weswigham avatar Aug 28 '19 21:08 weswigham

"Really, you'd prefer to go to the declaration file, and then interact again to go to the associated js? You'd not want to jump straight to the js?"

In case of 1a), double jump or single jump are both fine for me. Although single jump can be considered as "more efficient", double jump has got the benefit of "reminding" the user that the second jump is a jump to the black box from the "library consumption" point of view (i.e. reminder that the source file being navigated to is not part of the current project). Maybe this behaviour could be made configurable.

In case of 3a), 3b) or 4, I certainly want double jump. I first want to see the declarations and only after this (in very rare cases, like debugging) the implementation. Jumping straight to JS skipping the TS declaration removes all the benefits of the declarations. So, I definitely want to see the TS declaration first.

avkonst avatar Aug 28 '19 21:08 avkonst

(combining replies to @weswigham and @avkonst from multiple posts)

It goes to the first definition, where first is pretty arbitrary, and based on things like the order files were loaded in.

In the most common case where you want to go to the implementation for a particular imported symbol, why would there be multiple definitions? Is this a common situation?

produce a mapping from .d.ts to .js (without the intermediate .ts).

Not sure I understand "without the intermediate TS". Do you mean that even if the original .ts source is available, VSCode would navigate to the transpiled .js? That seems like a bad idea. If the original source is available, then VSCode should navigate there-- just like it would when the debugger steps into transpiled code that has a sourcemap. That said, if the package author was evil or lazy and didn't include the original TS source in their npm distro, then it seems reasonable for VSCode to fall back to the transpiled JS and perhaps show a warning message explaining why the user is seeing such weird, unreadable code and to encourage users to nag the package author to include the original source in their package.

Really, you'd prefer to go to the declaration file, and then interact again to go to the associated js? You'd not want to jump straight to the js?

I don't think a two-step would add value. I think it's much better to have distinct, consistent UX: use "Go To Type Definition" to navigate to the type definition, and use Go To Definition (or Go To Implementation-- whatever we choose to call it) to go to the original source code or, per above, to the transpiled JS if original source isn't available.

IMHO, it's much more confusing to have different behavior depending on whether the selected symbol is defined in your own code vs. in node_modules. It shouldn't matter. If I want to see the type, VSCode should take me to the type. If I want to see the implementation code, VSCode should take me there. This is especially true because, in large organizations, what's "my code" and what's "library code" isn't always a bright line. If someone else refactored code into a shared internal package, I shouldn't have to change how I navigate to that code.

I first want to see the declarations and only after this (in very rare cases, like debugging) the implementation.

Seems like it's clearer to require the developer to explicitly choose (by picking one menu item or another) whether they want to see a type declaration or to view the implementation code. Having navigation be inconsistent depending on where the code comes from seems unnecessarily confusing.

> Go To Type Definition doesn't work when the selection is itself a type. I think y'all should open a new issue on that one. That's just a failure of exepections/bug, imo.

Yep, this could definitely be fixed first. I included it here because if my preferred solution ("Go to Definition always navigates to the implementation") is adopted, then users will get in the habit of using Go To Type Definition whenever they want to see the .d.ts, so it'd be weird if doing this on a type didn't act consistently to how it acts for a non-type identifier.

justingrant avatar Aug 28 '19 22:08 justingrant

"It goes to the first definition, where first is pretty arbitrary, and based on things like the order files were loaded in."

I have not mentioned in my replies that it should navigate to the first definition. I said on Ctrl+Click on a symbol it should go first of all to the definition (as it works now) and only on the subsequent Ctrl+Click to the implementation.

avkonst avatar Aug 28 '19 22:08 avkonst

"Seems like it's clearer to require the developer to explicitly choose (by picking one menu item or another) whether they want to see a type declaration or to view the implementation code."

Separate context menus are fine and good idea to have. They are not mutually exclusive to the Ctrl+Click behaviour.

avkonst avatar Aug 28 '19 22:08 avkonst

Wonder why this critical bug is not being worked on yet. It is just becoming a more and more pain in the ass as typescript adoption goes up.

I don't want to hear technical explanations, I want to know why this issue isn't even acknowledged after so much description on different users.

zjaml avatar Sep 09 '19 23:09 zjaml

@zjaml are you wanting it to go to TypeScript source files or JavaScript source files? TypeScript source files are supported. It's JavaScript source files that this issue is currently still open for.

haugerbr avatar Sep 30 '19 18:09 haugerbr

Tangentially related; I have a monorepo with a common package that's published to npm. Because of this public interface the dist is js + d.ts files. When I'm working in the repo VSCode always jumps to the d.ts instead of the source. Do you know a way around this?

0x80 avatar Sep 30 '19 18:09 0x80

@0x80 I believe this issue is still open for the scenario you just described. The only way I think you could get around this is if the project is open source and you compiled it from the typescript source. Then you could turn on the --declarationMaps flag for the typescript compiler and IDEs that support the declaration maps will go to the typescript source instead of the d.ts files.

haugerbr avatar Sep 30 '19 18:09 haugerbr