import type {IsArrayOfUnknownLength, NonRecursiveType} from './internal/index.d.ts'; import type {UndefinedOnPartial} from './undefined-on-partial.d.ts'; // For `@link` /** Create a deep version of another type where all optional keys are set to also accept `undefined`. Note: This is only needed when the [`exactOptionalPropertyTypes`](https://www.typescriptlang.org/tsconfig#exactOptionalPropertyTypes) TSConfig setting is enabled. Use {@link UndefinedOnPartial} if you only need one level deep. Use-cases: - When `exactOptionalPropertyTypes` is enabled, an object like `{a: undefined}` is not assignable to the type `{a?: number}`. You can use `UndefinedOnPartialDeep<{a?: number}>` to make it assignable. @example ``` import type {UndefinedOnPartialDeep} from 'type-fest'; type Settings = { optionA: string; optionB?: number; subOption: { subOptionA: boolean; subOptionB?: boolean; }; }; const testSettingsA: Settings = { optionA: 'foo', optionB: undefined, // TypeScript error if `exactOptionalPropertyTypes` is true. // @ts-expect-error subOption: { subOptionA: true, subOptionB: undefined, // TypeScript error if `exactOptionalPropertyTypes` is true }, }; const testSettingsB: UndefinedOnPartialDeep = { optionA: 'foo', optionB: undefined, // `optionB` can be set to `undefined` now. subOption: { subOptionA: true, subOptionB: undefined, // `subOptionB` can be set to `undefined` now. }, }; ``` */ export type UndefinedOnPartialDeep = // Handle the non-recursive types. Mapping over one of them replaces it with a plain object of its own members, so a `Uint8Array` would come back as a fifty property object literal. T extends NonRecursiveType ? T // Handle tuple and array : T extends readonly unknown[] ? UndefinedOnPartialList // Handle map and readonly map : T extends Map ? Map> : T extends ReadonlyMap ? ReadonlyMap> // Handle weak map : T extends WeakMap ? WeakMap> // Handle set and readonly set : T extends Set ? Set> : T extends ReadonlySet ? ReadonlySet> // Handle weak set : T extends WeakSet ? WeakSet> // Handle object : T extends Record ? UndefinedOnPartialProperties : T; // If T is not builtins / function / array / map / set / object, return T // Handle tuples and arrays type UndefinedOnPartialList = IsArrayOfUnknownLength extends false ? UndefinedOnPartialProperties // An array of unknown length is rebuilt directly, because mapping it makes a recursive element type, such as `JsonValue`, infinitely deep. : T extends unknown[] ? Array> : ReadonlyArray>; /** Converts object properties and tuple elements, keeping their optional and readonly modifiers, and a tuple's length, labels and rest element. */ type UndefinedOnPartialProperties = { // A homomorphic mapped type keeps fixed positions at either end, including optional elements and elements after a leading rest. [Key in keyof T]: undefined extends T[Key] // An optional property or tuple element needs an explicit `undefined` in its value type, beyond the absence allowed by its `?` modifier. ? UndefinedOnPartialDeep | undefined : UndefinedOnPartialDeep }; export {};