tsconfig.json के Basics
In this page:
{
"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
// 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
// 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
// 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
// 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
// tsconfig.json
// {
// "include": ["src/**/*"],
// "exclude": ["node_modules", "dist"]
// }
console.log("include/exclude use glob patterns to select project files");
"strict": trueजैसी compiler settings top level पर रखनाcompilerOptionsके अंदर रखने के बजाय, इसलिए वे ignore हो जाती हैं।- Trailing commas या comments छोड़ना जहां editor complain करे, या single quotes लिखना, जो tsconfig को कई tools के लिए invalid JSON बना देता है।
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: