转译时排除 *.spec.ts 文件但仍然正确地整理它们

Rob*_*erd 5 typescript tsconfig atom-editor tslint

如何从被转译中排除打字稿文件,但仍确保它们与 Atom 编辑器中的 linter 一起正常工作?

我在我的*.spec.ts文件中收到此错误:

ES5/ES3 中的异步函数或方法需要“Promise”构造函数。确保您有“Promise”构造函数的声明或在您的--lib选项中包含“ES2015” 。

出现问题是因为我明确排除了包含所有测试文件的目录(请参阅下面的 tsconfig 文件),因为我不希望在构建项目时将这些文件转换为 JavaScript。但是,当我在 Atom 编辑器中查看它们时,我确实希望 tslint 插件正确地对这些文件进行检查。

我的设置:

  • 带有插件的 Atom.io 1.30:
    • 原子打字稿 12.6.3
    • 语言打字稿 0.4.0
    • linter-tslint 1.9.1
  • tslint 5.9.1
  • 打字稿 3.0.1

我的tsconfig.json文件:

{
  "compileOnSave": false,
  "compilerOptions": {
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "baseUrl": ".",
    "declaration": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "inlineSourceMap": true,
    "inlineSources": true,
    "lib": [
      "es2017",
      "dom"
    ],
    "moduleResolution": "node",
    "newLine": "lf",
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "outDir": "./dist",
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ]
  },
  "exclude": [
    "./spec",
    "./dist"
  ]
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*hen 5

您需要使用两个tsconfig.json文件,一个用于编辑器(包括*.spec.ts文件),另一个用于编译(不包括它们)。您可以使用extends来共享两个文件之间的大部分选项。请参阅此讨论

  • 这对我有用。我能够在我的 spec/ 目录中添加第二个 tsconfig.json 文件。Atom 编辑器会自动找到此文件并使用它来覆盖父目录中的主 tsconfig 文件。 (2认同)