TypeScript Configuration

⚙️ TypeScript Configuration (tsconfig.json)

https://www.scaler.com/topics/images/tsconfig-json-1.webp
TypeScript Configuration is done using a file called tsconfig.json.

This file tells the TypeScript compiler how to compile your code and which rules to follow.


📄 What is tsconfig.json?

  • A JSON file placed at the root of your project

  • Controls:

    • Target JavaScript version

    • Strictness rules

    • Module system

    • Output folders

    • Type checking behavior

📌 Presence of tsconfig.json = this folder is a TypeScript project


🛠️ Create tsconfig.json

Run this command in your project folder:

tsc --init

This generates a default config file.


🧩 Basic tsconfig.json Example

{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"strict": true
}
}

🔑 Important compilerOptions (Most Used)

1️⃣ target

Defines JavaScript version output.

"target": "ES5"

Common values:

  • ES5 (old browsers)

  • ES6 / ES2015

  • ES2020

  • ESNext


2️⃣ module

Defines module system.

"module": "commonjs"

Options:

  • commonjs → Node.js

  • esnext → Modern bundlers

  • amd, umd


3️⃣ rootDir

Where your TypeScript source files live.

"rootDir": "./src"

4️⃣ outDir

Where compiled JavaScript files go.

"outDir": "./dist"

5️⃣ strict ⭐ (Highly Recommended)

Enables all strict type-checking options.

"strict": true

Includes:

  • strictNullChecks

  • noImplicitAny

  • strictFunctionTypes

  • strictBindCallApply


6️⃣ noImplicitAny

Disallows implicit any.

"noImplicitAny": true

function add(a, b) {}

7️⃣ strictNullChecks

Forces explicit handling of null & undefined.

"strictNullChecks": true

8️⃣ esModuleInterop

Improves compatibility with CommonJS modules.

"esModuleInterop": true

Used when importing libraries like Express.


9️⃣ forceConsistentCasingInFileNames

Avoids issues on case-sensitive systems.

"forceConsistentCasingInFileNames": true

🔟 skipLibCheck

Skips checking .d.ts files (faster builds).

"skipLibCheck": true

📁 Include & Exclude Files

Include

"include": ["src/**/*"]

Exclude

"exclude": ["node_modules", "dist"]

🧪 Example: Real-World Recommended Config

{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src"]
}

▶️ Compile Using Config

tsc

(TypeScript automatically reads tsconfig.json)


🔑 tsconfig.json Summary

Option Purpose
target JS version
module Module system
rootDir Source folder
outDir Output folder
strict Enable strict checks
esModuleInterop Import compatibility
include Files to compile
exclude Files to ignore

⭐ Best Practices

✔ Always use strict: true
✔ Separate src and dist
✔ Use skipLibCheck for faster builds
✔ Commit tsconfig.json to version control

You may also like...