UNPKG

943 BTypeScriptView Raw
1import type {HasRequiredKeys} from './has-required-keys';
2import type {RequireAtLeastOne} from './require-at-least-one';
3
4/**
5Represents an object with at least 1 non-optional key.
6
7This is useful when you need an object where all keys are optional, but there must be at least 1 key.
8
9@example
10```
11import type {NonEmptyObject} from 'type-fest';
12
13type User = {
14 name: string;
15 surname: string;
16 id: number;
17};
18
19type UpdateRequest<Entity extends object> = NonEmptyObject<Partial<Entity>>;
20
21const update1: UpdateRequest<User> = {
22 name: 'Alice',
23 surname: 'Acme',
24};
25
26// At least 1 key is required, therefore this will report a 2322 error:
27// Type '{}' is not assignable to type 'UpdateRequest<User>'
28const update2: UpdateRequest<User> = {};
29```
30
31@see Use `IsEmptyObject` to check whether an object is empty.
32
33@category Object
34*/
35export type NonEmptyObject<T extends object> = HasRequiredKeys<T> extends true ? T : RequireAtLeastOne<T, keyof T>;