typescript 如何在没有绝对路径的情况下引用打字稿文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26822836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:56:31  来源:igfitidea点击:

How can I reference typescript files without absolute paths?

visual-studiotypescript

提问by Joshua Frank

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.tsor ..\..\Scripts\typings\jquery\jquery.d.tsseems really kludgy.

由于打字稿似乎不支持绝对路径引用,我不知道如何保持我的引用整洁。我的文件夹结构中的许多不同位置都有 ts 文件,并且必须非常小心我的意思..\Scripts\typings\jquery\jquery.d.ts..\..\Scripts\typings\jquery\jquery.d.ts看起来是否真的很笨拙。

Is there anyway 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?

没有办法指定根引用文件夹,这样我就不必指定相对于当前文件路径的所有路径,每个文件夹都不同?

采纳答案by WiredPrairie

There is not currently a way to specify a rootfolder 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.

绝对文件路径确实有效,但通常与多个开发人员交谈的路径维护使得这对于许多 TypeScript 开发项目来说可能是一个非启动项。

There have been discussionson 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.

例如,已经有关于 CodePlex 的讨论表达了类似的请求(但没有解决方案)。由于 TypeScript 文件是独立的,因此有些人担心向编译器引入类似“项目”的方案。

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.d.ts)并在那里列出对定义文件的引用。然后,该文件将从其他 TypeScript 文件中引用。它简化了,但并没有完全消除问题(因为您仍然需要使用具有 N 级目录弹出的相对文件引用):

/// <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.tsfile). (If you have for example, "compile on save" activated within an IDE).

但是,根据您拥有的文件数量和定义的大小,您可能会发现,由于文件是单独编译的,编译过程将花费更长的时间(因为它会从_references.d.ts文件中提取可能未使用的定义)。(例如,如果您在 IDE 中激活了“保存时编译”)。

回答by Patrik Forsberg

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

为了保持相对路径引用的整洁,请在 tsconfig.json 中指定路径别名

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

首先安装tspath一个用于解析 ts 路径的 npm 工具注意:tspath 需要最新版本的节点!

npm install -g tspath

Next, add path aliases to tsconfig.json

接下来,将路径别名添加到 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:

运行 TypeScript 编译器后,从项目目录运行:

tspath

or

或者

tspath -f

to skip the prompt!

跳过提示!

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

阅读更多信息:https: //www.npmjs.com/package/tspath

Hope this is what you asked for!

希望这是你所要求的!