Skip to content

Commit

Permalink
Add NonEmptyObject type (#623)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
kkmuffme and sindresorhus authored Aug 25, 2023
1 parent 1d4e122 commit 98bb74d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './source/observable-like';

// Utilities
export type {EmptyObject, IsEmptyObject} from './source/empty-object';
export type {NonEmptyObject} from './source/non-empty-object';
export type {UnknownRecord} from './source/unknown-record';
export type {Except} from './source/except';
export type {TaggedUnion} from './source/tagged-union';
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Click the type names for complete docs.

- [`EmptyObject`](source/empty-object.d.ts) - Represents a strictly empty plain object, the `{}` value.
- [`IsEmptyObject`](source/empty-object.d.ts) - Returns a `boolean` for whether the type is strictly equal to an empty plain object, the `{}` value.
- [`NonEmptyObject`](source/non-empty-object.d.ts) - Represents an object with at least 1 non-optional key.
- [`UnknownRecord`](source/unknown-record.d.ts) - Represents an object with `unknown` value. You probably want this instead of `{}`.
- [`Except`](source/except.d.ts) - Create a type from an object type without certain keys. This is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys).
- [`Writable`](source/writable.d.ts) - Create a type that strips `readonly` from all or some of an object's keys. The inverse of `Readonly<T>`.
Expand Down
35 changes: 35 additions & 0 deletions source/non-empty-object.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type {HasRequiredKeys} from './has-required-keys';
import type {RequireAtLeastOne} from './require-at-least-one';

/**
Represents an object with at least 1 non-optional key.
This is useful when you need an object where all keys are optional, but there must be at least 1 key.
@example
```
import type {NonEmptyObject} from 'type-fest';
type User = {
name: string;
surname: string;
id: number;
};
type UpdateRequest<Entity extends object> = NonEmptyObject<Partial<Entity>>;
const update1: UpdateRequest<User> = {
name: 'Alice',
surname: 'Acme',
};
// At least 1 key is required, therefore this will report a 2322 error:
// Type '{}' is not assignable to type 'UpdateRequest<User>'
const update2: UpdateRequest<User> = {};
```
@see Use `IsEmptyObject` to check whether an object is empty.
@category Object
*/
export type NonEmptyObject<T extends object> = HasRequiredKeys<T> extends true ? T : RequireAtLeastOne<T, keyof T>;
29 changes: 29 additions & 0 deletions test-d/non-empty-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {expectNever, expectType} from 'tsd';
import type {NonEmptyObject, RequireAtLeastOne} from '../index';

type TestType1 = {
a: string;
b: boolean;
};

type TestType2 = {
a?: string;
b?: boolean;
};

type TestType3 = {
a: string;
b?: boolean;
};

type TestType4 = {};

declare const test1: NonEmptyObject<TestType1>;
declare const test2: NonEmptyObject<TestType2>;
declare const test3: NonEmptyObject<TestType3>;
declare const test4: NonEmptyObject<TestType4>;

expectType<TestType1>(test1);
expectType<RequireAtLeastOne<TestType2>>(test2);
expectType<TestType3>(test3);
expectNever(test4);

0 comments on commit 98bb74d

Please sign in to comment.