Javascript 我是否必须在每个文件中引用打字稿定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28989399/
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-08-23 02:42:30  来源:igfitidea点击:

Do I have to reference typescript definition in every file

javascripttypescript

提问by HotStuff68

Is there a way to tell typescript to use a certain file (or set of files) as definition for everything compiled?

有没有办法告诉打字稿使用某个文件(或一组文件)作为编译的所有内容的定义?

My only alternative currently is to add something like this in every single typescript file (which seems clunky):

我目前唯一的选择是在每个打字稿文件中添加这样的东西(这看起来很笨重):

/// <reference path="DefinitelyTyped/requirejs/require.d.ts" />

采纳答案by Dynalon

When using TypeScript's internal module system, you can avoid having any <reference>tags at all in the code. I personally do this because I don't want to encode paths (realtive or absolute) within the code as I constantly move stuff around.

使用 TypeScript 的内部模块系统时,您可以完全避免在<reference>代码中包含任何标签。我个人这样做是因为我不想在代码中编码路径(真实的或绝对的),因为我经常移动东西。

One way to do this is by making sure all required declaration files and TypeScript source files are passed to the compiler as arguments during compile time.

一种方法是确保所有必需的声明文件和 TypeScript 源文件在编译时作为参数传递给编译器。

Using gulptogether with gulp-typescriptsimplifies this task. You can set noExternalResolvein gulp-typescriptto true, and create gulp tasks that take all your .d.ts files along with your sources and pipe it down to the compiler. When you pull in tsdinto your stack, you only need to pass the tsd.d.tsfile that contains references to all other definition files installed via tsd.

gulpgulp-typescript一起使用可以简化此任务。您可以将noExternalResolvein设置gulp-typescript为 true,然后创建 gulp 任务,将所有 .d.ts 文件与源代码一起传送到编译器。当您将tsd拉入堆栈时,您只需要传递tsd.d.ts包含对通过tsd.

UPDATE for TypeScript >= v1.5: you can use a tsconfig.jsonfile, and the compiler will get the ordering of the classes right. This removes the need to use gulp-typescriptalltogether. You can either chose to have all files explicitly listed in the tsconfig.jsonfile, or completely leave out the filesproperty to include all *.ts/*.tsxfiles within the directory the tsconfig.jsonresides (including all subfolders).

TypeScript >= v1.5 的更新:您可以使用tsconfig.json文件,编译器将正确排序类。这消除了使用gulp-typescriptalltogether的需要。您可以选择在文件中明确列出所有文件tsconfig.json,也可以完全忽略该files属性以包含驻留*.ts/*.tsx目录中的tsconfig.json所有文件(包括所有子文件夹)。

A sample tsconfig.jsonmay look like:

示例tsconfig.json可能如下所示:

{
    "compilerOptions": {
        "target": "ES5",
        "module": "commonjs",
        "lib": [ "es5", "es2015.promise", "dom" ]
    },
    "include": [
        "src/**/*.ts"
    ]
}

回答by juFo

What I've learned so far is that /// < reference >-ing a modulewith reference comments is not a good method.

到目前为止,我学到的是/// < reference >-ing带有参考注释的模块不是一个好方法。

For example: in case you have a file Fooand a file Bar. Both files use jquery, but only file Foohas a reference comment to jquery. If file Foois deleted for some reason, your file Baris broken, because the reference is missing.

例如:如果您有一个文件Foo和一个文件Bar。两个文件都使用 jquery,但只有文件Foo有对 jquery 的引用注释。如果文件Foo由于某种原因被删除,则您的文件Bar已损坏,因为缺少引用。

If you are using TypeScript >= 2.0 It is better to define the TypeScript definition files (.d.ts) in your tsconfig.json under the "files"section.

如果您使用的是 TypeScript >= 2.0 最好在“文件”部分下的 tsconfig.json 中定义 TypeScript 定义文件 (.d.ts) 。

This could look like this:

这可能是这样的:

{
  "compileOnSave": true,
  "compilerOptions": {
    "noImplicitAny": true,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5", 
    "outDir": "./Scripts/"
  },
  "files": [
    "./src/foo.ts",
    "./src/bar.ts",
    "./Scripts/typings/jquery/jquery.d.ts",
    "./Scripts/typings/jqueryui/jqueryui.d.ts",
    "./Scripts/MicrosoftMaps/Microsoft.Maps.d.ts"
  ]
}

Using the /// directive (reference comments) is often used in examples to get you started quickly, but it is not a best practice. Also many examples come from a version < TypeScript 2.0.

在示例中经常使用 /// 指令(参考注释)来帮助您快速入门,但这不是最佳实践。还有许多示例来自 < TypeScript 2.0 的版本。

回答by Richard

This question is a duplicate of Reference typescript definitions in one file, instead of all JS files?

这个问题是一个文件引用打字稿定义的副本,而不是所有 JS 文件?

The answer is, for now, add each file you want to reference to your tsconfig.json file's "files" section. It's still many lines, but all in one file.

答案是,现在,将要引用的每个文件添加到 tsconfig.json 文件的“文件”部分。它仍然有很多行,但都在一个文件中。

In future when Typescript 2is released you can then use the "filesGlob" section and solve the problem in two lines.

将来,当Typescript 2发布时,您可以使用“filesGlob”部分并在两行中​​解决问题。

回答by Fenton

Some IDEs auto-detect all the files in a project (Visual Studio).

某些 IDE 会自动检测项目 (Visual Studio) 中的所有文件。

For everything else, you can create a _references.tsfile and put all of your reference comments in there - then you only ever need to add:

对于其他所有内容,您可以创建一个_references.ts文件并将所有参考评论放在那里 - 然后您只需要添加:

/// <reference path="_references.ts" />

...to each file (instead of possibly many).

...到每个文件(而不是可能很多)。

Your IDE may also support tsconfig files.

您的 IDE 也可能支持tsconfig 文件

回答by Cika Raki

I've started recently with TypeScript and as I've understood the internal modules resolution is that yes, you can compile all .tsfiles from the tsconfig.json's directory and all its subdirectories, provided that you don't have set .tsfiles in it without /// <references path="" />.

我最近开始使用 TypeScript,据我所知,内部模块解析是,是的,您可以编译's 目录及其所有子目录中的所有.ts文件tsconfig.json,前提是您没有在其中设置.ts没有/// <references path="" />.

But the order in which the .tsfiles are compiled into resulting .jsfiles is not determined by the dependencies the files (or classes they contain) have. So it is possible to have a situation where the child class is compiled before the parent one (child inherits from parent relation). Then the code won't run, even though it is compiled successfully. It will complain that it couldn't understand the parent class within the child class. Therefore you need to add a /// <references path="" />as a hint to the compiler to resolve the dependencies between .tsfiles.

但是.ts文件被编译成结果.js文件的顺序不是由文件(或它们包含的类)所具有的依赖关系决定的。因此,可能会出现子类在父类之前编译的情况(子类从父关系继承)。然后代码不会运行,即使它编译成功。它会抱怨它无法理解子类中的父类。因此,您需要向/// <references path="" />编译器添加一个作为提示来解决.ts文件之间的依赖关系。

This is want the Typescript documentation says:

这是想要打字稿文档说:

The /// directive is the most common of this group. It serves as a declaration of dependency between files.

Triple-slash references instruct the compiler to include additional files in the compilation process.

They also serve as a method to order the output when using --out or --outFile. Files are emitted to the output file location in the same order as the input after preprocessing pass.

/// 指令是该组中最常见的。它用作文件之间依赖关系的声明。

三斜线引用指示编译器在编译过程中包含其他文件。

在使用 --out 或 --outFile 时,它​​们还可以用作对输出进行排序的方法。预处理过程后,文件以与输入相同的顺序发送到输出文件位置。