← Back to TypeScript Course | Chapter 27: Migration from JavaScript | Lesson 1 of 6

Migration की Strategy

एक JavaScript project हर file को एक साथ convert करने के बजाय gradually TypeScript में migrate हो सकता है। एक staged approach risk कम करता है और team को incrementally सीखने देता है।
Syntax
typescript
// 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

typescript
// 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

typescript
// 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

typescript
// 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

typescript
// 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

typescript
// 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");
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. पूरे codebase को एक commit में convert करने की कोशिश करना, जब migration file by file आसान है।
  2. strict: true immediately on करना और errors में डूब जाना।
  3. हर जगह any से errors silence करना, इसलिए project में TypeScript syntax है लेकिन कोई असली safety नहीं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.