Interfaces परिभाषित करना
In this page:
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
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
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
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
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
interface User {
name: string;
age: number;
}
function printUser(user: User) {
console.log(`${user.name} (${user.age})`);
}
printUser({ name: "Omar", age: 22 });
- एक interface के object बनाते समय एक required property छोड़ देना, जैसे
Userसेageomit करना। - एक object literal में एक extra property जोड़ना जिसे interface declare नहीं करता, जो एक excess property के रूप में flag होती है।
- यह उम्मीद करना कि interface runtime पर मौजूद है, उदाहरण के लिए
x instanceof Usercheck करना, जब interfaces erased होते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: