UNPKG

type-fest

Version:

A collection of essential TypeScript types

40 lines (30 loc) • 1.3 kB
import type {UnknownCollection} from './unknown-collection.d.ts'; import type {NonRecursiveType} from './internal/type.d.ts'; /** Revert the `Required` modifier on an object type. Use-case: Infer the underlying type `T` when only `Required<T>` is available or the original type may not be directly accessible. @example ``` import type {UnwrapRequired} from 'type-fest'; type ContactFormData = Required<{ email: string; message?: string; }>; type DraftContactFormData = UnwrapRequired<ContactFormData>; //=> {email: string; message?: string} ``` Note: - If the provided type isn’t of the form `Required<T>`, `UnwrapRequired` simply returns the input type. - A numeric or symbol index signature cannot be reversed through `Required<infer T>`, so `UnwrapRequired<{[index: number]: string}>` is `unknown` rather than the input type. - `UnwrapRequired` doesn't work with arrays, if instantiated with arrays, it simply returns the input type. @category Object */ export type UnwrapRequired<RequiredObjectType> = RequiredObjectType extends NonRecursiveType | UnknownCollection ? RequiredObjectType : _UnwrapRequired<RequiredObjectType>; type _UnwrapRequired<RequiredObjectType> = RequiredObjectType extends Required<infer ObjectType> ? ObjectType : RequiredObjectType; export {};