为什么TypeScript编译器会忽略tsconfig.json?

Pro*_*ofK 11 json typescript gulp asp.net-core

我有这个文件,从教程中粘贴(让我们面对它,文档,tuts和示例之间的差异令人震惊):

/scripts/tsconfig.json:

{
    "compilerOptions": {
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "module": "commonjs",
        "noEmitOnError": true,
        "noImplicitAny": false,
        "outDir": "../wwwroot/appScripts/",
        "removeComments": false,
        "sourceMap": true,
        "target": "es5",
        "moduleResolution": "node"
    },
    "exclude": [
        "node_modules",
        "typings/index",
        "typings/index.d.ts"
    ]
}
Run Code Online (Sandbox Code Playgroud)

选项设置为在保存时编译,但每当我保存一个TypeScript文件时,JavaScript输出最终在源文件的"下"或"附加到":

TypeScript
|
--test.ts 
    |
    --test.js
Run Code Online (Sandbox Code Playgroud)

,它与源物理位于同一目录中/TypeScript.如果tsconfig.json缺少,编译器会抱怨,但是当它存在时,肯定是,编译器会忽略该"outDir": "../wwwroot/appScripts/"设置.

我真的是Gulp的新手,但Gulp的任务看起来还不错:

var tsProject = ts.createProject('scripts/tsconfig.json');
gulp.task('ts', function (done) {
    //var tsResult = tsProject.src()
    var tsResult = gulp.src([
            "scripts/*.ts"
    ])
        .pipe(ts(tsProject), undefined, ts.reporter.fullReporter());
    return tsResult.js.pipe(gulp.dest('./wwwroot/appScripts'));
});
Run Code Online (Sandbox Code Playgroud)

Sk_*_*Sk_ 15

选项设置为在保存时编译

保存文件时,它会自动编译该文件中导入的单个文件和文件.从IDE关闭自动编译选项,因此编译器将考虑tsconfig.json文件.

在命令行中指定输入文件时,将忽略tsconfig.json文件.

目录中存在tsconfig.json文件表示该目录是TypeScript项目的根目录.tsconfig.json文件指定编译项目所需的根文件和编译器选项.项目以下列方式之一编译:

使用tsconfig.json

  1. 通过在没有输入文件的情况下调用tsc,在这种情况下,编译器将从当前目录开始搜索tsconfig.json文件并继续向上运行父目录链.

  2. 通过调用没有输入文件的tsc和--project(或只是-p)命令行选项来指定包含tsconfig.json文件的目录的路径.

https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

  • 当您指定要编译的文件时,tsc 忽略您的配置文件是否有逻辑原因?当您尝试通过在单个文件上测试配置的速度来调试配置时,这尤其令人烦恼。 (11认同)
  • 我仔细地指定了所有这些编译器选项,然后,在一种未记录的奇怪行为中,编译器忽略了编译器选项......因为我指定了一个输入文件?什么? (8认同)
  • 5 岁了,仍然准确。这应该是公认的答案。 (4认同)

Rom*_*kyi 14

原因之一可以明确指定文件

代替

npx tsc index.ts
Run Code Online (Sandbox Code Playgroud)

跑步

npx tsc
Run Code Online (Sandbox Code Playgroud)

通过在没有输入文件的情况下调用 tsc,在这种情况下,编译器会搜索tsconfig.json(c) TypeScript 文档