TypeScript Best Practices की Review
In this page:
Core Concept
एक TypeScript codebase की best-practices review आमतौर पर any के उपयोग (हर एक type safety में एक gap है) जांचती है, क्या strict mode enabled है, और क्या types sensibly colocated हैं बजाय files के across duplicate होने के।
उदाहरण: Core Concept
// Checklist: grep for ": any", check strict mode, check for duplicated types.
function search(term: string): any { return term; } // flagged in a review
console.log(search("x"));
Basic Setup
एक basic review process strict: true temporarily enabled करके tsc --noEmit run करता है (भले ही project इसे day-to-day उपयोग न करे) यह सामने लाने के लिए कि current configuration के नीचे कितने latent type issues मौजूद हैं।
उदाहरण: Basic Setup
// tsc --noEmit --strict (run temporarily even if not the project default)
console.log("Reveals latent type issues hidden beneath the current config");
Typed Example
एक common finding का typed example: एक function जो SomeType | null return करने के लिए typed है जहां हर असली call site तुरंत result!.property (एक non-null assertion) करती है आमतौर पर signal करती है कि type को सिर्फ assert away करने के बजाय properly narrow किया जाना चाहिए।
उदाहरण: Typed Example
interface Result { value: string; }
function find(id: number): Result | null {
return id === 1 ? { value: "found" } : null;
}
const result = find(2);
// result!.value is a red flag -- prefer narrowing instead:
if (result) console.log(result.value);
Project Usage
एक असली project में, एक periodic review overly broad types (object, Function, any[]) भी जांचती है जो technically compile होते हैं लेकिन बहुत कम असली safety देते हैं, उन्हें specific interfaces या generics से replace करते हुए।
उदाहरण: Project Usage
// Flag overly broad types found during review:
function process(data: object) { // too broad
console.log(data);
}
interface Payload { id: number; } // replace with something specific
process({ id: 1 } satisfies Payload);
Best Practices
CI में समय के साथ एक metric जैसे "any और @ts-ignore occurrences की count" track करें, क्योंकि एक बढ़ता trend आमतौर पर मतलब होता है कि type debt चुकाए जाने से ज़्यादा तेज़ी से जमा हो रहा है।
उदाहरण: Best Practices
// CI metric: count of ": any" and "@ts-ignore" occurrences over time
let anyCount = 0;
let tsIgnoreCount = 0;
console.log("Track these counts in CI to catch rising type debt");
- सिर्फ
strictपर भरोसा करना और मौजूदाanytypes को नज़रअंदाज़ करना जो इसे bypass करते हैं। - एक
T | nullreturn type को हर call site पर!जोड़कर fix करना बजाय null handle करने के। objectयाFunctionजैसे overly broad types उपयोग करना जो compile होते हैं लेकिन लगभग कुछ भी allow करते हैं।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Todo App with TypeScript
- REST API with Express + TypeScript
- React Dashboard with TypeScript
- CLI Tool with TypeScript
- Library with TypeScript
- Full Stack TypeScript App
- TypeScript Design System
- TypeScript Monorepo Project
- Authentication System
- Real-time App with Socket.io
- GraphQL API with TypeScript
- Microservices with TypeScript
- TypeScript Best Practices Review
- What to Learn Next