Skip to content

Latest commit

 

History

History
34 lines (25 loc) · 1 KB

File metadata and controls

34 lines (25 loc) · 1 KB

Home > types-kit > PickAtLeastOne

PickAtLeastOne type

Create a type that requires at least one of the given keys. The remaining keys are kept as is.

Signature:

export type PickAtLeastOne<T, K extends Keys<T>> = StrictOmit<T, K> &
  {
    [P in K]: Required<Pick<T, P>> & Partial<Pick<T, StrictExclude<K, P>>>
  }[K]

References: Keys, StrictOmit, StrictExclude

Example

    interface Responder {
      text?: () => string;
      json?: () => string;
      secure?: boolean;
    };
    const responder: RequireAtLeastOne<Responder, 'text' | 'json'> = {
      // set at least one property, 'text' or 'json', otherwise throw error
      json: () => '{"message": "ok"}',
      secure: true
    };