UNPKG

type-fest

Version:

A collection of essential TypeScript types

57 lines (46 loc) • 2.13 kB
import type {KeysOfUnion} from './keys-of-union.d.ts'; import type {Except} from './except.d.ts'; import type {If} from './if.d.ts'; import type {IfNotAnyOrNever, RequireAll} from './internal/index.d.ts'; import type {RequireNone} from './require-none.d.ts'; import type {IsAny} from './is-any.d.ts'; import type {IsNever} from './is-never.d.ts'; /** Create a type that requires all of the given keys or none of the given keys, while keeping the remaining keys as is. Use-cases: - Creating interfaces for components with mutually-inclusive keys. The caveat with `RequireAllOrNone` is that TypeScript doesn't always know at compile time every key that will exist at runtime. Therefore `RequireAllOrNone` can't do anything to prevent extra keys it doesn't know about. @example ``` import type {RequireAllOrNone} from 'type-fest'; type Responder = { text?: () => string; json?: () => string; secure: boolean; }; const responder1: RequireAllOrNone<Responder, 'text' | 'json'> = { secure: true, }; const responder2: RequireAllOrNone<Responder, 'text' | 'json'> = { text: () => '{"message": "hi"}', json: () => '{"message": "ok"}', secure: true, }; ``` @category Object */ // `IfNotAnyOrNever` comes first, so an `any` `ObjectType` stays `any` even when `KeysType` is generic. export type RequireAllOrNone<ObjectType, KeysType extends KeysOfUnion<ObjectType> = KeysOfUnion<ObjectType>> = IfNotAnyOrNever<ObjectType, { ifNot: If<IsNever<KeysType>, ObjectType, _RequireAllOrNone<ObjectType, If<IsAny<KeysType>, KeysOfUnion<ObjectType>, KeysType>>>; }>; // The keys are intersected with each member's own inside the distribution, because `keyof` of a union is the intersection of its members' keys, so a key only one member has would otherwise be dropped before the type ever saw it. type _RequireAllOrNone<ObjectType, KeysType> = ObjectType extends unknown ? ( | RequireAll<ObjectType, KeysType & keyof ObjectType> | RequireNone<KeysType & keyof ObjectType> ) & Except<ObjectType, KeysType & keyof ObjectType> // Keep unselected declared fields even when an index signature covers them. : never; export {};