← Back to TypeScript Course | Chapter 1: Introduction to TypeScript | Lesson 6 of 7

tsconfig.json के Basics

tsconfig.json file एक TypeScript project के लिए compiler settings store करती है। यह define कर सकती है कि कौन सी files project का हिस्सा हैं और TypeScript को उन्हें कैसे check और compile करना चाहिए।
Syntax
typescript
{
  "compilerOptions": {
    "target": "ES2020",
    "strict": true,
    "outDir": "./dist"
  },
  "include": ["src"]
}

tsconfig.json बनाना

एक tsconfig.json file एक JSON configuration file है जो एक TypeScript project की root पर placed होती है। यह TypeScript compiler को बताती है कि कौन सी files project का हिस्सा हैं और उन्हें exactly कैसे check और compile किया जाना चाहिए, ताकि हर developer और tool वही settings उपयोग करे।

उदाहरण: Creating tsconfig.json

typescript
// tsconfig.json
// {
//   "compilerOptions": { "target": "ES2020" }
// }
console.log("tsconfig.json configures the whole TypeScript project");

Compiler Options

compilerOptions property वह जगह है जहां ज़्यादातर configuration रहती है, जिसमें settings होती हैं जो type checking strictness और generated JavaScript की shape दोनों control करती हैं। Common options में target, module, strict, rootDir, और outDir शामिल हैं।

उदाहरण: Compiler Options

typescript
// tsconfig.json
// {
//   "compilerOptions": { "target": "ES2020", "module": "commonjs", "strict": true }
// }
console.log("compilerOptions controls type checking and JS output shape");

Target और Module

target option control करता है कि compiler कौन सा JavaScript language version emit करता है, जैसे ES2015 या ES2022, यह affect करते हुए कि कौन सी modern syntax ज्यों की त्यों रहती है बनाम पुराने environments के लिए transformed होती है।

module option अलग से module system control करता है, जैसे CommonJS या ESNext, जो generated output में उपयोग होता है।

उदाहरण: Target and Module

typescript
// tsconfig.json
// {
//   "compilerOptions": { "target": "ES2015", "module": "commonjs" }
// }
console.log("target picks the JS version; module picks the module format");

Strict Type Checking

strict option एक single switch है जो एक साथ stronger type-checking rules का एक बड़ा set enable करता है, जिसमें strict null checks और stricter function types शामिल हैं।

इसे on करना बहुत ज़्यादा potential mistakes पकड़ता है और नए TypeScript projects के लिए standard practice है।

उदाहरण: Strict Type Checking

typescript
// tsconfig.json
// { "compilerOptions": { "strict": true } }
let value: number | null = null;
// value.toFixed(2); // strict null checks reject this without a null check
console.log(value);

Files Include और Exclude करना

include और exclude settings control करती हैं कि compiler असल में किन files को project का हिस्सा मानता है, glob patterns उपयोग करते हुए। यह मायने रखता है जब एक project directory में generated output, test files, या node_modules हों जिन्हें कभी type-check या recompile नहीं होना चाहिए।

उदाहरण: Including and Excluding Files

typescript
// tsconfig.json
// {
//   "include": ["src/**/*"],
//   "exclude": ["node_modules", "dist"]
// }
console.log("include/exclude use glob patterns to select project files");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. "strict": true जैसी compiler settings top level पर रखना compilerOptions के अंदर रखने के बजाय, इसलिए वे ignore हो जाती हैं।
  2. Trailing commas या comments छोड़ना जहां editor complain करे, या single quotes लिखना, जो tsconfig को कई tools के लिए invalid JSON बना देता है।
  3. target को ES5 जैसी एक old value पर set करना और यह सुनकर surprised होना कि modern features downlevel हो गए, या runtime support करने से ज़्यादा नया target चुनना।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.