import type {HasMultipleCallSignatures} from './has-multiple-call-signatures.d.ts'; import type {CallSignatureOf, IsArrayOfUnknownLength, NonRecursiveLeaf, NonRecursiveType} from './internal/index.d.ts'; import type {ApplyDefaultOptions} from './apply-default-options.d.ts'; import type {IsNever} from './is-never.d.ts'; import type {ReadonlyDate} from './readonly-date.d.ts'; import type {Writable} from './writable.d.ts'; // For `@link` /** @see {@link WritableDeep} */ export type WritableDeepOptions = { /** The types to keep as is, instead of making deeply writable. A kept type is left untouched at every depth, including everything inside it. Matching uses assignability. @default never @example ``` import type {WritableDeep} from 'type-fest'; type Color = {readonly red: number}; type Settings = {readonly name: string; readonly color: Color}; type WritableSettings = WritableDeep; //=> {name: string; color: Color} ``` */ readonly keep?: unknown; }; /** Create a deeply mutable version of an `object`/`ReadonlyMap`/`ReadonlySet`/`ReadonlyArray` type. The inverse of `ReadonlyDeep`. Use `Writable` if you only need one level deep. This can be used to [store and mutate options within a class](https://github.com/sindresorhus/pageres/blob/4a5d05fca19a5fbd2f53842cbf3eb7b1b63bddd2/source/index.ts#L72), [edit `readonly` objects within tests](https://stackoverflow.com/questions/50703834), [construct a `readonly` object within a function](https://github.com/Microsoft/TypeScript/issues/24509), or to define a single model where the only thing that changes is whether or not some of the keys are writable. A {@link ReadonlyDate} becomes a `Date`. @example ``` 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 = {a: 1, b: ['2'], c: true}; writableDeepFoo.a = 3; writableDeepFoo.b[0] = 'new value'; writableDeepFoo.b = ['something']; ``` Pass the types to keep as is through the `keep` option to leave matching types untouched at every depth, like a class instance whose properties and methods should not be made writable. Matching uses assignability, so a plain object type that structurally matches a kept type is kept too, and a wide type, like `object`, keeps every object: @example ``` import type {WritableDeep} from 'type-fest'; class Color { get r(): number { return 0; } get g(): number { return 0; } } declare const readonlyTheme: { readonly name: string; readonly background: Color; }; declare const writableTheme: WritableDeep; writableTheme.name = 'solarized'; // OK // @ts-expect-error writableTheme.background.r = 3; // Error: `Color` is kept as is, so its properties stay readonly ``` Overloaded functions, including their properties, are kept as is due to a [TypeScript limitation](https://github.com/microsoft/TypeScript/issues/29732). See {@link HasMultipleCallSignatures} for overload detection limitations. @see {@link Writable} @see {@link WritableDeepOptions} @category Object @category Array @category Set @category Map */ export type WritableDeep = _WritableDeep['keep']>; /** Internal helper for {@link WritableDeep}. */ type _WritableDeep = // A kept type is returned as is, including everything inside it. It is matched before everything else, so a kept `Map` or `Set` is not rebuilt either. Value extends Keep ? Value : Value extends NonRecursiveLeaf // A `Date` is a `ReadonlyDate` too, so it is matched first to keep its subclasses and tags. Anything else that matches `ReadonlyDate` becomes a plain `Date`. ? Value extends Date ? Value : Value extends ReadonlyDate ? Date : Value : Value extends (...arguments_: any[]) => unknown ? IsNever extends true // For functions with no properties ? Value : HasMultipleCallSignatures extends true ? Value : CallSignatureOf & _WritableObjectDeep : Value extends ReadonlyMap ? WritableMapDeep : Value extends ReadonlySet ? WritableSetDeep : Value extends readonly unknown[] ? WritableArrayDeep // These are a single value rather than a structure to recurse into, or a weak collection whose contents cannot be reached, so they are left alone and their methods are not rebuilt. : Value extends NonRecursiveType | WeakMap | WeakSet ? Value : Value extends object ? _WritableObjectDeep : unknown; /** Same as `WritableDeep`, but accepts only `Map`s as inputs. Internal helper for `WritableDeep`. */ type WritableMapDeep, Keep> = MapType extends ReadonlyMap ? Map<_WritableDeep, _WritableDeep> : MapType; // Should not happen /** Same as `WritableDeep`, but accepts only `Set`s as inputs. Internal helper for `WritableDeep`. */ type WritableSetDeep, Keep> = SetType extends ReadonlySet ? Set<_WritableDeep> : SetType; // Should not happen /** Same as `WritableDeep`, but accepts only `object`s as inputs. Internal helper for `WritableDeep`. */ export type _WritableObjectDeep = { -readonly [KeyType in keyof ObjectType]: _WritableDeep }; /** Same as `WritableDeep`, but accepts only `Array`s as inputs. Internal helper for `WritableDeep`. */ type WritableArrayDeep = // A tuple has fixed elements at either end, including an optional first element or a fixed element after a leading rest. IsArrayOfUnknownLength extends false // A homomorphic mapped type keeps the length, labels, optional elements and rest element without a recursion step per element. ? _WritableObjectDeep // An array of unknown length is rebuilt directly, because mapping it makes a recursive element type, such as `JsonValue`, infinitely deep. : Array<_WritableDeep>; export {};