UNPKG

903 BTypeScriptView Raw
1import type {RequireExactlyOne} from './require-exactly-one';
2import type {RequireNone} from './internal';
3
4/**
5Create 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```
9import type {RequireOneOrNone} from 'type-fest';
10
11type Responder = RequireOneOrNone<{
12 text: () => string;
13 json: () => string;
14 secure: boolean;
15}, 'text' | 'json'>;
16
17const responder1: Responder = {
18 secure: true
19};
20
21const responder2: Responder = {
22 text: () => '{"message": "hi"}',
23 secure: true
24};
25
26const responder3: Responder = {
27 json: () => '{"message": "ok"}',
28 secure: true
29};
30```
31
32@category Object
33*/
34export type RequireOneOrNone<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> = (
35 | RequireExactlyOne<ObjectType, KeysType>
36 | RequireNone<KeysType>
37) & Omit<ObjectType, KeysType>; // Ignore unspecified keys.