Sub topic 9
The tsconfig files and what each one covers
A new Angular workspace contains several TypeScript configuration files. They all feed the same compiler, but each one answers a different question: which compiler rules apply everywhere, which files belong to the application, and which files belong to the tests. This chapter explains how they relate and how to change them safely.
It assumes you can read JSON and basic TypeScript. It also assumes you know that angular.json points the build and test targets at specific config files, as the earlier chapter on angular.json showed.
Why there is more than one tsconfig
A single TypeScript config applies one set of rules to one set of files. An Angular project needs at least two sets:
- The application build compiles
src/main.tsand everything it imports. It must not see test-only types such asdescribeandit. - The test build compiles
*.spec.tsfiles. It needs the testing framework's types.
Angular solves this with one shared base file and one small file per purpose. Each small file uses extends to inherit the base and then narrows the file list and types.
tsconfig.json: the shared base
This file sits at the workspace root. It holds compiler rules that every other config inherits. Its content varies a little by CLI version, but a typical file looks like this:
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"isolatedModules": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"moduleResolution": "bundler",
"importHelpers": true,
"target": "ES2022",
"module": "ES2022"
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}It has no files or include list. The base file does not choose which sources to compile. The derived files do.
Options worth understanding
| Option | What it does |
|---|---|
| `strict` | Turns on the family of strict checks, including `strictNullChecks` and `noImplicitAny`. |
| `noImplicitOverride` | Requires the `override` keyword when a subclass method replaces a base method. |
| `noPropertyAccessFromIndexSignature` | Forces `obj['key']` for properties that exist only through an index signature. |
| `skipLibCheck` | Skips type checking of `.d.ts` files in dependencies. This speeds up builds and avoids errors from third-party typings. |
| `isolatedModules` | Reports constructs that cannot be compiled one file at a time. The esbuild-based builder needs this. |
| `experimentalDecorators` | Enables the decorator syntax that `@Component` and `@Injectable` use. |
| `moduleResolution: bundler` | Resolves imports the way a bundler does, so extensionless relative imports work. |
| `target` / `module` | Set the output language level. `ES2022` keeps modern syntax. The CLI's own build step does further downleveling for browsers. |
The angularCompilerOptions block
TypeScript ignores angularCompilerOptions. The Angular compiler reads it. The most important flag is strictTemplates. With it on, Angular type-checks your HTML templates as strictly as TypeScript code. For example, binding a string | undefined value to an input that accepts only string becomes an error instead of a silent bug.
tsconfig.app.json: the application build
The build target in angular.json uses this file through its tsConfig option.
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": []
},
"files": ["src/main.ts"],
"include": ["src/**/*.d.ts"]
}Each part has a job:
extendspulls in every option from the base file. Options set here override the base.outDiris only a placeholder for type-checking output. The real build output goes todist/, controlled byangular.json.types: []stops TypeScript from automatically including every@types/*package innode_modules. An empty list means application code sees no ambient test globals. If you usedescribein application code by mistake, the build fails.fileslists the entry point. The compiler follows imports fromsrc/main.ts, so you do not list every component.includeadds declaration files (.d.ts) that no import reaches, such as a global type shim.
Because the build starts at main.ts, a file that nothing imports is not compiled. If you add a component but never reference it, it is silently left out of the bundle. Recent CLI versions may also warn about unused standalone imports.
tsconfig.spec.json: the test build
The test target points at this file.
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": ["jasmine"]
},
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}Two differences from the app file matter:
typeslistsjasmine, sodescribe,itandexpecttype-check. If you switch to another runner, such as Jest or Vitest, change this entry to match, for example"jest"or"vitest/globals". Newer CLI versions generate a different value when they default to a different runner, so check what your workspace contains.includeuses a glob for spec files instead of a single entry point, because tests have no one root file. Every*.spec.tsundersrcis compiled.
Other files you may see
tsconfig.worker.jsonappears if you generate a web worker. It follows the same pattern, withtypesset to["worker"]-style typings and its owninclude.- Library projects in a multi-project workspace get
tsconfig.lib.jsonandtsconfig.lib.prod.json. The production variant disables Ivy partial compilation settings differently, so published libraries stay compatible across Angular versions. - Server-side rendering adds
tsconfig.server.jsonin some versions. Newer versions fold it intotsconfig.app.json.
Adding path aliases
Long relative imports such as ../../../shared/ui/button are a common reason to edit the base config. Add paths under compilerOptions in tsconfig.json:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"paths": {
"@shared/*": ["src/app/shared/*"],
"@env/*": ["src/environments/*"]
}
}
}The snippet above shows only the added option, so merge the paths block into your existing compilerOptions rather than creating a second file. Then import with import { Button } from '@shared/ui/button';.
Put aliases in the base file. Because the app and spec files inherit it, both builds resolve the same aliases. If you put them only in tsconfig.app.json, your tests will fail to resolve the imports. With moduleResolution: bundler, paths are resolved relative to the tsconfig file that declares them, and baseUrl is not required.
Common errors
- Test globals missing in specs. Symptom:
Cannot find name 'describe'. Cause:typesintsconfig.spec.jsondoes not name your test framework, or the spec file lies outsideinclude. - Test globals leaking into app code. Cause:
typeswas removed fromtsconfig.app.json, so every installed@typespackage is included. - Alias works in the editor but not in the build. Cause: the alias was defined in a file that the build target does not extend. Check the
tsConfigpath inangular.json. - Editor shows errors the CLI does not. Cause: the editor picked
tsconfig.json, which has nofiles, instead of the file for that source. Open the project through a solution-style setup or reload the TypeScript server. - Changed an option and nothing happened. The dev server reads configs at startup. Restart
ng serveafter editing a tsconfig.
Exercise
A teammate adds "@shared/*": ["src/app/shared/*"] to paths in tsconfig.app.json. The app builds, but ng test fails with "Cannot find module '@shared/ui/button'". Why, and where should the alias live?
Show answer
The test target uses tsconfig.spec.json, which extends only tsconfig.json, not tsconfig.app.json. The alias is therefore invisible to tests. Move the paths entry into the base tsconfig.json so both derived files inherit it.
Exercise
Why does tsconfig.app.json set "types": [], and what happens if you delete that line?
Show answer
An empty types list stops TypeScript from including every @types/* package automatically, so application code cannot use test globals such as describe. If you delete the line, all installed @types packages become visible to the app build, and test-only names can slip into production code without an error.