---
title: "Advanced TypeScript Utility Patterns"
publishDate: "2024-11-01"
topic: "TypeScript"
tags: ["TypeScript", "Generics", "TypeLevel"]
canonicalUrl: "https://sahillangoo.in/notes/typescript-utility-patterns/"
---

# Advanced TypeScript Utility Patterns

> Compact snippet for creating deeply immutable types and type-safe key transformations.

- **Published**: 2024-11-01
- **Topic**: TypeScript
- **Tags**: TypeScript, Generics, TypeLevel

When building robust design systems or API clients, you often need deep immutability:

```typescript
type DeepReadonly<T> = T extends Function | boolean | number | string | null | undefined
  ? T
  : T extends Array<infer U>
    ? ReadonlyArray<DeepReadonly<U>>
    : T extends Map<infer K, infer V>
      ? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>>
      : T extends Set<infer M>
        ? ReadonlySet<DeepReadonly<M>>
        : { readonly [P in keyof T]: DeepReadonly<T[P]> };
```

This prevents accidental mutation in deeply nested configuration objects.
