import type {HasMultipleCallSignatures} from './has-multiple-call-signatures.d.ts'; import type {CallSignatureOf, IsArrayOfUnknownLength, NonRecursiveLeaf} from './internal/index.d.ts'; import type {ApplyDefaultOptions} from './apply-default-options.d.ts'; import type {IsNever} from './is-never.d.ts'; /** @see {@link RequiredDeep} */ export type RequiredDeepOptions = { /** The types to keep as is, instead of making deeply required. A kept type is left untouched at every depth, including everything inside it. Matching uses assignability. @default never @example ``` import type {RequiredDeep} from 'type-fest'; type Color = {red?: number}; type Settings = {name?: string; color?: Color}; type RequiredSettings = RequiredDeep; //=> {name: string; color: Color} ``` */ readonly keep?: unknown; }; /** Create a deeply required version of another type. Use-cases: - Creating optional configuration interfaces where the underlying implementation still requires all options to be fully specified. - Modeling the resulting type after a deep merge with a set of defaults. Use [`Required`](https://www.typescriptlang.org/docs/handbook/utility-types.html#requiredtype) if you only need one level deep. @example ``` import type {RequiredDeep} from 'type-fest'; type Settings = { textEditor?: { fontSize?: number; fontColor?: string; fontWeight?: number | undefined; }; autocomplete?: boolean; autosave?: boolean | undefined; }; type RequiredSettings = RequiredDeep; //=> { // textEditor: { // fontSize: number; // fontColor: string; // fontWeight: number | undefined; // }; // autocomplete: boolean; // autosave: boolean | undefined; // } ``` 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 required. 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 {RequiredDeep} from 'type-fest'; class Color { r?: number; g?: number; } type Settings = { name?: string; color?: Color; }; type RequiredKeptSettings = RequiredDeep; // { // name: string; // color: Color; // } ``` 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. Note that an `Error` is walked like any other object, because a plain object with `name` and `message` is structurally an `Error` too. So `RequiredDeep` makes `stack` required, and a real `Error` is not assignable to it. Pass `{keep: Error}` as the second type argument to keep it as is. @see {@link RequiredDeepOptions} @category Utilities @category Object @category Array @category Set @category Map */ export type RequiredDeep = _RequiredDeep['keep']>; /** Internal helper for {@link RequiredDeep}. */ type _RequiredDeep = // 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 ? Value : Value extends Map ? Map<_RequiredDeep, _RequiredDeep> : Value extends Set ? Set<_RequiredDeep> : Value extends ReadonlyMap ? ReadonlyMap<_RequiredDeep, _RequiredDeep> : Value extends ReadonlySet ? ReadonlySet<_RequiredDeep> : Value extends WeakMap ? WeakMap<_RequiredDeep, _RequiredDeep> : Value extends WeakSet ? WeakSet<_RequiredDeep> : Value extends Promise ? Promise<_RequiredDeep> : Value extends (...arguments_: any[]) => unknown ? IsNever extends true ? Value : HasMultipleCallSignatures extends true ? Value : CallSignatureOf & RequiredObjectDeep // The `Function` interface has no call signature, so the branch above misses it, and mapping it would produce an uncallable record of its own methods. : Value extends Function ? Value : Value extends readonly unknown[] ? IsArrayOfUnknownLength extends true // Rebuild arrays directly to keep recursive elements lazy, and read through `Required` to preserve its treatment of `undefined`. The element is matched against `Keep` before `Required`, so a kept element union is not stripped by it. ? Value extends unknown[] ? Array<_RequiredDeep[number], Keep>> : ReadonlyArray<_RequiredDeep[number], Keep>> : RequiredObjectDeep : Value extends object ? RequiredObjectDeep // The terminal intersection expands the object without a second mapping. : unknown; type RequiredObjectDeep = { [KeyType in keyof ObjectType]-?: _RequiredDeep } & {}; export {};