如何在不使用任何引用的情况下编译 TypeScript

How to compile TypeScript without using any references whatsoever

请原谅我的误解,但我认为如果我在我的项目根目录下使用 tsconfig.json 文件,那么我将不再需要使用任何 ///<reference path="..." /> 标签来制作我的代码编译。我错了吗?

例如,我正在使用 AngularJS。我的 App.ts 文件看起来像这样:

import SomeModule from './whatever/SomeModule';

angular.module('foo', [SomeModule.name]).run(...);

我的 tsconfig.json 文件(部分)如下所示:

{
    "compileOnSave": false,
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs"
    },
    "filesGlob": [
        "./www/**/*.ts",
        "./typings/**/*.ts"
    ],
    "files": [
        /* a bunch of files omitted for brevity*/
        "./typings/angularjs/angular.d.ts"
    ],
}

请注意,我在文件数组中列出了 angular 定义文件的路径。另请注意,我将 compileOnSave 选项设置为 false。最终我想使用 Browserify 来编译和捆绑所有代码。但是现在我只想看看我是否可以用 tsc.

编译它

但是当我 运行 tsc App.ts 时,我得到一个错误 "Cannot find name 'angular'." 如何让 TypeScript 在编译代码时使用 angular.d.ts 文件?

运行 tsc App.ts 将不起作用,因为正在指定文件。按照文档中的说明进行操作:

Using tsconfig.json

  • By invoking tsc with no input files, in which case the compiler searches for the tsconfig.json file starting in the current directory and continuing up the parent directory chain.
  • By invoking tsc with no input files and a -project (or just -p) command line option that specifies the path of a directory containing a tsconfig.json file.

When input files are specified on the command line, tsconfig.json files are ignored.

Source