From 98bb74d3cbb73a71ed325cd984a754118ba4d054 Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Fri, 25 Aug 2023 13:19:25 +0200 Subject: [PATCH] Add `NonEmptyObject` type (#623) Co-authored-by: Sindre Sorhus --- index.d.ts | 1 + readme.md | 1 + source/non-empty-object.d.ts | 35 +++++++++++++++++++++++++++++++++++ test-d/non-empty-object.ts | 29 +++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 source/non-empty-object.d.ts create mode 100644 test-d/non-empty-object.ts diff --git a/index.d.ts b/index.d.ts index 85140b9d2..40f9b58e5 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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'; diff --git a/readme.md b/readme.md index 62e464d4c..54a18efb0 100644 --- a/readme.md +++ b/readme.md @@ -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`. diff --git a/source/non-empty-object.d.ts b/source/non-empty-object.d.ts new file mode 100644 index 000000000..0f65417a7 --- /dev/null +++ b/source/non-empty-object.d.ts @@ -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 = NonEmptyObject>; + +const update1: UpdateRequest = { + name: 'Alice', + surname: 'Acme', +}; + +// At least 1 key is required, therefore this will report a 2322 error: +// Type '{}' is not assignable to type 'UpdateRequest' +const update2: UpdateRequest = {}; +``` + +@see Use `IsEmptyObject` to check whether an object is empty. + +@category Object +*/ +export type NonEmptyObject = HasRequiredKeys extends true ? T : RequireAtLeastOne; diff --git a/test-d/non-empty-object.ts b/test-d/non-empty-object.ts new file mode 100644 index 000000000..0f14a676a --- /dev/null +++ b/test-d/non-empty-object.ts @@ -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; +declare const test2: NonEmptyObject; +declare const test3: NonEmptyObject; +declare const test4: NonEmptyObject; + +expectType(test1); +expectType>(test2); +expectType(test3); +expectNever(test4);