UNPKG

827 BTypeScriptView Raw
1import {Except} from './except';
2
3/**
4Create a type that requires at least one of the given properties. The remaining properties are kept as is.
5
6@example
7```
8import {RequireAtLeastOne} from 'type-fest';
9
10type Responder = {
11 text?: () => string;
12 json?: () => string;
13
14 secure?: boolean;
15};
16
17const responder: RequireAtLeastOne<Responder, 'text' | 'json'> = {
18 json: () => '{"message": "ok"}',
19 secure: true
20};
21```
22*/
23export type RequireAtLeastOne<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> =
24 {
25 // For each Key in KeysType make a mapped type
26 [Key in KeysType]: (
27 // …by picking that Key's type and making it required
28 Required<Pick<ObjectType, Key>>
29 )
30 }[KeysType]
31 // …then, make intersection types by adding the remaining properties to each mapped type.
32 & Except<ObjectType, KeysType>;