1 | import type {RequireExactlyOne} from './require-exactly-one';
|
2 | import type {RequireNone} from './internal';
|
3 |
|
4 | /**
|
5 | Create a type that requires exactly one of the given keys and disallows more, or none of the given keys. The remaining keys are kept as is.
|
6 |
|
7 | @example
|
8 | ```
|
9 | import type {RequireOneOrNone} from 'type-fest';
|
10 |
|
11 | type Responder = RequireOneOrNone<{
|
12 | text: () => string;
|
13 | json: () => string;
|
14 | secure: boolean;
|
15 | }, 'text' | 'json'>;
|
16 |
|
17 | const responder1: Responder = {
|
18 | secure: true
|
19 | };
|
20 |
|
21 | const responder2: Responder = {
|
22 | text: () => '{"message": "hi"}',
|
23 | secure: true
|
24 | };
|
25 |
|
26 | const responder3: Responder = {
|
27 | json: () => '{"message": "ok"}',
|
28 | secure: true
|
29 | };
|
30 | ```
|
31 |
|
32 | @category Object
|
33 | */
|
34 | export type RequireOneOrNone<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> = (
|
35 | | RequireExactlyOne<ObjectType, KeysType>
|
36 | | RequireNone<KeysType>
|
37 | ) & Omit<ObjectType, KeysType>; // Ignore unspecified keys.
|