← Back to TypeScript Course | Chapter 28: Real World Projects | Lesson 13 of 14

TypeScript Best Practices की Review

एक best-practices review compiler settings, type design, project structure, error handling, testing, और maintainability जांचता है। Goal अपने आप में types के लिए types नहीं बल्कि useful types हैं।

Core Concept

एक TypeScript codebase की best-practices review आमतौर पर any के उपयोग (हर एक type safety में एक gap है) जांचती है, क्या strict mode enabled है, और क्या types sensibly colocated हैं बजाय files के across duplicate होने के।

उदाहरण: Core Concept

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

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

typescript
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

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

typescript
// 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");
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. सिर्फ strict पर भरोसा करना और मौजूदा any types को नज़रअंदाज़ करना जो इसे bypass करते हैं।
  2. एक T | null return type को हर call site पर ! जोड़कर fix करना बजाय null handle करने के।
  3. object या Function जैसे overly broad types उपयोग करना जो compile होते हैं लेकिन लगभग कुछ भी allow करते हैं।

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.