import type {ApplyDefaultOptions, BuiltIns, HasMultipleCallSignatures} from './internal/index.d.ts'; import type {IsNever} from './is-never.d.ts'; /** @see {@link PartialDeep} */ export type PartialDeepOptions = { /** Whether to affect the individual elements of arrays and tuples. @default false */ readonly recurseIntoArrays?: boolean; /** Allows `undefined` values in non-tuple arrays. - When set to `true`, elements of non-tuple arrays can be `undefined`. - When set to `false`, only explicitly defined elements are allowed in non-tuple arrays, ensuring stricter type checking. @default false @example You can allow `undefined` values in non-tuple arrays by passing `{recurseIntoArrays: true; allowUndefinedInNonTupleArrays: true}` as the second type argument: ``` import type {PartialDeep} from 'type-fest'; type Settings = { languages: string[]; }; declare const partialSettings: PartialDeep; partialSettings.languages = [undefined]; // OK ``` */ readonly allowUndefinedInNonTupleArrays?: boolean; }; type DefaultPartialDeepOptions = { recurseIntoArrays: false; allowUndefinedInNonTupleArrays: false; }; /** Create a type from another type with all keys and nested keys set to optional. Use-cases: - Merging a default settings/config object with another object, the second object would be a deep partial of the default object. - Mocking and testing complex entities, where populating an entire object with its keys would be redundant in terms of the mock or test. @example ``` import type {PartialDeep} from 'type-fest'; let settings = { textEditor: { fontSize: 14, fontColor: '#000000', fontWeight: 400, }, autocomplete: false, autosave: true, }; const applySavedSettings = (savedSettings: PartialDeep) => ( {...settings, ...savedSettings, textEditor: {...settings.textEditor, ...savedSettings.textEditor}} ); settings = applySavedSettings({textEditor: {fontWeight: 500}}); ``` By default, this does not affect elements in array and tuple types. You can change this by passing `{recurseIntoArrays: true}` as the second type argument: ``` import type {PartialDeep} from 'type-fest'; type Shape = { dimensions: [number, number]; }; const partialShape: PartialDeep = { dimensions: [], // OK }; partialShape.dimensions = [15]; // OK ``` @see {@link PartialDeepOptions} @category Object @category Array @category Set @category Map */ export type PartialDeep = _PartialDeep>; type _PartialDeep> = T extends BuiltIns | ((new (...arguments_: any[]) => unknown)) ? T : T extends Map ? PartialMapDeep : T extends Set ? PartialSetDeep : T extends ReadonlyMap ? PartialReadonlyMapDeep : T extends ReadonlySet ? PartialReadonlySetDeep : T extends (...arguments_: any[]) => unknown ? IsNever extends true ? T // For functions with no properties : HasMultipleCallSignatures extends true ? T : ((...arguments_: Parameters) => ReturnType) & PartialObjectDeep : T extends object ? T extends ReadonlyArray // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156 ? Options['recurseIntoArrays'] extends true ? ItemType[] extends T // Test for arrays (non-tuples) specifically ? readonly ItemType[] extends T // Differentiate readonly and mutable arrays ? ReadonlyArray<_PartialDeep> : Array<_PartialDeep> : PartialObjectDeep // Tuples behave properly : T // If they don't opt into array testing, just use the original type : PartialObjectDeep : unknown; /** Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`. */ type PartialMapDeep> = {} & Map<_PartialDeep, _PartialDeep>; /** Same as `PartialDeep`, but accepts only `Set`s as inputs. Internal helper for `PartialDeep`. */ type PartialSetDeep> = {} & Set<_PartialDeep>; /** Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`. */ type PartialReadonlyMapDeep> = {} & ReadonlyMap<_PartialDeep, _PartialDeep>; /** Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`. */ type PartialReadonlySetDeep> = {} & ReadonlySet<_PartialDeep>; /** Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`. */ type PartialObjectDeep> = { [KeyType in keyof ObjectType]?: _PartialDeep }; export {};