WritableDeep: T extends BuiltIns
    ? T
    : T extends (...arguments: any[]) => unknown
        ? {} extends WritableObjectDeep<T>
            ? T
            : HasMultipleCallSignatures<T> extends true
                ? T
                : (...arguments: Parameters<T>) => ReturnType<T> & WritableObjectDeep<T>
        : T extends Readonly<ReadonlyMap<infer KeyType, infer ValueType>>
            ? WritableMapDeep<KeyType, ValueType>
            : T extends Readonly<ReadonlySet<infer ItemType>>
                ? WritableSetDeep<ItemType>
                : T extends object ? WritableObjectDeep<T> : unknown

Create a deeply mutable version of an object/ReadonlyMap/ReadonlySet/ReadonlyArray type. The inverse of ReadonlyDeep<T>. Use Writable<T> if you only need one level deep.

This can be used to store and mutate options within a class, edit readonly objects within tests, construct a readonly object within a function, or to define a single model where the only thing that changes is whether or not some of the keys are writable.

Type Parameters

  • T
import type {WritableDeep} from 'type-fest';

type Foo = {
readonly a: number;
readonly b: readonly string[]; // To show that mutability is deeply affected.
readonly c: boolean;
};

const writableDeepFoo: WritableDeep<Foo> = {a: 1, b: ['2'], c: true};
writableDeepFoo.a = 3;
writableDeepFoo.b[0] = 'new value';
writableDeepFoo.b = ['something'];

Note that types containing overloaded functions are not made deeply writable due to a TypeScript limitation.

Writable