UNPKG

1.25 kBTypeScriptView Raw
1import type {RequireNone} from './internal';
2
3/**
4Requires all of the keys in the given object.
5*/
6type RequireAll<ObjectType, KeysType extends keyof ObjectType> = Required<Pick<ObjectType, KeysType>>;
7
8/**
9Create a type that requires all of the given keys or none of the given keys. The remaining keys are kept as is.
10
11Use-cases:
12- Creating interfaces for components with mutually-inclusive keys.
13
14The 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.
15
16@example
17```
18import type {RequireAllOrNone} from 'type-fest';
19
20type Responder = {
21 text?: () => string;
22 json?: () => string;
23 secure: boolean;
24};
25
26const responder1: RequireAllOrNone<Responder, 'text' | 'json'> = {
27 secure: true
28};
29
30const responder2: RequireAllOrNone<Responder, 'text' | 'json'> = {
31 text: () => '{"message": "hi"}',
32 json: () => '{"message": "ok"}',
33 secure: true
34};
35```
36
37@category Object
38*/
39export type RequireAllOrNone<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> = (
40 | RequireAll<ObjectType, KeysType>
41 | RequireNone<KeysType>
42) & Omit<ObjectType, KeysType>; // The rest of the keys.