Migration की Strategy
In this page:
// tsconfig.json
{
"compilerOptions": {
"allowJs": true,
"checkJs": false
}
}
Core Concept
एक JavaScript codebase को TypeScript में migrate करना incrementally, file by file, सबसे अच्छा काम करता है, एक बड़े rewrite की कोशिश करने के बजाय — TypeScript JavaScript का एक superset है, इसलिए एक .js file rename होकर .ts बनते ही valid TypeScript source बन जाती है।
उदाहरण: Core Concept
// utils.js -> utils.ts is valid the moment you rename it.
console.log("TypeScript is a JS superset -- migrate incrementally, file by file");
Basic Setup
एक basic setup पहले tsconfig.json में "allowJs": true और "checkJs": false enable करता है, जिससे .js और .ts files किसी भी type errors enforce होने से पहले साथ exist और compile हो सकें।
उदाहरण: Basic Setup
// tsconfig.json: { "compilerOptions": { "allowJs": true, "checkJs": false } }
console.log("allowJs lets .js and .ts coexist before enforcing any type errors");
Typed Example
एक typed example of the usual path: एक low-dependency utility file को .ts में rename करें, TypeScript immediately जो handful of type errors flag करे उन्हें fix करें, फिर उन files की ओर बढ़ें जो इसे import करती हैं, dependency graph की leaves से outward काम करते हुए।
उदाहरण: Typed Example
// Step 1: rename a low-dependency utils.js to utils.ts, fix its errors.
// Step 2: move outward to files that import it.
function double(x: number): number { return x * 2; }
console.log(double(5));
Project Usage
एक असली project में, teams अक्सर migration को शुरू में "strict": false से gate करती हैं, फिर individual strict-mode flags (noImplicitAny, strictNullChecks, आदि) को एक-एक करके on करती हैं जैसे codebase का type coverage improve होता है।
उदाहरण: Project Usage
// tsconfig.json starts with { "strict": false }, flags enabled one at a time.
console.log("Teams flip noImplicitAny, strictNullChecks, etc. incrementally");
Best Practices
Migration progress को "percentage of files that are .ts" जैसी एक metric या एक strictness-adoption dashboard से track करें, क्योंकि एक बड़े codebase पर all-or-nothing migration की कोशिश शायद ही कभी finish होती है।
उदाहरण: Best Practices
// Track "% of files that are .ts" as a migration progress metric.
console.log("An all-or-nothing migration attempt on a large codebase rarely finishes");
- पूरे codebase को एक commit में convert करने की कोशिश करना, जब migration file by file आसान है।
strict: trueimmediately on करना और errors में डूब जाना।- हर जगह
anyसे errors silence करना, इसलिए project में TypeScript syntax है लेकिन कोई असली safety नहीं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: