What if one simple file could make your whole TypeScript project compile perfectly every time?
Why tsconfig.json Configuration Basics in Typescript? - Purpose & Use Cases
Imagine you have a big TypeScript project with many files. You want to compile them all, but you have to run the compiler separately for each file and remember all the settings every time.
This manual way is slow and tiring. You might forget important options or make mistakes in commands. It becomes hard to keep your project consistent and working well.
The tsconfig.json file lets you save all your compiler settings in one place. Now, you just run the compiler once, and it knows exactly what to do for your whole project.
tsc file1.ts tsc file2.ts --target ES6 --strict
tsc
// uses settings from tsconfig.json automaticallyWith tsconfig.json, you can easily manage and share your TypeScript project settings, making your work faster and less error-prone.
When working on a team, everyone uses the same tsconfig.json file so the code compiles the same way on every computer without confusion.
Save time: No need to type long commands repeatedly.
Stay consistent: Everyone uses the same settings.
Reduce errors: Avoid forgetting important compiler options.
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
