21

Since typescript doesn't seem to support absolute path references, I can't see how to keep my references tidy. I've got ts files at many different locations in my folder structure, and having to be really careful about whether I mean ..\Scripts\typings\jquery\jquery.d.ts or ..\..\Scripts\typings\jquery\jquery.d.ts seems really kludgy.

Is there any way to specify a root references folder, so that I don't have to specify all paths relative to the current file path, which is different for every folder?

4
  • A path like <reference path="c:/users/josh/node-samples/typings/jquery/jquery.d.ts" /> should work. The issue you linked to shows as fixed (and I confirmed it works). Commented Nov 8, 2014 at 22:50
  • 1
    Hmm, that's better than sloppy references, but hardcoding the directory structure means that the location in my file system is embedded in the files, which will break on other machines. Surely Typescript should support root-relative paths, i.e. /Scripts/typings/... Commented Nov 8, 2014 at 23:25
  • Unfortunately, it doesn't support such a thing as each file could be compiled independently into a JavaScript file, so there isn't a well defined location for a root path. The team is very good about listening to feedback on github. Commented Nov 9, 2014 at 4:03
  • Why does it matter how it's compiled, since these references are for design time only? Commented Nov 9, 2014 at 13:27

2 Answers 2

18

There is not currently a way to specify a root folder to use within references.

Absolute file paths do work, but maintenance of the paths generally speaking with multiple developers makes this likely a non-starter for many TypeScript development projects.

There have been discussions on CodePlex for example that expressed a similar request (but without a resolution). As TypeScript files are stand-alone, some have been concerned about introducing a "project" like scheme to the compiler.

Some developers will put the most commonly needed references in a single file (called for example, _references.d.ts) and list references to the definition files there. Then, that file will be referenced from other TypeScript files. It simplifies, but does not completely eliminate the problem (as you still will need to use relative file references with N levels of directory popping potentially):

/// <references path="../../../_references.d.ts." />

Depending on how many files you have and the size of the definitions though, you may find that as files are individually compiled that the compile process will take longer (as it pulls in potentially unused definitions from the _references.d.ts file). (If you have for example, "compile on save" activated within an IDE).

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

5 Comments

I'm not sure how Typescript in general should work, but it seems to me that Visual Studio could overlay tooling to recognize absolute paths, maybe using the common ~/ ASP.NET convention. Any reason not to do that?
The TypeScript compiler ships separately ... with the right adjustments to the TSC compiler, yes, a ~/defs/etc.d.ts could be made to work.
In Visual Studio, if you download .d.ts files through nuget at least, you don't need to include the references. This is great, but it locks you into using only one IDE.
having such convoluted reference system makes TS projects are non starter for everyone. I mean, they already can build a graph, why do they need those stupid illogical references.
@GuardianX - it's hardly a non-starter for "everyone." I hardly use references anymore at all. In fact, most of my code doesn't use them as I've moved to TypeScript 2.0. Using import should suffice assuming you've got the declarations available.
2

In order to keep your relative path references tidy, specify path alias(es) in your tsconfig.json

First of all install tspath an npm tool for resolving ts paths NOTE: tspath requires a very recent versison of node!

npm install -g tspath

Next, add path aliases to tsconfig.json

"baseUrl": "./",
  "paths": {
    "@Scripts/*": ["./Scripts/typings/jquery/*"],
    "@Plugins/*": ["./MyPlugins/V2/*"]
  }

Now use your alias when referencing your scripts

@Scripts/jquery
@Plugins/myPlugin

After you have run the TypeScript compiler, from your project directory, run:

tspath

or

tspath -f

to skip the prompt!

Read more at: https://www.npmjs.com/package/tspath

Hope this is what you asked for!

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.