我必须在每个文件中引用TypeScript定义吗? [英] Do I have to reference TypeScript definition in every file?

查看:81
本文介绍了我必须在每个文件中引用TypeScript定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以告诉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.tsfile that contains references to all other definition files installed via tsd.

TypeScript> = v1.5的更新:您可以使用

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆