Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reference typescript files without absolute paths?

Tags:

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?

like image 782
Joshua Frank Avatar asked Nov 08 '14 22:11

Joshua Frank


People also ask

Why is it better to use relative paths instead of absolute paths?

Relative links show the path to the file or refer to the file itself. A relative URL is useful within a site to transfer a user from point to point within the same domain. Absolute links are good when you want to send the user to a page that is outside of your server.

How do you reference a relative path?

Relative path Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy. A single dot represents the current directory itself.

How do you convert an absolute path to a relative path?

The absolutePath function works by beginning at the starting folder and moving up one level for each "../" in the relative path. Then it concatenates the changed starting folder with the relative path to produce the equivalent absolute path.

Why do we use absolute paths?

The absolute path of a file allows you to specify the exact location of the file, regardless of the location of the user's current directory. Links are fundamental elements of the World Wide Web, especially when they are presented as absolute paths in computer terms.


2 Answers

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

like image 137
WiredPrairie Avatar answered Oct 18 '22 18:10

WiredPrairie


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!

like image 20
Patrik Forsberg Avatar answered Oct 18 '22 18:10

Patrik Forsberg