Type alias DeepImmutable<T>

DeepImmutable<T>: T extends UnknownFunction
    ? T
    : {
        readonly [K in Keys<T>]: T[K] extends unknown
            ? DeepImmutable<T[K]>
            : T[K]
    }

A type that recursively turns the proprties within a given object type T immutable.

Type Parameters

  • T

Example

type Actual = {
a: () => 1;
x: string;
s: {
q: Nullable;
s: {
i: {
x: {
o: Maybe<Primitive>;
n: Falsy;
};
e: 'foo';
};
};
};
};

type Expected = {
readonly a: () => 1;
readonly x: string;
readonly s: {
readonly q: Nullable;
readonly s: {
readonly i: {
readonly x: {
readonly o: Maybe<Primitive>;
readonly n: Falsy;
};
readonly e: 'foo';
};
};
};
};
type T = DeepImmutable<Actual>; // T Results in: Expected