Back to all snippets
Library/TypeScript/Utility Types
typescriptadvancedtypesutilitiesgenerics

How to implement Utility Types in Typescript

Useful TypeScript utility type definitions

Quick Answer

TypeScript utility types like `Partial<T>`, `Pick<T,K>`, and `Omit<T,K>` derive new types from existing ones — eliminating manual interface duplication.

Code Snippet

1// Make all properties optional recursively
2type DeepPartial<T> = {
3  [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
4};
5
6// Make specific keys required
7type RequireKeys<T, K extends keyof T> = T & Required<Pick<T, K>>;
8
9// Extract keys of specific type
10type KeysOfType<T, U> = {
11  [K in keyof T]: T[K] extends U ? K : never;
12}[keyof T];

What is Utility Types?

TypeScript ships with useful built-in utility types (Partial, Readonly, Pick, etc.) but some common patterns require custom implementations. These helpers — DeepPartial for recursive optional fields, RequireKeys for making specific fields required, and KeysOfType for filtering keys by value type — cover the most frequently needed advanced scenarios.

How It Works

  1. 1`DeepPartial<T>` recursively applies `?` to every property at every nesting level.
  2. 2`RequireKeys<T, K>` intersects T with `Required<Pick<T, K>>` to mandate specific fields.
  3. 3`KeysOfType<T, U>` maps over keys and returns `never` for keys whose value type does not extend U, then indexes to collect the matching keys.

Common Use Cases

  • Form state - Use DeepPartial for partial form models with nested objects
  • API updates - Require certain fields when constructing PATCH payloads
  • Config objects - Filter config keys that accept boolean values
  • Generic utilities - Build type-safe helper functions with conditional types

Key Benefits

  • Eliminates repetitive type duplication across your codebase
  • Works at the type level with zero runtime cost
  • Fully composable with built-in TypeScript utility types
  • Improves IDE autocomplete and compile-time safety

Common Mistakes to Avoid

  • Using `Partial` when you actually need a discriminated union — Partial allows all fields absent simultaneously, which may represent an invalid state.
  • Applying `DeepPartial` to types with circular references — this can cause infinite type instantiation errors.
  • Forgetting that `RequireKeys` still allows the non-required keys to be absent — they stay as they were defined on T.

Quick Tips

  • Click the "Copy" button above to copy the code to your clipboard
  • This code is production-ready and can be used in your projects immediately
  • Check out related snippets below for more typescript examples

Frequently Asked Questions

What is the difference between Pick and Omit?

`Pick<T, K>` whitelists the keys you want to keep. `Omit<T, K>` blacklists the keys you want to remove. Use Pick when the keep-list is short; use Omit when the remove-list is short.

About This Typescript Code Snippet

This free typescript code snippet for utility types is production-ready and copy-paste friendly. Whether you are building a web app, API, or frontend interface, this advanced-level example will help you implement utility types quickly and correctly.

All snippets in the Snippetly library follow typescript best practices and are tested for real-world use. You can adapt this code to work with React, Vue, Node.js, or any project that uses typescript.

Tags: types, utilities, generics  | Language: typescript  | Difficulty: advanced  | Category: TypeScript

Build Your Own Snippet Library

Organise your team's code snippets with Snippetly. Share knowledge and boost productivity across your organisation.