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?
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.
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.
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.
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.
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).
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With