我必须在每个文件中引用TypeScript定义吗? [英] Do I have to reference TypeScript definition in every file?
问题描述
是否可以告诉TypeScript使用某个文件(或一组文件)作为所有编译内容的定义?
Is there a way to tell TypeScript to use a certain file (or set of files) as a definition for everything compiled?
目前,我唯一的选择是在每个TypeScript文件中添加这样的内容(看起来很笨拙):
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" />
推荐答案
在使用TypeScript的内部模块系统时,可以避免在代码中完全没有任何<reference>
标记.我个人这样做是因为在不断移动内容时,我不想在代码中编码路径(真实或绝对).
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源文件作为参数传递给编译器.
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.
将 gulp 与 tsd 放入堆栈时,只需传递包含对所有内容的引用的tsd.d.ts
文件通过tsd
安装的其他定义文件.
Using gulp together with gulp-typescript simplifies this task. You can set noExternalResolve
in gulp-typescript
to 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 tsd into your stack, you only need to pass the tsd.d.ts
file that contains references to all other definition files installed via tsd
.
UPDATE for TypeScript >= v1.5: you can use a tsconfig.json file, and the compiler will get the ordering of the classes right. This removes the need to use gulp-typescript
alltogether. You can either chose to have all files explicitly listed in the tsconfig.json
file, or completely leave out the files
property to include all *.ts/*.tsx
files within the directory the tsconfig.json
resides (including all subfolders).
示例tsconfig.json
可能如下所示:
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"lib": [ "es5", "es2015.promise", "dom" ]
},
"include": [
"src/**/*.ts"
]
}
这篇关于我必须在每个文件中引用TypeScript定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!