tsconfig.json Configuration Basics in Typescript - Time & Space Complexity
When we use tsconfig.json, it controls how TypeScript compiles code. Understanding time complexity here means looking at how the compiler's work grows as the project size grows.
We want to know: How does the compile time change when we add more files or options?
Analyze the time complexity of the following tsconfig.json setup.
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"outDir": "dist"
},
"include": ["src/**/*"]
}
This config tells TypeScript to compile all files in the src folder with strict checks and output to dist.
Look at what the compiler does repeatedly.
- Primary operation: Compiling each source file in the included folders.
- How many times: Once per file matched by the include pattern.
As you add more files in src, the compiler must process each one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Compiles 10 files |
| 100 | Compiles 100 files |
| 1000 | Compiles 1000 files |
Pattern observation: The compile work grows directly with the number of files.
Time Complexity: O(n)
This means compile time grows in a straight line as you add more files.
[X] Wrong: "Adding more compiler options will always make compile time much slower."
[OK] Correct: Some options only change checks or output style without adding loops over files, so they don't always increase compile time significantly.
Knowing how tsconfig.json affects compile time helps you understand project scaling and build performance, a useful skill for real projects and teamwork.
"What if we add an exclude pattern to skip some files? How would the time complexity change?"
Practice
tsconfig.json file in a TypeScript project?Solution
Step 1: Understand the role of tsconfig.json
Thetsconfig.jsonfile tells TypeScript how to compile the project, including options and files.Step 2: Compare options
The other options describe unrelated tasks: listing files, installing packages, or running commands manually.Final Answer:
To configure how TypeScript compiles the project -> Option DQuick Check:
tsconfig.json = compile config [OK]
- Thinking tsconfig.json installs packages
- Confusing tsconfig.json with package.json
- Believing tsconfig.json runs commands
tsconfig.json?Solution
Step 1: Identify correct property for target
The correct property insidecompilerOptionsto set JavaScript version istarget.Step 2: Check JSON structure
The snippet withtargetnested insidecompilerOptionsis correct. The one with target directly at root misses thecompilerOptionswrapper. The ones usingversionorjsVersionhave wrong property names.Final Answer:
{ "compilerOptions": { "target": "ES6" } } -> Option BQuick Check:
target inside compilerOptions = correct [OK]
- Placing target outside compilerOptions
- Using wrong property names like version or jsVersion
- Incorrect JSON nesting
tsconfig.json snippet:{
"compilerOptions": {
"outDir": "dist",
"target": "ES5"
},
"include": ["src/**/*"]
}Which files will TypeScript compile?
Solution
Step 1: Understand the include pattern
The patternsrc/**/*means all files in src and any subfolders recursively.Step 2: Check outDir effect
outDirsets where compiled files go (dist), but does not affect which files are compiled.Final Answer:
All files inside the src folder and its subfolders -> Option AQuick Check:
include src/**/* = all src files [OK]
- Confusing outDir with input files
- Thinking include only covers top-level files
- Assuming node_modules are included by default
tsconfig.json snippet:{
"compilerOptions": {
"target": "ES6",
"outDir": "build"
},
"include": ["src/**/*"],
"exclude": "node_modules"
}Solution
Step 1: Check exclude property type
Theexcludeproperty must be an array of strings, but here it is a single string.Step 2: Validate other properties
targetwith "ES6" is valid,outDircan be any folder name, andincludeis correctly an array.Final Answer:
"exclude" should be an array, not a string -> Option CQuick Check:
exclude must be array [OK]
- Using string instead of array for exclude
- Thinking ES6 is invalid target
- Confusing include and exclude types
app folder but exclude test files ending with .spec.ts. Which tsconfig.json configuration achieves this?Solution
Step 1: Understand include and exclude patterns
To compile files inapp, includeapp/**/*.ts. To exclude test files ending with.spec.ts, exclude**/*.spec.tsglobally.Step 2: Evaluate options
{ "include": ["app/**/*.ts"], "exclude": ["app/*.spec.ts"] } excludes only top-level test files directly inapp, missing those in subfolders. { "include": ["app/**/*.ts", "!app/**/*.spec.ts"] } uses negation in include, which is invalid. { "include": ["app/**/*.ts"], "exclude": ["app/**/*.test.ts"] } excludes.test.tsfiles, not.spec.ts.Step 3: Choose best practice
{ "include": ["app/**/*.ts"], "exclude": ["**/*.spec.ts"] } correctly includes all.tsfiles in app and excludes all.spec.tsfiles anywhere, ensuring test files are excluded.Final Answer:
Include app/**/*.ts and exclude **/*.spec.ts -> Option AQuick Check:
exclude **/*.spec.ts filters test files [OK]
- Using negation inside include array
- Excluding wrong test file patterns
- Not excluding test files at all
Ready to prove it?
Pass the test and this pattern moves unknown
