Enable Go to Implementation in Typescript Editors
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
Hi @andrewbranch - thanks for jumping in on this important issue. After following it for a few years, I'll add context that may help.
- My understanding is that Go To Implementation currently works, but only for packages that publish their .d.ts, sourcemaps, declaration maps, and original sources to npm. Unless I'm mistaken, packages that are compiled with TS (an increasing number of popular packages!) can make Go To Implementation work today.
- Caveat: TSC may emit declaration maps that work, but popular transpilers and bundlers (rollup, esbuild, babel, etc.) may not.
rollup-plugin-tshas problems, and I suspect others do too. If the TS team could help those maintainers fix their support, the ecosystem would be hugely grateful!
- Caveat: TSC may emit declaration maps that work, but popular transpilers and bundlers (rollup, esbuild, babel, etc.) may not.
- Increasingly, maintainers are packaging original sources, because original sources (or inline sourcemaps) are required for easier debugging in browser dev tools and IDEs like VSCode.
- This is especially true for the increasing number of popular packages that are being ported to TS.
- I've pushed PRs to fix sourcemaps and ship original source for 10+ packages. If my memory is correct, only once has a maintainer refused to ship source. The others were happy to ship source because it helped their users debug their package more easily.
- There are ecosystem changes coming (e.g. to create-react-app, perhaps later to
npm publish) that may accelerate the trend of shipping original source along with sourcemaps.
- Transpiled JS is often hard to understand, esp. for async/await or other ES6+ features that don't transpile cleanly to ES5. For this reason, the VSCode debugger's Step Into (the debugger counterpart of Go To Implementation) always only shows transpiled JS if the original source is unavailable. Seems like Go To Implementation should behave the same way, both for consistency across different ways of "navigating" and also because many users are confused by transpiled source.
- You're 💯 correct that DT won't work with declaration maps because of version mismatches. My fault for not thinking this through before posting.
- I'm seeing many package maintainers bundling their .d.ts with their packages to avoid versioning mismatches, or because they're porting their source to TS. There's still a lot of reliance on DT, so this trend doesn't invalidate the need for a DT-friendly solution. But it also suggests that the non-DT (bundled declarations) case is important too and will get more important over time.
For all the reasons above, I suspect that the right solution is two-tracked:
-
For packages built with TS, packaging declaration maps (along with sourcemaps, .d.ts, and original sources) seems like a good solution, because it can provide granular, precise navigation that I'm not sure is possible with an exports-only solution. I assume it'd provide the best perf too. Also it actually may work today! But...
- We're lacking guidance for package maintainers about how to make declaration maps, sourcemaps, etc. work correctly. IMO, what's needed is general ""how to build a library in TS" docs. Here's a possible outline for this content.
- As noted above, non-TSC bundlers and transpilers probably need help with emitting declaration maps properly because today those maps can be broken by code splitting, tree shaking, etc.
- Consider defaulting
declarationMapto betrueif thesourceMaporinlineSourceMapoption istrue - For packages with inline sourcemaps, can we use the same code that the JS debugger extension uses to show "sources" using inline sourcemaps?
-
For packages that aren't built with TS (or where the declaration map => sourcemap => original source chain is broken), there should be a fallback solution. For example assume the following code:
// mode_modules/some-package/src/Foo.js
import doSomething from 'do-something';
class Foo {
static bar(x) {
doSomething(x);
}
}
export default Foo;
// mayapp.ts
import Foo from 'some-package';
const x = Foo.bar(42);
I assume that Go To Implementation for import('some-package').Foo.bar should behave as follows:
- If the location of
barcan be resolved, e.g. via sourcemaps or some TS JIT magic, then navigate tostatic bar(x) { - If all we know is imports, then navigate to
class Foo {(I'm assuming that the same magic that the IDE uses to make Go To Definition work for regular JS files will also work as long asFoois declared at global scope.) - If all we know is the file (e.g. if package.json has
exports) but the exported symbol cant be resolved in that file, then navigate to the first line of Foo.js - If we don't even know the file then navigate to
mainormodule.
The bullet-points above are pretty hand-wavey because I don't know enough about TS and Node's module-resolution behavior. But I assume TS and/or Node-resolution experts will know what's possible!
@andrewbranch what do you think? You likely understand this stuff better than I do, so please correct me where I'm wrong above.
Why navigating to transpiled source is bad
- I think more people would be just as happy or happier to be able to go to the
.jsand leave the.tsout of their node_modules.
Are you sure about this? Have you heard this sentiment from median-skill developers? I'm skeptical, because transpiled code can be really hard to understand for non-advanced developers.
For example, here's an example of the code I saw in VSCode when I first debugged into a method of AWS Amplify, Amazon's JS front-end library that wraps AWS services:
onSuccess: function (session) { return __awaiter(_this, void 0, void 0, function () {
var cred, e_1, currentUser, e_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
logger.debug(session);
delete user['challengeName'];
delete user['challengeParam'];
_a.label = 1;
case 1:
_a.trys.push([1, 4, 5, 9]);
return [4 /*yield*/, this.Credentials.clear()];
case 2:
_a.sent();
return [4 /*yield*/, this.Credentials.set(session, 'session')];
case 3:
cred = _a.sent();
logger.debug('succeed to get cognito credentials', cred);
return [3 /*break*/, 9];
case 4:
e_1 = _a.sent();
logger.debug('cannot get cognito credentials', e_1);
return [3 /*break*/, 9];
case 5:
_a.trys.push([5, 7, , 8]);
return [4 /*yield*/, this.currentUserPoolUser()];
case 6:
currentUser = _a.sent();
that.user = currentUser;
dispatchAuthEvent('signIn', currentUser, "A user " + user.getUsername() + " has been signed in");
resolve(currentUser);
return [3 /*break*/, 8];
case 7:
e_2 = _a.sent();
logger.error('Failed to get the signed in user', e_2);
reject(e_2);
return [3 /*break*/, 8];
case 8: return [7 /*endfinally*/];
case 9: return [2 /*return*/];
}
});
}); },
Eek! I assume that most JS developers won't easily understand happening in the state machine above. But after https://github.com/aws-amplify/amplify-js/pull/2680 was merged, debugging into the same method showed the original source, which is much easier to understand:
onSuccess: async session => {
logger.debug(session);
delete user['challengeName'];
delete user['challengeParam'];
try {
await this.Credentials.clear();
const cred = await this.Credentials.set(session, 'session');
logger.debug('succeed to get cognito credentials', cred);
} catch (e) {
logger.debug('cannot get cognito credentials', e);
} finally {
try {
// In order to get user attributes and MFA methods
// We need to trigger currentUserPoolUser again
const currentUser = await this.currentUserPoolUser();
that.user = currentUser;
dispatchAuthEvent(
'signIn',
currentUser,
`A user ${user.getUsername()} has been signed in`
);
resolve(currentUser);
} catch (e) {
logger.error('Failed to get the signed in user', e);
reject(e);
}
}
},
I agree that there are some cases where sophisticated users may want to see the transpiled JS. But those are unusual cases. The normal case (and the only case that many unsophisticated developers can handle) is to show the original source, for the same reasons that we show original source in the debugger.
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.
I think you are correct here. It doesn't have to be perfect to add a lot of value.
Thanks for the detailed explanation, and all good points. I don’t doubt that publishing TS sources to npm for debugging purposes is becoming more common. Likewise, as you also noted, we’re seeing a slow but steady trend of packages moving away from DefinitelyTyped to ship their own types (presumably most of these are autogenerated from TS sources, but some are hand-authored or generated from a tool other than tsc). However, I feel confident that the majority of people’s node_modules today lacks TS implementation files, so any solution that does not include an attempt to resolve symbols to JS, at least when no other option is present, I think will be highly unsatisfying and confusing.
Are you sure about this? Have you heard this sentiment from median-skill developers? I'm skeptical, because transpiled code can be really hard to understand for non-advanced developers.
Nope! This was a total guess based on past experience with JS-only VS Code users who get confused (and sometimes angry 😅) when they get dropped into something that looks TypeScripty. I think there are users who would rather get dropped into the JS, but at this point I don’t have evidence to say they make up a particular ratio of all users. But, I don’t think it really matters—if we have all the pieces in place to jump to the exact right place in a .ts file, we can be nearly 100% sure that result is correct and we can calculate it very fast, so I think we should always do that. The point I should have been making was, again, that we need to be able to make a guess at the JS when the TS doesn’t exist.
I guess my thought process was, if I need to look at implementation files in node_modules, something has already gone horribly, horribly wrong, and I’m bracing for a bad time. I hopefully don’t need to fully understand generator downleveling in order to find a line to put a breakpoint on. But I’m sure my standards here are too low for long-term thinking 😄. It seems like you’re doing a very good job of thinking about how to improve things for the long game, which I very much appreciate. I was admittedly more focused on how to get acceptable results now. Point well taken that we should be thinking about both. It’s probably worth at least doing some writing about the benefits of publishing declaration maps and sources with your packages.
The bullet-points above are pretty hand-wavey because I don't know enough about TS and Node's module-resolution behavior. But I assume TS and/or Node-resolution experts will know what's possible!
I actually don’t think we know what’s possible without some experiments. My suspicion is that exports will usually be easy, and drilling down into them further will be hit-or-miss, but I don’t know.
Cool, sounds good. A few responses inline. So glad you're looking into this!
Nope! This was a total guess based on past experience with JS-only VS Code users who get confused (and sometimes angry 😅) when they get dropped into something that looks TypeScripty.
Yep. Those users have a difficult tradeoff: hard-to-read transpiled code or also-hard-to-read (for them) TS code. But this is exactly the same problem they have in the debugger today, and debugging into node_modules is much more common than using Go To Implementation for node_modules.
For this reason, I'd recommend thinking of these problems separately.
- How can Go To Implementation show original source (from source/declaration maps) with a fallback to transpiled JS if the mapping doesn't work? The default should be "original source" because that's consistent with how (more common) debugger use cases behave.
- How to easily flip from transpiled source to original source (and flip back too) in the IDE? This would affect the most common case (the debugger) as well as less common cases like Go To Implementation. FWIW, when debugging I frequently have cases where I want to view transpiled source (e.g. when I suspect the sourcemap is out-of-date after a hot reload) so this feature would be useful beyond TS haters. The UI could be a right-click command, or even a button in the upper-right-corner toolbar like the button used to navigate from a diff view to the underlying file.
I think keeping the problems separate will make it easier to make progress on both in parallel without getting stuck in arguments about whether to show TS or JS for Go To Implementation.
Question: do declaration maps that TSC emits point to transpiled code or to original source? If the former, then they'll work just like the debugger. 👍 But if the latter, then it seems like it'd be hard to find the transpiled JS. I guess TS could load the entire sourcemap of the package and see if the file/row/col that was resolved by the declaration map also shows up in a sourcemap file, and trace it back from there? Anyway, it'd be good to know whether declaration maps point to transpiled or original source.
if we have all the pieces in place to jump to the exact right place in a
.tsfile, we can be nearly 100% sure that result is correct and we can calculate it very fast, so I think we should always do that. The point I should have been making was, again, that we need to be able to make a guess at the JS when the TS doesn’t exist.
👍 Yep! I come back to aligning with the (more commonly used for node_modules navigation) debugger behavior: if there's original source, show that. If not, then try to fall back to something reasonable.
if I need to look at implementation files in node_modules, something has already gone horribly, horribly wrong, and I’m bracing for a bad time.
Many years ago, I was working with the VS team back when the original "Just My Code" feature was being built. There was a big debate between the C# and (especially) the C++ team whose users had a long history of viewing, debugging, and copying libraries like MFC vs. the VB team whose users were confused by library code, esp. because libraries were written in C# and C++! What's nice is that both preferences have been improved. For library-code-haters, Just My Code continues to exist and be enhanced. For library-code-lovers, it's gotten easier to view, debug into, and modify library code.
But the fact that 15 years later this debate hasn't gone away is a signal that this is a persistent difference between developers. For some devs, viewing library code is an awful, exceptional case. But for many other users, debugging, viewing, and even editing/PR-ing library code is a normal course of business, because libraries are often buggy, poorly documented, or need to be extended in some way. You see this parallel in other walks of life too. For example, if a light switch isn't working, some folks will grab some tools while others will grab a phone and call an electrician.
So I think it's helpful to assume that for many developers (but not all!) they will read, debug into, fix, and borrow library code frequently, so we should try to make it work as well as possible.
For Just My Code fans, they won't be clicking Go To Implementation anyways (because their preference is to avoid library code!) so I think this feature is really targeted only at the anti-JMC crowd.
exports will usually be easy, and drilling down into them further will be hit-or-miss, but I don’t know.
Yep, I think the same.
It’s probably worth at least doing some writing about the benefits of publishing declaration maps and sources with your packages.
What are next steps to get more attention on this? In https://github.com/microsoft/TypeScript/issues/16792#issuecomment-892068076 I wrote a possible outline for this content. I'm happy to help contribute but honestly I feel unqualified to write it because I don't understand enough about the problem space. But I'd love to pair with someone to help make this content happen. LMK how I could help.
A good place to start might be to simply create a "Writing libraries in TypeScript" section in the docs and put some minimal content there. Then that section in the docs could be a honeypot to attract tips and tricks from various TS library authors via PRs. In addition to sourcemaps/decl-maps content, there could be sections about how to configure various bundlers, how to migrate from DT to bundling types, and other topics that apply to library authors.
So one thing I haven't seen mentioned, though I may have missed it in this long thread, I miss seeing the actual code comments, function & parameter descriptions. Sure types are great, but knowing the package authors intention for each function is almost more valuable.
date-fns for example doesn't tell you in their .d.ts files which date is supposed to come after the other for functions like isAfter. The answer is clear in the functions actual Docblocks. And digging into a large node_modules folder to find that one js file is tedious when you need to do it a dozen times.
What VSCode gives me:

What the package authors intended me to see:

@danieliser totally agree—however, if we get a solution to this at all, it’s likely to be a “best guess” effort to find the corresponding JS from the typings. I’m ok with providing a go-to function for that because it’s better than nothing, but I don’t think we’ll achieve high enough confidence (or satisfactory performance) in that guess in order to pull in the JSDoc. This seems like a case where the answer is just to get the package authors to copy the JSDoc comments to the typings (or copy it in a DT PR). If the typings are generated from TS source, the comments will be copied to the typings automatically. If they’re missing, it means someone’s manual process or third-party tooling is insufficient and the problem should be addressed there.
Please note the milestone change means I plan to investigate solutions to this during the 4.7 timeframe. I don’t think we have a clear path yet, so it doesn’t necessarily mean that something will ship in 4.7.
I'd like to add a use case: some of our files are authored as .js files (with JSDoc style typing) with explicit/manual .d.ts files for them to express things that cannot be expressed otherwise in JSDoc (e.g. circular type definitions, function overloads, interface, etc.)
The problem is that with such files, it's impossible to "go to definition" / "go to implementation" as VS Code and other editors stop at the .d.ts file. declarationMap isn't an option since the .d.ts file is manually authored.
I have a prototype up at #48264 that I’m welcoming feedback on. I’m 100% sure it doesn’t cover everything but if folks are willing to give it a try, it would be helpful to gather some test cases that don’t work yet so I can see if there’s a good way forward on them.
To try it: There’s a build from @typescript-bot on the PR (copied below) that you can npm install, then ensure your VS Code window is using that build of TypeScript by opening the command palette (in a TS or JS file) and running TypeScript: Select TypeScript Version. You should see the one newly in your node_modules listed as version 4.7.0. At that point, your Go To Definition command will be replaced with the new logic that finds more JS files and ignores .d.ts files. 🚨 This is not the final experience! We are not replacing Go To Definition like this! We don’t know for sure yet where the new logic will be exposed: it might be in a new command, or its results might be merged into Go To Definition or Go To Implementation. There are technical and UX reasons why the latter might not be desirable, but if it’s in a new command, none of you would be able to test it right now without also running a custom build of VS Code. So this build temporarily replaces the existing go-to-def implementation with the new command so you can easily test it.
One specific thing to keep an eye out for in testing: There are some cases where we are quite confident in the results we return, other cases where the best we can do is jumping to the top of a file, and then other cases where we’re confident about the file but we make a number of guesses within the file. Under the hood, I am indicating whether or not the result is a guess, but this information isn’t currently exposed in VS Code. If we go forward with presenting guesses like this, it will almost certainly go along with some kind of visual indicator noting that it’s a guess. Here’s an example on import { add } from "lodash":
Two of these guesses are right, while the top one is wrong. If you notice more places containing wrong guesses like this, please note that and tell me how to repro them on the PR. I need a bit of crowdsourcing to determine whether the guesses are worth doing at all and if there’s an easy way to improve them.
Thanks all!
Hey @andrewbranch, I've packed this into an installable tgz. You can install it for testing by referencing it in your
package.jsonlike so:{ "devDependencies": { "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/122072/artifacts?artifactName=tgz&fileId=F1B646030001FF6586C865D86A3B212F194CAFCD7C9C74E8E24C4BEC6A44B7E502&fileName=/typescript-4.7.0-insiders.20220316.tgz" } }and then running
npm install.
The plan is to add an experimental new command in VS Code to trigger the behavior added in #48264. I’ll keep this thread updated to let you know when you can try it out in VS Code Insiders + typescript@next.
Great progress here. Thanks @andrewbranch for your perseverance in making this happen! Can't wait to try it out.
I’ll keep this thread updated to let you know when you can try it out in VS Code Insiders + typescript@next.
I forgot 🤦♂️
You can try this in VS Code Insiders and typescript@next! And pretty soon it will be in the stable versions. I started an issue to gather feedback: #49003. Thanks!
Already in the newest release: Visual Studio Code April 2022
@andrewbranch Thank you thank you thank you for making this happen! You just made my life so much easier