Utility Types में Omit
In this page:
type Result = Omit<TypeName, "key1" | "key2">;
Basic Omit
Omit<T, K>, K में named properties हटाता है जबकि T की हर दूसरी property intact रखता है, effectively Pick<T, K> का inverse operation। यह एक otherwise बड़े, mostly-reusable type से एक या दो fields strip करने के लिए खासतौर पर काम का है।
उदाहरण: Basic Omit
interface User {
id: number;
name: string;
password: string;
}
type PublicUser = Omit<User, "password">;
const u: PublicUser = { id: 1, name: "Ravi" };
console.log(u);
Omit for Input Objects
Omit ऐसे input types बनाने के लिए उपयोगी है जो generated या protected fields exclude करें — उदाहरण के लिए एक नया record create करने के लिए उपयोग होने से पहले एक type से एक auto-generated id हटाना।
उदाहरण: Omit for Input Objects
interface User {
id: number;
name: string;
}
type NewUser = Omit<User, "id">;
const newUser: NewUser = { name: "Ravi" };
console.log(newUser);
Omit Multiple Properties
Omit keys का एक union supply करके एक साथ कई properties हटा सकता है, जैसे Omit<User, id | createdAt>, बजाय एक समय में सिर्फ एक property हटाने में सक्षम होने के।
उदाहरण: Omit Multiple Properties
interface User {
id: number;
createdAt: string;
name: string;
}
type CreateUserInput = Omit<User, "id" | "createdAt">;
const input: CreateUserInput = { name: "Ravi" };
console.log(input);
Omit with Functions
Omit precise function parameter types बना सकता है उन fields को हटाकर जिनकी एक function को ज़रूरत नहीं या जिन्हें accept नहीं करना चाहिए, function की signature को पूरे original type से आगे tighten करते हुए।
उदाहरण: Omit with Functions
interface Task {
id: number;
title: string;
done: boolean;
}
function createTask(task: Omit<Task, "id" | "done">) {
console.log("Creating task:", task.title);
}
createTask({ title: "Write code" });
When to Use Omit
Omit उपयोग करें जब एक नए type में एक existing type का लगभग सब कुछ होना चाहिए सिवाय properties की एक छोटी, specific संख्या के जो उस particular context में नहीं belong करती, जैसे एक नया record create करने के लिए data वापस भेजने से पहले server-generated fields हटाना।
उदाहरण: When to Use Omit
interface Record_ {
id: number;
createdAt: string;
name: string;
}
type NewRecord = Omit<Record_, "id" | "createdAt">;
const r: NewRecord = { name: "Ravi" };
console.log(r);
- यह मान लेना कि
Omit<User, "passwrd">typos पकड़ता है, जबOmitकोई भी string keys accept करता है और type को बस unchanged छोड़ देता है। - Omit को एक union type पर उपयोग करना, जहां यह union को common properties में collapse कर देता है।
- यह सोचना कि Omit property को runtime पर हटाता है, जब असली object में अब भी यह मौजूद है।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- TypeScript Advanced Types
- Mapped Types
- Conditional Types
- Custom Type Guards
- Assertion Functions
- Control Flow Analysis
- Exhaustiveness Checking
- Satisfies Operator
- Template Literal Types
- Utility Types - Partial
- Utility Types - Required
- Utility Types - Readonly
- Utility Types - Pick
- Utility Types - Omit
- Utility Types - Record
- Utility Types - Exclude and Extract
- Utility Types - NonNullable
- Utility Types - ReturnType
- Utility Types - Parameters
- TypeScript 5 Updates