Migration की सामान्य समस्याएँ
In this page:
// 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
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
// 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
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
// 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
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");
- Function parameters untyped छोड़ना, जैसे
function process(value), जो silentlyanyबन जाते हैं जब तकnoImplicitAnyon न हो। - हर error पर
// @ts-ignoreयाas anyजोड़ना underlying types fix करने के बजाय। - एक JavaScript library import करना जिसमें कोई types नहीं और
Could not find a declaration fileमिलना, जब आपको@types/<package>या एकdeclare moduleline चाहिए।
- एक migration strategy आपको एक JavaScript project को TypeScript में step by step move करने देती है।
allowJsऔरcheckJsoptions और 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: