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 = { secure: true, }; const responder2: RequireAllOrNone = { 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 = KeysOfUnion> = IfNotAnyOrNever, ObjectType, _RequireAllOrNone, KeysOfUnion, 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 extends unknown ? ( | RequireAll | RequireNone ) & Except // Keep unselected declared fields even when an index signature covers them. : never; export {};