{
  "id": 3041757,
  "title": "Write your custom generic utility in TypeScript",
  "url": "https://urgent.news/2026/08/24/write-your-custom-generic-utility-in-typescript",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-08-24T14:27:10.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/thatonevikash/write-your-custom-generic-utility-in-typescript-5f16"
  },
  "original_language": "en",
  "account": "I recently discovered that I was writing the same TypeScript type transformation in multiple places. My name is Vikash Kumar. Initially, I was just writing the transformation inline. However, I soon realized that this pattern could be turned into a reusable utility. So, I created the following code:\n\nexport type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;\n\nThis simple utility has turned out to be surprisingly useful. Let's consider a configuration type:\n\ntype Config = {\nname: string;\nversion: string;\ndescription: string;\n};\n\nTypically, TypeScript expects all three properties. However, in a specific function, the description might be optional. In such cases, we could create a new type manually:\n\ntype PartialConfig = {\nname: string;\nversion: string;\ndescription?: string;\n};\n\nWhile this works, it results in duplicated type definitions. As the original type grows larger, this duplication becomes increasingly annoying. That's where the Optional utility comes in handy.\n\nBy using the Optional utility, we can simplify the process:\n\ntype PartialConfig = Optional<Config, 'description'>;\n\nNow, TypeScript understands that in the PartialConfig type, the name and version properties remain required, while the description becomes optional. No duplicate type definitions are needed, and there's no manual rewriting involved.\n\nUnder the hood, the Optional utility combines three TypeScript utilities: Omit, Pick, and Partial. Let's break it down:\n\n1. Pick<T, K> selects the properties we want to keep. For example:\nPick<Config, 'description'> becomes { description: string; }\n\n2. Partial<Pick<T, K>> makes the selected properties optional:\nPartial<Pick<Config, 'description'>> becomes { description?: string; }\n\n3. Omit<T, K> keeps everything except the properties we want to exclude:\nOmit<Config, 'description'> becomes { name: string; version: string; }\n\nCombining these three utilities, we get:\nOmit<Config, 'description'> & Partial<Pick<Config, 'description'>> which simplifies to { name: string; version: string; description?: string; }\n\nThis utility proves particularly useful when working with APIs, configuration objects, constructors, and update functions. For instance, consider an update function for a User type:\n\ntype User = {\nid: string;\nname: string;\nemail: string;\nrole: 'admin' | 'user';\n};\n\nWhen updating a user, we might not want to require every property. However, we still want the id to be required:\n\ntype UserUpdate = Optional<Pick<User, 'id' | 'name' | 'email' | 'role'>, 'name' | 'email' | 'role'>;\n\nAs a result, the UserUpdate type is now defined as:\n{ id: string; role: 'admin' | 'user'; }\n\nThis concise and reusable Optional utility saves time and effort by eliminating the need for manual type duplication and rewriting.",
  "summary": "I recently found myself writing the same kind of TypeScript type transformation in a few different places. Hello, I'm Vikash Kumar . I had an existing type where most properties were required, but for one particular use case, I wanted some of them to become optional. At first, I was just writing the transformation inline. Then I realized: Why not turn the pattern into a reusable utility? So I…",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}