'Specific tsconfig.json rules inside folder

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?



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matt McCutchen
Solution 2 thisismydesign
Solution 3 Jack Punt