Skip to content

Commit

Permalink
Add NonEmptyObject type
Browse files Browse the repository at this point in the history
Close #621
  • Loading branch information
kkmuffme committed Aug 18, 2023
1 parent 10dcb30 commit c826a14
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
30 changes: 30 additions & 0 deletions source/non-empty-object.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
Represents an object with at least 1 non-optional key.
This is useful where you need an object where all keys are optional, but there must be at least 1 key.
@example
```
import type {NonEmptyObject} from 'type-fest';
interface User {
name: string;
surname: string;
id: number;
}
type UpdateRequest<Entity extends object> = NonEmptyObject<Entity>;
const update1: UpdateRequest<User> = {
name: 'Alice',
surname: 'Acme',
};
const update2: UpdateRequest<User> = {}; // that's a bug!
```
@category Utilities
*/
export type NonEmptyObject<T> = {
[K in keyof T]-?: Pick<Required<T>, K>
}[keyof T]
34 changes: 34 additions & 0 deletions test-d/non-empty-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {expectType} from 'tsd';
import type {NonEmptyObject} from '../index';

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

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

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

type TestType4 = {};

type NonEmptyObject1 = NonEmptyObject<TestType1>;
type NonEmptyObject2 = NonEmptyObject<TestType2>;
type NonEmptyObject3 = NonEmptyObject<TestType3>;
type NonEmptyObject4 = NonEmptyObject<TestType4>;

declare const test1: NonEmptyObject1;
declare const test2: NonEmptyObject2;
declare const test3: NonEmptyObject3;
declare const test4: NonEmptyObject4;

expectType<TestType1>(test1);
expectType<{a: string; b?: boolean}|{a?: string; b: boolean}>(test2);
expectType<TestType3|{a?: string; b: boolean}>(test3);
expectType<never>(test4);

0 comments on commit c826a14

Please sign in to comment.