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

TypeScript का Design System

एक design system component props, tokens, variants, और reusable UI contracts define करने के लिए TypeScript उपयोग कर सकता है। Strong types components को consistent रखने में help करते हैं।
Syntax
typescript
export interface ComponentProps {
  variant: "optionA" | "optionB";
  onClick: () => void;
}

Core Concept

एक typed design system reusable UI component props को TypeScript interfaces के रूप में define करता है, इसलिए एक Button या Card component का हर consumer यह compile-time enforcement पाता है कि exactly कौन से props valid और required हैं।

उदाहरण: Core Concept

typescript
interface ButtonProps {
  variant: "primary" | "secondary";
  onClick: () => void;
}
const props: ButtonProps = { variant: "primary", onClick: () => console.log("clicked") };
console.log(props);

Basic Setup

एक basic setup हर component को इसके typed props interface के साथ export करता है (export interface ButtonProps { variant: primary | secondary; onClick: () => void }), एक अपने versioned package के रूप में published।

उदाहरण: Basic Setup

typescript
export interface ButtonProps {
  variant: "primary" | "secondary";
  onClick: () => void;
}
console.log("Component and its typed props interface exported together");

Typed Example

एक typed example: variant: primary | secondary | danger जैसा एक string-literal union उपयोग करने का मतलब है कि variant="primry" (एक typo) pass करना एक compile error है, production में एक silently-unstyled button नहीं।

उदाहरण: Typed Example

typescript
type Variant = "primary" | "secondary" | "danger";
function applyVariant(variant: Variant) {
  return `btn-${variant}`;
}
console.log(applyVariant("danger"));
// applyVariant("primry") would be a compile error, not a silent style bug

Project Usage

एक असली project में, एक design system के typed props वह हैं जो दर्जनों feature teams को shared components confidently consume करने देते हैं, क्योंकि compiler — एक style guide document नहीं — correct usage enforce करता है।

उदाहरण: Project Usage

typescript
interface CardProps {
  title: string;
  footer?: string;
}
function renderCard(props: CardProps) {
  return `${props.title}${props.footer ? " - " + props.footer : ""}`;
}
console.log(renderCard({ title: "Stats" }));

Best Practices

अपने design system की typed API को deliberately version करें और breaking prop changes को एक changelog में document करें, क्योंकि एक prop type change हर consumer के लिए एक breaking change है भले ही runtime behavior similar दिखे।

उदाहरण: Best Practices

typescript
// CHANGELOG.md: "v2.0.0: ButtonProps.variant no longer accepts 'default'"
interface ButtonProps {
  variant: "primary" | "secondary";
}
console.log("Breaking prop changes documented in a changelog");
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. variant को string के रूप में type करना एक literal union के बजाय, इसलिए "primry" accepted हो जाता है।
  2. Props interface export न करना, इसलिए consumers इसे reuse या extend नहीं कर सकते।
  3. बहुत ज़्यादा props को optional बनाना, इसलिए components ऐसे तरीकों से उपयोग हो सकते हैं जो कभी designed नहीं थे।

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.