microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.75k stars 12.46k forks source link

cmd+click function name goes to type definition file instead of source #36418

Open tgreen7 opened 4 years ago

tgreen7 commented 4 years ago

duplicate of microsoft/vscode#68782 but that was closed without fix.

Steps to Reproduce:

  1. Create a project folder with 2 files:

lib.js

export const testMethod = (name, params) => {
  params && params.age
    ? `hello ${name} with age: ${params.age}`
    : `hello ${name}`;
};

test.js

import { testMethod } from "./lib";
testMethod("foo", { age: 3 });

cmd+click works and jumps to definition.

no-type

now add a type definition file

lib.d.ts

declare namespace MyModule {
  interface IParams {
    age: number;
  }

  function testMethod(name: string, params: IParams): string;
}
export = MyModule;

cmd+click jumps to type definition instead of source definition.

with-type-file

I do not think this should be the default behavior. I would expect cmd+click to bring me to the function definition.

mjbvz commented 4 years ago

This is the designed behavior for TS files at least.

Hand authoring d.ts files for js files that you already have the code for is sort of an odd case. For TS projects with generated definitions, the solution is to use declaration maps so tools can map from the type definitions back to the implementation

tnrich commented 4 years ago

@mjbvz

Hand authoring d.ts files for js files that you already have the code for is sort of an odd case.

I would argue that this is actually a very frequent case, think of all the pure js libraries that have d.ts files. IMO there should be a way to jump to either the definition file or completely disregard the definition file and jump to the source code.

RyanCavanaugh commented 4 years ago

From an architectural perspective, this is a bit of a jump for us. If there's a .d.ts file, we don't even look for .js and wouldn't realize that there was another option to even go to. If we did decide this was worth investing in based on feedback, there'd need to be a separate verb so that we didn't spend a bunch of timing trying to load JS files the user wasn't interested in.

tnrich commented 4 years ago

I think that approach makes sense @RyanCavanaugh ! Definitely wouldn't want to bog down users who were uninterested in this feature.

That being said I bet there will be quite a bit of interest from users who want this feature.

Thanks for the reply!

ayqy commented 4 years ago

IMO, there are all of abstract interfaces defined in declaration files, and we should not care implementations most of the time. But we will be frustrated sometimes if we want to inspect or suspect something under the hood. Haven't you guys had the same frustrating experience and then tried searching from all project files ?

So, from a VS Code user's view, I really hope there is an option for this purpose.

P.S. declarationMap maybe a ideal solution at this point, since the lack of map files in JavaScript ecosystem, such as DefinitelyTyped