How does tsc know which files to check, how strictly to check them, and where to put the .js files it writes? Before it starts, tsc reads one file: tsconfig.json, the TypeScript settings file for one project. (tsc is the TypeScript Compiler, the program that checks your .ts and then turns it into .js.)
If TypeScript is a security guard, tsconfig.json is the guard's duty sheet. The sheet says which rooms (files) to watch (include), how strictly the guard checks (strict), and where the finished work (the .js files) goes and in what form (outDir, target, module).
Making a tsconfig.json
In the project folder, run npx tsc --init. You get a tsconfig.json with the recommended settings, comments included. For this lesson we use a small version:
{
"compilerOptions": {
"target": "es2022",
"module": "esnext",
"strict": true,
"rootDir": "./src",
"outDir": "./dist"
},
"include": ["src"]
}
compilerOptionsholds the settings for how to check the code and how to write the JavaScript.target: the JavaScript version produced, for examplees2017ores2022(the JavaScript version of 2017 or 2022).module: the shape ofimport/exportin the output.esnextfor a Vite or modern browser project,nodenextfor Node.js.strict: turns on every strict check at once.rootDirandoutDir: which folder the.tsfiles come from, and which folder the.jsfiles are written to. With these settings,src/index.tsbecomesdist/index.js.include: which files get checked.
The file from npx tsc --init in TypeScript 7 is much stricter than this small version. Three of its settings often reject your first file. The table's first row names "type" in package.json. That setting decides whether your project's files are read as CommonJS (the old Node.js module style, with require) or as modules with import/export:
Written by --init |
What happens |
|---|---|
"module": "nodenext" and "verbatimModuleSyntax": true |
If package.json holds "type": "commonjs" (written by npm init -y, the command that creates package.json), export function is rejected: A top-level 'export' modifier cannot be used on value declarations in a CommonJS module when 'verbatimModuleSyntax' is enabled. ts(1287). Change it to "type": "module". |
"noUncheckedIndexedAccess": true |
An array item such as list[0] may be undefined, so you must check it first. |
"types": [] |
The types from @types/node are not loaded: Cannot find name 'process'. ts(2591). Write "types": ["node"]. |
If you are just starting, use the small version above. If you use the --init file, first change two things from that table: "type" in package.json and "types" in the tsconfig.
Once the tsconfig is ready, run one of these commands:
npx tsc: check every file ininclude, then write its.jstooutDir.npx tsc --noEmit: only check, and write no files at all. Vite projects check the same way. In the React + TypeScript template, for example, thebuildscript (the commandnpm run buildruns) istsc -b && vite build, with"noEmit": true(the same as--noEmit, but written in the tsconfig). The reason is that Vite itself turns.tsinto JavaScript.npx tsc --watch: check again every time a file is saved.
include: which files get checked
"include": ["src"] means every TypeScript file in the src folder and its subfolders. Files outside it are never read. Say there's a other/x.ts holding const x: number = "text": its error never appears, because that file is never checked. exclude removes some files from include, for example "exclude": ["src/**/*.test.ts"] (every file ending in .test.ts in src and its subfolders). When exclude isn't written, node_modules and the outDir folder are already left out.
strict: one switch, many checks
This is the src/index.ts that the Lab also uses:
export function greet(name) {
return "Hello, " + name
}
const cities = ["Leeds", "Manchester"]
const found = cities.find((k) => k.startsWith("S"))
console.log(found.toUpperCase())
const first = cities[0]
console.log(first.toUpperCase())
console.log(greet(found ?? "Guest"))
"strict": true turns on several checks at once. These are the two you'll notice most:
noImplicitAny: a parameter with no type is rejected.Parameter 'name' implicitly has an 'any' type.ts(7006). This meansnamesilently gets the typeany.strictNullChecks: a value that can beundefinedornullhas to be checked first.findreturnsundefinedwhen nothing matches, sofound.toUpperCase()is rejected:'found' is possibly 'undefined'.ts(18048).
Since TypeScript 6.0, strict is on when you write nothing. So greet(name) still gets ts(7006) even when the tsconfig is just {}. Still write "strict": true so it's clear to whoever reads it. Each check can be turned off on its own, for example "noImplicitAny": false.
One check is not part of strict: noUncheckedIndexedAccess. When it's on, cities[0] is treated as possibly undefined (the array could be empty), so first.toUpperCase() is rejected too. noUncheckedIndexedAccess only has an effect when strictNullChecks is on.
These are the errors for the src/index.ts above, with each setting:
| Settings | ts(7006) on name |
ts(18048) on found |
ts(18048) on first |
Total |
|---|---|---|---|---|
"strict": false |
- | - | - | 0 |
"strict": true |
yes | yes | - | 2 |
"strict": true, "noImplicitAny": false |
- | yes | - | 1 |
"strict": true, "noUncheckedIndexedAccess": true |
yes | yes | yes | 3 |
Why bother? No city starts with S, so found really is undefined, and this code crashes when it runs: TypeError: Cannot read properties of undefined (reading 'toUpperCase'). With strict off, tsc reports no error at all, and you only find the bug in the browser.
target and module: the shape of the JavaScript
Checking settings such as strict don't change the JavaScript that tsc writes. target and module do. The last line of src/index.ts uses ??, which was only added in ES2020 (the JavaScript version of 2020). This is that line in dist/index.js, for two target values:
target |
Last line of dist/index.js |
|---|---|
es2017 |
console.log(greet(found !== null && found !== void 0 ? found : "Guest")); |
es2020 and up |
console.log(greet(found ?? "Guest")); |
With an older target, tsc rewrites the code so older browsers can run it. void 0 means undefined, so the long line does the same as found ?? "Guest". Then module decides the shape of the export. With "module": "esnext", export function greet stays as it is. With "module": "commonjs", it becomes:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.greet = greet;
function greet(name) {
return "Hello, " + name;
}
⚠️ Settings that are out of date, or switched off
- Copying an old tsconfig from a tutorial.
"target": "es5"and"moduleResolution": "node"were removed in TypeScript 7, and TypeScript 7 rejects them:Option 'target=ES5' has been removed. Please remove it from your configuration.ts(5108). Usees2017or later. outDirwithoutrootDir. In TypeScript 7, if your files live insrcand you writeoutDir, you have to writerootDirtoo:The common source directory of 'tsconfig.json' is './src'. The 'rootDir' setting must be explicitly set to this or another path to adjust your output's file layout.ts(5011).- Thinking
noUncheckedIndexedAccesscomes withstrict. It doesn't.npx tsc --initalready turns it on, but in the small tsconfig above, write it yourself if you want to use it. - Turning
strictoff because there are lots of errors. Each error fromstrictpoints at a bug that can really happen, likefoundturning out to beundefinedabove.
TL;DR: tsconfig.json is the settings file of tsc (the TypeScript Compiler) for one project, and it is made with npx tsc --init. include decides which files get checked, and exclude removes some of them. strict turns on strict checks such as noImplicitAny (a parameter with no type is rejected) and strictNullChecks (a value that could be undefined must be checked first). Since TypeScript 6.0, strict is on even when you don't write it. noUncheckedIndexedAccess (array items are treated as possibly undefined) is not part of strict. target sets the JavaScript version produced, module sets the shape of its import/export, and outDir and rootDir set the output and source folders. npx tsc checks and then writes .js, while npx tsc --noEmit only checks.
⚡ Orders for tsc
Write:
{
"compilerOptions": {
"target": "es2022",
"module": "esnext",
"strict": true,
"rootDir": "./src",
"outDir": "./dist"
},
"include": ["src"]
}
Meaning: This is tsconfig.json, the settings file of tsc for one project. It works like a duty sheet: which files to check, how strictly, and where to write the result. include decides which files get checked, and files outside it aren't read at all. strict turns on the strict checks. target and module decide the shape of the JavaScript produced. rootDir (the source folder) and outDir (the output folder) turn src/index.ts into dist/index.js. npx tsc checks and then writes .js, while npx tsc --noEmit only checks.
⚡ strict catches what could be undefined
Write:
const cities = ["Leeds", "Manchester"]
const found = cities.find((k) => k.startsWith("S"))
console.log(found.toUpperCase())
Guess first: find looks for a city that starts with S. With strict off, what happens when this code is checked and then run?
tsc reports no error, then the code crashes in the browser: TypeError: Cannot read properties of undefined. No city starts with S, so find returns undefined, and found really is undefined.
Meaning: strict turns on many checks at once, like noImplicitAny (a parameter without a type is rejected) and strictNullChecks (a value that could be undefined must be checked first). With strict on, found.toUpperCase() is rejected with ts(18048) before the code runs. Since TypeScript 6.0, strict is on even when you don't write it.
⚡ target decides the JavaScript's shape
Meaning: Checking settings such as strict don't change the JavaScript that tsc writes. target and module do. With target set to es2017 (the JavaScript version of 2017), found ?? "Guest" is rewritten as a long check so older browsers can run it, because ?? was only added in ES2020. With es2020 and up, ?? stays as it is.