20

How can I use an inner config.json in my Typescript project?

For clarity:

I have a root /config.json as following:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es2017",
        "inlineSourceMap": true,
        "outDir": "./app/",
        "lib": [
            "es6"
        ],
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "moduleResolution": "node",
        "baseUrl": "./",
        "noImplicitAny": true,
        "skipLibCheck": true, 
        "paths": {
            "*": [
                "types/*"
            ]
        }
    },
    "include": [
        "./src/**/*.ts"
    ]
}

I want to have a /innerFolder/config.json with the following:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "noImplicitReturns": true,
    "noUnusedParameters": true,
    "noUnusedLocals": true,
    "strictNullChecks": true,
  }
}

I'm doing that because I want to make a refactor on the team's code, but it's big and I don't want to change the all my files at once.

I want to do it folder-by-folder and "lock" the folder with the changes.

According to the documentation, the compiler looks up to a parent tsconfig.json, but not down for specific tsconfig.json.

Is there a way to accomplish that?

Gustavo Lopes
  • 3,794
  • 4
  • 17
  • 57

4 Answers4

5

TypeScript 2.1 added configuration inheritance and this should now work the way you're using it.

thisismydesign
  • 21,553
  • 9
  • 123
  • 126
3

Within a single project, there's no way to apply different compiler options to different files. If your folders follow a simple dependency structure, you may be able to use multiple projects with project references. Otherwise, you could write a script that runs tsc on the entire project twice (once with the stricter options and once with the looser options) and combines the errors from the first run on innerFolder and the errors from the second run on all other files; see this answer for some inspiration on how to filter errors by filename. This won't help with your IDE; there, you may have to just use the looser options.

Matt McCutchen
  • 28,856
  • 2
  • 68
  • 75
1

Once I only needed the rules for strict checking, I was able to achieve it using tslint.

extends from tslint does exactly what I need, it goes down, overriding rules in inner directories.

Gustavo Lopes
  • 3,794
  • 4
  • 17
  • 57
  • how to set implicit any false in tslint rules , i'm in similar situation as yours where i only need this configuration for some inner folder only. – Deepak Apr 15 '21 at 17:05
  • I guess you cannot do it. Only tsconfig has this rule, unfortunately – Gustavo Lopes Apr 19 '21 at 14:14
0

It should suffice to use:

tsc -p tsconfig.test.json

and set your "test" script to invoke that usage of tsc

see also: How to exclude specific files in typescript only for the build?

Jack Punt
  • 342
  • 1
  • 14