2

I would like VSCode intellisense to understand the semantics of my TS import statements. If it did, then VSCode could try to resolve those statements to files and pull types from those files without any annotations. Sometimes it can, if it can find the typescript file that exports what I'm importing. However, there are times when it can't do that, specifically, when I'm importing a library that has been typed with an ambient declaration. But almost everything I want to import is typed with an ambient declaration, so this is a big deal.

I understand that this is complicated by the fact that import statements compile to different things depending on the module flag, and that generated import statements can have an additional layer of runtime routing that confuses a simple file-based search strategy.

In a perfect world, since we're dreaming, I would like to be able to reference a requirejs config object, defined as a standalone json file or literally defined in tsconfig.json, and have VS Code use requirejs routing semantics to resolve the imported moduleid to a file on disk (or else warning me at compile time when my import statement won't resolve to a module at runtime).

However, even being able to just have intellisense search all of the paths in a list to heuristically find the module that I'm importing would be nice, and that would be module-loader agnostic. With a routing layer like requirejs imposed over top, it would be possible in theory to resolve to a different module than the one that TS finds; in most cases it would work, but the oddballs could be handled with an annotation.


I don't think either of these things is currently possible in any editor, including VSCode. Quite possibly they'll be there in the future, but for now, I'm curious how other people are getting intellisense. I'm willing to go to extreme lengths to avoid the <reference path=.../> annotations, since they are code duplication.

Some of my libraries are typed with DefinitelyTyped packages, but some of them are not, e.g., some of them are internal modules that have been browserified. So tsd is not a full solution.

One level of "solution" is to simply make all the types of the project available in every file, e.g. by coalescing every type declaration from the entire project, including ambient modules from .d.ts files, and then referencing that agglomeration, so long as two ambient modules don't have a namespace collision. This solution works until there is a namespace collision, and then it permanently stops working. So I don't really like this idea.

Is there anything else better? Has anyone gotten tsserver to work? I can find scant little documentation about it - I only know it exists because I noticed that it was installed when I installed node's tsc implementation.

2
  • VSCode does understand ES6 import statements, and intellisense works as you would expect. If you create a tsconfig.json in your project, VSCode will include all .ts files in that directory and sub directories by default. Beyond that, I'm not sure what you're asking for. Commented Jul 31, 2015 at 2:45
  • It does sometimes understand them, but fails in the cases I've mentioned. Commented Jul 31, 2015 at 17:20

2 Answers 2

2

Has anyone gotten tsserver to work

Its for Editors. The customization you are seeking needs to go in the compiler not TSServer.

AND it is on the plans

See this issue : https://github.com/Microsoft/TypeScript/issues/2338

2 things will happen:

  • TypeScript will be able to resolve to a .d.ts files like require resolves to a .js file.

  • You will be able to configure this resolution using path mappings which will be superset of requirejs + systemjs path mappings.

Sign up to request clarification or add additional context in comments.

2 Comments

Still however interested in how people are doing things in the meantime.
BTW, I have a grunt task compiling & watching the typescript. The only thing I can't get quite right is intellisense (in my editor), which is why I was interested in tsserver.
2

All of my concerns were subsequently addressed by the TypeScript team. They have provided extensions to tsconfig.json which allow us to declare module resolution and mimic the resolution strategy of all the major module systems (requirejs, webpack, browserify).

It's all here https://www.typescriptlang.org/docs/handbook/module-resolution.html.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.