Typescript DeepReadOnly

A nice utility types for typescript

  • Tags: Web Development
  • Last updated: 

export type DeepReadonly<T> = T extends Primitive
    ? T
    : T extends Array<infer U>
      ? DeepReadonlyArray<U>
      : DeepReadonlyObject<T>;

type Primitive = string | number | boolean | undefined | null;

interface DeepReadonlyArray<T> extends ReadonlyArray<DeepReadonly<T>> {}

type DeepReadonlyObject<T> = {
    readonly [P in keyof T]: DeepReadonly<T[P]>;
};

Credits: https://www.builder.io/blog/utility-types