Sub topic 1
What tsconfig.json does and how tsc finds it
A tsconfig.json file tells the TypeScript compiler (tsc) three things. It says which files to read, which JavaScript to produce, and which checks to run. The file also marks a directory as the root of a TypeScript project, so editors and build tools use it as well.
This chapter covers what the file contains, how tsc locates it, and the mistakes that come from misunderstanding the lookup. Later chapters cover individual options in detail.
Prerequisites
This chapter assumes you already know the following:
- Basic TypeScript syntax: type annotations, interfaces, and functions.
- How to run commands in a terminal with Node.js and npm installed.
- JSON syntax, including objects, arrays, strings, and booleans.
You also need TypeScript installed in your project, for example with npm install --save-dev typescript. The examples run it through npx.
What the file does
Without a config file, tsc needs every input file on the command line, and it uses default options that are lenient. That does not scale. A tsconfig.json records your decisions once, so every developer, your editor, and your CI job compile the project the same way.
A minimal file for a Node.js project looks like this:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"outDir": "dist",
"rootDir": "src",
"strict": true,
"skipLibCheck": true
},
"include": ["src"]
}The file has two jobs, and its top-level keys reflect them:
| Key | Purpose |
|---|---|
| `compilerOptions` | Controls language level, module format, output location, and type-checking rules. |
| `include`, `exclude`, `files` | Select the input files. |
| `extends` | Inherits settings from another config file. |
| `references` | Links to other projects in a multi-package repository. |
Other top-level keys exist, such as watchOptions, but you will rarely need them early on.
The file format
tsconfig.json looks like JSON, but tsc parses it more loosely. It accepts comments (// and /* */) and trailing commas. Strict JSON tools may reject these, so keep that in mind if another program reads the file.
{
"compilerOptions": {
// Emit modern JavaScript for Node 18+
"target": "ES2022",
"strict": true, // trailing commas are also allowed
},
}Relative paths
Paths inside a tsconfig.json, such as outDir, rootDir, and the entries in include, are resolved relative to the directory that contains that file. They are not relative to the directory where you run the command. This matters once you have several config files, and it matters again when one config inherits from another.
How tsc finds the file
The lookup depends on how you invoke tsc.
- You pass input files on the command line (
npx tsc src/index.ts).tscignorestsconfig.jsoncompletely and uses default options. - You pass no input files and no
--projectflag (npx tsc).tsclooks fortsconfig.jsonin the current directory. If it is not there, it checks the parent directory, then that directory's parent, and so on up to the filesystem root. - You pass
--projector-p. The value can be a directory containing atsconfig.json, or a path to a specific JSON file. No upward search happens.
The common commands are these:
npx tsc --init
npx tsc --noEmit
npx tsc --showConfig
npx tsc --listFiles--initwrites a startertsconfig.jsonin the current directory. The generated contents, including which options are on and how many comments it has, vary between TypeScript versions. Read the result and edit it instead of assuming it is right for your runtime.--noEmitruns the type checker without writing any JavaScript. It is a common CI step.--showConfigprints the configuration thattscresolved, withextendsmerged in.--listFilesprints every file in the compilation, including files fromnode_modulesand the built-in library typings.
The last two commands are your main debugging tools. A later chapter in this tutorial covers them in detail.
Choosing a config explicitly
The upward search is convenient but can surprise you. If you run npx tsc in a subdirectory that has no config, it may silently pick up one from a parent directory, and then your paths resolve relative to that parent. In scripts, prefer being explicit:
npx tsc -p tsconfig.build.json
npx tsc --project packages/apiA common pattern is to keep tsconfig.json for the editor and type-checking, and a second file such as tsconfig.build.json for production builds that excludes test files.
How editors find the file
Your editor does not run tsc. It runs a language service (tsserver) that applies the same idea per file. For each file you open, it searches upward from that file's directory for the nearest tsconfig.json. It then checks whether the file is selected by that config's include, exclude, and files.
If no config includes the file, the editor treats it as part of an "inferred project" with default options. That is why a file can show no errors in your editor but fail in tsc, or the reverse. Typical causes:
- The file sits outside every
includepattern, for example a test file in atestsdirectory whenincludelists onlysrc. - A nested
tsconfig.jsonis closer to the file than the one you edited. - The editor has not reloaded after you changed the config. Restarting the TypeScript server fixes this.
Common errors
- Passing a file name and expecting your options to apply. Running
npx tsc src/index.tscompiles with defaults, so yourstrictsetting has no effect. Newer TypeScript releases report an error when a config file exists but is skipped for this reason. Runnpx tscwith no file arguments, or use-p. - "No inputs were found in config file". The
includepatterns matched nothing, often becauseincludepoints at a directory that does not exist or has no.tsfiles. - Output in the wrong place. Without
outDir,tscwrites each.jsfile next to its.tssource. SetoutDirearly. - Surprising file selection. If you omit
includeandfiles,tsctakes every TypeScript file under the config's directory. Be explicit instead of relying on this default. - Running from the wrong directory. The upward search can load a config you did not intend. Print the result with
--showConfigif you are unsure.
A short worked example
Create a project with this layout:
my-app/
tsconfig.json
src/
index.tsPut the minimal config from earlier into tsconfig.json. Then add this to src/index.ts:
export function greet(name: string): string {
return `Hello, ${name}`;
}
console.log(greet("tsconfig"));Run npx tsc from my-app. The compiler finds tsconfig.json in the current directory, reads src/index.ts because of include, and writes dist/index.js because of outDir. Because rootDir is src, the output path drops the src segment. Now move into src and run npx tsc again. The search goes up one level, finds the same config, and the result is identical. Run npx tsc src/index.ts instead, and your options are skipped.
Exercise
You run npx tsc src/app.ts in a project whose tsconfig.json sets "strict": true and "outDir": "dist". Which settings apply, and what command compiles the project with its config?
Show answer
Neither setting applies, because naming an input file on the command line makes tsc ignore tsconfig.json and use default options. The output also lands next to the source file instead of in dist. To use the config, run npx tsc with no file arguments, or npx tsc -p tsconfig.json to name the config explicitly.
Exercise
A file shows no type errors in your editor, but npx tsc reports errors in it. Give two likely causes.
Show answer
Likely causes include the editor using a different tsconfig.json than tsc (a nearer nested config, or no config at all so the file is in an inferred project with default options), and the editor's TypeScript server running with stale settings until it is restarted. Running npx tsc --listFiles and npx tsc --showConfig shows which files and options tsc actually uses.