← Back to TypeScript Course | Chapter 4: Interfaces | Lesson 1 of 7

Interfaces परिभाषित करना

TypeScript में एक interface एक object की shape को उसके properties और उनके types specify करके describe करता है। Interfaces object-based code को समझना, check करना, और maintain करना आसान बनाते हैं।
Syntax
typescript
interface InterfaceName {
  property1: type;
  property2: type;
  method(param: type): returnType;
}

Creating a Basic Interface

interface keyword, एक नाम और property definitions के एक block के बाद, वह required shape define करता है जो एक object को उस interface को satisfy करने के लिए होनी चाहिए।

उस interface type के एक variable को assign किया गया कोई भी object interface declare करने वाली हर property include करना चाहिए।

उदाहरण: Creating a Basic Interface

typescript
interface User {
  name: string;
  age: number;
}
const user: User = { name: "Farah", age: 27 };
console.log(user);

Multiple Properties

एक interface ज़रूरत के हिसाब से कितनी भी properties list कर सकता है, हर एक अपने type के साथ, एक complex object की shape को पूरी तरह describe करने के लिए। यह एक single, centralized definition देता है जिसे codebase के कई parts उसी object shape को बार-बार दोहराने के बजाय reference कर सकते हैं।

उदाहरण: Multiple Properties

typescript
interface Product {
  id: number;
  name: string;
  price: number;
  inStock: boolean;
}
const item: Product = { id: 1, name: "Mug", price: 8, inStock: true };
console.log(item);

Interfaces with Functions

Interfaces function-typed properties को उतनी ही आसानी से describe कर सकते हैं जितना ordinary data properties, एक object की shape को onClick: () => void जैसे callable members include करने देते हुए।

यह callback handlers include करने वाले configuration objects describe करने के लिए common है।

उदाहरण: Interfaces with Functions

typescript
interface Button {
  label: string;
  onClick: () => void;
}
const btn: Button = { label: "Save", onClick: () => console.log("Saved!") };
btn.onClick();

Interfaces with Arrays

एक interface उस structure को describe कर सकता है जो एक array में हर object को share करना चाहिए, commonly User[] जैसी array typing के साथ paired जहां User वह interface है।

यह ensure करता है कि collection में हर single item, सिर्फ array खुद नहीं, वही shape के against checked हो।

उदाहरण: Interfaces with Arrays

typescript
interface User {
  name: string;
}
const users: User[] = [{ name: "Ken" }, { name: "Lia" }];
console.log(users);

Using Interfaces in Functions

Interfaces अक्सर function parameter types के रूप में सीधे उपयोग होते हैं, जो exactly document करता है कि एक function किस shape के object की उम्मीद receive करने की करता है।

यह function signatures को self-explanatory रखता है और compiler को किसी भी required property की कमी वाले object को reject करने देता है।

उदाहरण: Using Interfaces in Functions

typescript
interface User {
  name: string;
  age: number;
}
function printUser(user: User) {
  console.log(`${user.name} (${user.age})`);
}
printUser({ name: "Omar", age: 22 });
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. एक interface के object बनाते समय एक required property छोड़ देना, जैसे User से age omit करना।
  2. एक object literal में एक extra property जोड़ना जिसे interface declare नहीं करता, जो एक excess property के रूप में flag होती है।
  3. यह उम्मीद करना कि interface runtime पर मौजूद है, उदाहरण के लिए x instanceof User check करना, जब interfaces erased होते हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.