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

Migration की सामान्य समस्याएँ

Migration implicit any values, incompatible libraries, module differences, और existing JavaScript में incorrect assumptions expose कर सकता है। इन issues को समझना conversion को smoother बनाता है।
Syntax
typescript
// tsconfig.json
{
  "compilerOptions": { "noImplicitAny": true }
}

function functionName(param: ParamType): ReturnType {
    // ...
}

Core Concept

सबसे common early migration issue हर जगह implicit any है — function parameters और variables जिनका कोई inferable type न हो वे any में default होते हैं जब तक noImplicitAny on न हो, migrate करने के पूरे point को silently defeat करते हुए।

उदाहरण: Core Concept

typescript
function process(value) { // implicit any without noImplicitAny
  return value;
}
console.log(process(5));

Basic Setup

एक basic fix pattern: noImplicitAny को full strict mode से पहले ही early enable करें, क्योंकि यह वह single flag है जो एक freshly-migrated codebase में सबसे ज़्यादा "this needs an actual type" locations surface करता है।

उदाहरण: Basic Setup

typescript
// tsconfig.json: { "compilerOptions": { "noImplicitAny": true } }
console.log("noImplicitAny surfaces every location needing a real type");

Typed Example

एक classic gotcha का typed example: एक function जो कभी null return करता है और कभी एक object वह silently object shape के रूप में type होगा जब तक strictNullChecks on न हो, एक असली runtime null-check bug को तब तक छुपाते हुए जब तक यह production में crash न हो।

उदाहरण: Typed Example

typescript
function findUser(id: number): { name: string } | null {
  return id === 1 ? { name: "Ravi" } : null;
}
const user = findUser(2);
// Without strictNullChecks, user.name would compile but crash at runtime.
if (user) console.log(user.name);

Project Usage

एक असली project में, type definitions के बिना third-party libraries एक frequent blocker हैं — या तो एक @types/<package> package install करने की ज़रूरत होती है, या migration को unblock करने के लिए एक local .d.ts declaration hand-written करना पड़ता है।

उदाहरण: Project Usage

typescript
// npm install --save-dev @types/lodash
// or write a local declare module "untyped-lib" { ... } block.
console.log("Untyped third-party libraries often block migration progress");

Best Practices

हर migration-era error को silence करने के लिए // @ts-ignore छिड़कने की इच्छा का विरोध करें — हर एक वह जगह है जहां type checker ने एक असली potential bug ढूंढा, और इसे suppress करना सिर्फ problem को fix करने के बजाय defer करता है।

उदाहरण: Best Practices

typescript
function risky(value: unknown) {
  // Avoid: // @ts-ignore
  // Prefer narrowing the actual value instead of suppressing the error:
  if (typeof value === "string") console.log(value.toUpperCase());
}
risky("hello");
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. Function parameters untyped छोड़ना, जैसे function process(value), जो silently any बन जाते हैं जब तक noImplicitAny on न हो।
  2. हर error पर // @ts-ignore या as any जोड़ना underlying types fix करने के बजाय।
  3. एक JavaScript library import करना जिसमें कोई types नहीं और Could not find a declaration file मिलना, जब आपको @types/<package> या एक declare module line चाहिए।
चैप्टर सारांश
  • एक migration strategy आपको एक JavaScript project को TypeScript में step by step move करने देती है।
  • allowJs और checkJs options और JSDoc type annotations existing JavaScript files में type checking जोड़ते हैं।
  • Gradual migration आपको common migration issues handle करने में help करती है सब कुछ एक साथ rewrite किए बिना।
🔒

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.