tsconfig deep dive
Files and include
A tsconfig file answers two questions at once: which files belong to the project and which rules apply to them. The include list names the source folders, while exclude keeps generated output out.
Start strict about scope. One source folder in and the output folder out covers most small projects.
{ "include": ["src"], "exclude": ["dist"], "compilerOptions": { "strict": true } }
Target vs lib
Target sets the age of the JavaScript that tsc emits. A modern target keeps classes and async code almost untouched, while an older target rewrites them into longer compatible forms.
Lib lists the built in APIs the checker may assume, such as DOM or ES2020 helpers. Match lib to the runtimes where the code will actually run.
{ "compilerOptions": { "target": "ES2020", "lib": ["ES2020", "DOM"] } }
Module systems
Module picks the import and export format of the emitted files. Browsers and bundlers prefer ESNext style imports, while plain Node scripts often still consume CommonJS output.
Pick the format your runner understands first, then let bundlers translate from there. Changing it later rewrites every emitted file.
{ "compilerOptions": { "module": "ESNext", "moduleResolution": "Bundler" } }
Paths and baseUrl
Deep relative imports are hard to read and brittle to move. BaseUrl plus paths defines short aliases so an import always starts from a known root.
Keep the alias map tiny. One or two aliases remove most dot heavy imports without hiding the folder structure.
{ "compilerOptions": { "baseUrl": ".", "paths": { "@app": ["app"], "@utils": ["utils"] } } }
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the
includelist of a tsconfig name?Answer
The source files and folders that belong to the project
include is the scope of the project, and exclude removes folders such as the build output.
-
Why does a project exclude its output folder?
Answer
So tsc does not read and recompile its own emitted files
The emitted JavaScript is not source, and including it can build stale files into the project.
-
What is the difference between
targetandlib?Answer
target chooses the output syntax; lib lists the APIs the checker assumes
target rewrites the emitted JavaScript, while lib decides which built-in types exist.
-
What does
moduleResolutionchoose?Answer
How the compiler looks up the module an import names
The setting tells the compiler which lookup rules apply, as Bundler or Node style.
-
What does this program print?
const found = [1, 2, 3].includes(2); console.log(found);Answer
true
The array holds 2, so includes finds it and returns true.
-
What does this program print?
console.log("abc".padStart(5, "-"));Answer
--abc
padStart adds the dashes on the left until the string has five characters.
-
What does this program print?
console.log(Object.entries({ a: 1 }).length);Answer
1
The object has one entry, and the pair list has length 1.
-
What does this program print?
console.log(2 ** 10);Answer
1024
The exponent operator raises 2 to the tenth power.
-
What does the compiler say about this program?
const x: string | undefined = undefined; console.log(x.length);Answer
error TS18048: 'x' is possibly 'undefined'.
strictNullChecks is part of strict, so the possibly undefined value must be checked first.
-
What does the compiler say about this program?
function greet(name) { return "Hi " + name; } console.log(greet("Ada"));Answer
error TS7006: Parameter 'name' implicitly has an 'any' type.
strict includes noImplicitAny, so every parameter needs a type.
Article author: Arthur Isaev