Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude *.spec.ts files when transpiling but still lint them properly

How can I exclude typescript files from being transpiled but still ensure that they work properly with the linter in the Atom editor?

I'm getting this error in my *.spec.ts files:

An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your --lib option.

The problem occurs because I'm explicitly excluding the directory with all of my test files (see the tsconfig file below), because I don't want these files to be transpiled to JavaScript when I build the project. However, I do want these files to be properly linted by the tslint plugin while I'm viewing them in the Atom editor.

My setup:

  • Atom.io 1.30 with plugins:
    • atom-typescript 12.6.3
    • language-typescript 0.4.0
    • linter-tslint 1.9.1
  • tslint 5.9.1
  • typescript 3.0.1

My tsconfig.json file:

{
  "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"
  ]
}
like image 234
RobotNerd Avatar asked Sep 14 '18 23:09

RobotNerd


1 Answers

You'll need to use two tsconfig.json files, one for the editor (which includes the *.spec.ts files) and another for compilation (which excludes them). You can use extends to share most of the options between the two files. See this discussion.

like image 87
Matt McCutchen Avatar answered Sep 21 '22 15:09

Matt McCutchen