Last change
on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
848 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | import {Except} from './except';
|
---|
| 2 |
|
---|
| 3 | /**
|
---|
| 4 | Create a type that requires at least one of the given keys. The remaining keys are kept as is.
|
---|
| 5 |
|
---|
| 6 | @example
|
---|
| 7 | ```
|
---|
| 8 | import {RequireAtLeastOne} from 'type-fest';
|
---|
| 9 |
|
---|
| 10 | type Responder = {
|
---|
| 11 | text?: () => string;
|
---|
| 12 | json?: () => string;
|
---|
| 13 |
|
---|
| 14 | secure?: boolean;
|
---|
| 15 | };
|
---|
| 16 |
|
---|
| 17 | const responder: RequireAtLeastOne<Responder, 'text' | 'json'> = {
|
---|
| 18 | json: () => '{"message": "ok"}',
|
---|
| 19 | secure: true
|
---|
| 20 | };
|
---|
| 21 | ```
|
---|
| 22 | */
|
---|
| 23 | export type RequireAtLeastOne<
|
---|
| 24 | ObjectType,
|
---|
| 25 | KeysType extends keyof ObjectType = keyof ObjectType
|
---|
| 26 | > = {
|
---|
| 27 | // For each `Key` in `KeysType` make a mapped type:
|
---|
| 28 | [Key in KeysType]-?: Required<Pick<ObjectType, Key>> & // 1. Make `Key`'s type required
|
---|
| 29 | // 2. Make all other keys in `KeysType` optional
|
---|
| 30 | Partial<Pick<ObjectType, Exclude<KeysType, Key>>>;
|
---|
| 31 | }[KeysType] &
|
---|
| 32 | // 3. Add the remaining keys not in `KeysType`
|
---|
| 33 | Except<ObjectType, KeysType>;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.