← Back to TypeScript Course | Chapter 8: Advanced Types | Lesson 14 of 20

Utility Types में Omit

Omit<T, K> दूसरे type से selected properties हटाकर एक नया type बनाता है। यह उपयोगी है जब कुछ fields expose या supply नहीं होने चाहिए।
Syntax
typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
interface Record_ {
  id: number;
  createdAt: string;
  name: string;
}
type NewRecord = Omit<Record_, "id" | "createdAt">;
const r: NewRecord = { name: "Ravi" };
console.log(r);
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. यह मान लेना कि Omit<User, "passwrd"> typos पकड़ता है, जब Omit कोई भी string keys accept करता है और type को बस unchanged छोड़ देता है।
  2. Omit को एक union type पर उपयोग करना, जहां यह union को common properties में collapse कर देता है।
  3. यह सोचना कि Omit property को runtime पर हटाता है, जब असली object में अब भी यह मौजूद है।

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.