compilerOptions sets how TypeScript compiles your code.
Examples
This sets the JavaScript version to ES5 for compatibility with older browsers.
Typescript
{
"compilerOptions": {
"target": "es5"
}
}
This tells TypeScript to only compile files inside the app folder.
Typescript
{
"include": ["app/**/*.ts"]
}
This excludes the tests folder from compilation.
Typescript
{
"exclude": ["tests"]
}
Sample Program
This example shows a tsconfig.json that compiles all TypeScript files in src folder to ES6 JavaScript in the dist folder. The code prints a greeting message.
2. Which of the following is the correct way to specify the JavaScript version target in tsconfig.json?
easy
A. {
"target": "ES6"
}
B. {
"compilerOptions": {
"target": "ES6"
}
}
C. {
"compilerOptions": {
"version": "ES6"
}
}
D. {
"compilerOptions": {
"jsVersion": "ES6"
}
}
Solution
Step 1: Identify correct property for target
The correct property inside compilerOptions to set JavaScript version is target.
Step 2: Check JSON structure
The snippet with target nested inside compilerOptions is correct. The one with target directly at root misses the compilerOptions wrapper. The ones using version or jsVersion have wrong property names.
Final Answer:
{
"compilerOptions": {
"target": "ES6"
}
} -> Option B
Quick Check:
target inside compilerOptions = correct [OK]
Hint: Always put target inside compilerOptions [OK]
Common Mistakes:
Placing target outside compilerOptions
Using wrong property names like version or jsVersion
The exclude property must be an array of strings, but here it is a single string.
Step 2: Validate other properties
target with "ES6" is valid, outDir can be any folder name, and include is correctly an array.
Final Answer:
"exclude" should be an array, not a string -> Option C
Quick Check:
exclude must be array [OK]
Hint: exclude always needs brackets [] [OK]
Common Mistakes:
Using string instead of array for exclude
Thinking ES6 is invalid target
Confusing include and exclude types
5. You want to compile only TypeScript files inside the app folder but exclude test files ending with .spec.ts. Which tsconfig.json configuration achieves this?
hard
A. {
"include": ["app/**/*.ts"],
"exclude": ["**/*.spec.ts"]
}
B. {
"include": ["app/**/*.ts", "!app/**/*.spec.ts"]
}
C. {
"include": ["app/**/*.ts"],
"exclude": ["app/**/*.test.ts"]
}
D. {
"include": ["app/**/*.ts"],
"exclude": ["app/*.spec.ts"]
}
Solution
Step 1: Understand include and exclude patterns
To compile files in app, include app/**/*.ts. To exclude test files ending with .spec.ts, exclude **/*.spec.ts globally.
Step 2: Evaluate options
{
"include": ["app/**/*.ts"],
"exclude": ["app/*.spec.ts"]
} excludes only top-level test files directly in app, 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.ts files, not .spec.ts.
Step 3: Choose best practice
{
"include": ["app/**/*.ts"],
"exclude": ["**/*.spec.ts"]
} correctly includes all .ts files in app and excludes all .spec.ts files anywhere, ensuring test files are excluded.
Final Answer:
Include app/**/*.ts and exclude **/*.spec.ts -> Option A
Quick Check:
exclude **/*.spec.ts filters test files [OK]
Hint: Use exclude with **/*.spec.ts to skip test files [OK]
Common Mistakes:
Using negation inside include array
Excluding wrong test file patterns
Not excluding test files at all
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.