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