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.
Create a deeply mutable version of an
object/ReadonlyMap/ReadonlySet/ReadonlyArraytype. The inverse ofReadonlyDeep<T>. UseWritable<T>if you only need one level deep.This can be used to store and mutate options within a class, edit
readonlyobjects within tests, construct areadonlyobject 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.