Sub topic 9
The tsconfig files and what each one covers
This chapter assumes you know basic TypeScript syntax, have Node.js and npm installed, and can run commands in a terminal. It also assumes you have created a workspace with ng new.
Why there is more than one tsconfig
A new Angular workspace contains three TypeScript configuration files. The TypeScript compiler reads a tsconfig file to learn which files to compile and which rules to apply. Angular splits this setting into layers, because application code, test code, and editor tooling need different settings.
| File | Location | Purpose |
|---|---|---|
| `tsconfig.json` | workspace root | Shared compiler options and Angular compiler options |
| `tsconfig.app.json` | workspace root | Which files make up the application build |
| `tsconfig.spec.json` | workspace root | Which files make up the unit test build |
The base file: tsconfig.json
tsconfig.json holds the options shared by every build. It also configures the editor, which reads this file when you open a TypeScript file.
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"strict": true,
"noImplicitOverride": true,
"target": "ES2022",
"module": "preserve"
},
"angularCompilerOptions": {
"strictTemplates": true
}
}The exact list varies by CLI version. Three settings matter most:
strictturns on a group of type-checking rules, such asstrictNullChecksandnoImplicitAny. Leave it on for new projects.targetsets the JavaScript version that the compiler emits.angularCompilerOptionsis read by the Angular compiler, not by plain TypeScript.strictTemplatesmakes the compiler type-check your HTML templates.
The build files: tsconfig.app.json and tsconfig.spec.json
Both files extend the base file and then narrow the input.
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": []
},
"files": ["src/main.ts"],
"include": ["src/**/*.d.ts"]
}extends copies every option from the base file. The files entry names src/main.ts as the starting point, and the compiler follows imports from there. The empty types array stops global type packages, such as the test framework's, from leaking into application code.
tsconfig.spec.json follows the same pattern, but it adds the test framework types and includes the spec files:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": ["jasmine"]
},
"include": ["src/**/*.ts"]
}The types value depends on your test runner. Jasmine is shown here, and newer CLI versions may generate a different runner.
How angular.json connects to them
The files do not run by themselves. Each build target in angular.json points to one through its tsConfig option. The build target uses tsconfig.app.json, and the test target uses tsconfig.spec.json.
Common mistakes
- Loosening
strictto silence errors. The errors usually point to real bugs. Fix the types instead. - Putting application-only options in the base file. They then affect tests too. Put them in the specific file.
- Using a new file without referencing it. A new tsconfig has no effect until a target's
tsConfigoption points to it.
Exercise
A teammate adds "types": ["jasmine"] to tsconfig.json so that test globals stop showing errors in the editor. What problem does this create, and where should the setting go instead?
Show answer
Because tsconfig.app.json extends tsconfig.json, the application build now also loads the Jasmine types, so test globals such as describe and it become valid in application code without an error. The setting belongs in tsconfig.spec.json, which only the test build uses.