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

1

6

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"
  ]
}
Endolymph answered 14/9, 2018 at 23:14 Comment(0)
J
7

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.

Jammie answered 15/9, 2018 at 2:47 Comment(1)
This worked for me. I was able to add a second tsconfig.json file in my spec/ directory. The Atom editor automatically found this file and used it to override the main tsconfig file from the parent directory.Endolymph

© 2022 - 2024 — McMap. All rights reserved.