Skip to main content
← Gists
typescript Feb 19, 2026

DeepReadonly for Configuration

Freeze nested objects at the type level.

Use DeepReadonly when you want config objects to be immutable end-to-end.

Type

deep-readonly.ts
type DeepReadonly<T> = T extends Function
? T
: T extends object
  ? { readonly [K in keyof T]: DeepReadonly<T[K]> }
  : T

type Config = {
api: {
  baseUrl: string
  timeoutMs: number
}
features: string[]
}

type ReadonlyConfig = DeepReadonly<Config>