From d0f7964928d3cee6b1a6fe314a3c2f6c71974716 Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Fri, 18 Aug 2023 19:30:09 +0200 Subject: [PATCH] code review suggestions --- source/non-empty-object.d.ts | 11 +++++++---- test-d/non-empty-object.ts | 17 ++++++----------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/source/non-empty-object.d.ts b/source/non-empty-object.d.ts index 8b40741b2..fe48fa30d 100644 --- a/source/non-empty-object.d.ts +++ b/source/non-empty-object.d.ts @@ -7,11 +7,11 @@ This is useful where you need an object where all keys are optional, but there m ``` import type {NonEmptyObject} from 'type-fest'; -interface User { +type User = { name: string; surname: string; id: number; -} +}; type UpdateRequest = NonEmptyObject; @@ -20,10 +20,13 @@ const update1: UpdateRequest = { surname: 'Acme', }; -const update2: UpdateRequest = {}; // that's a bug! +// at least 1 key is required, therefore this will report a 2322 error +// Type '{}' is not assignable to type 'UpdateRequest' +// you can use "IsEmptyObject" to check if a object is empty +const update2: UpdateRequest = {}; ``` -@category Utilities +@category Object */ export type NonEmptyObject = { [K in keyof T]-?: Pick, K> diff --git a/test-d/non-empty-object.ts b/test-d/non-empty-object.ts index d96a51c2e..98d4ee4cb 100644 --- a/test-d/non-empty-object.ts +++ b/test-d/non-empty-object.ts @@ -1,4 +1,4 @@ -import {expectType} from 'tsd'; +import {expectType, expectNever} from 'tsd'; import type {NonEmptyObject} from '../index'; type TestType1 = { @@ -18,17 +18,12 @@ type TestType3 = { type TestType4 = {}; -type NonEmptyObject1 = NonEmptyObject; -type NonEmptyObject2 = NonEmptyObject; -type NonEmptyObject3 = NonEmptyObject; -type NonEmptyObject4 = NonEmptyObject; - -declare const test1: NonEmptyObject1; -declare const test2: NonEmptyObject2; -declare const test3: NonEmptyObject3; -declare const test4: NonEmptyObject4; +declare const test1: NonEmptyObject; +declare const test2: NonEmptyObject; +declare const test3: NonEmptyObject; +declare const test4: NonEmptyObject; expectType(test1); expectType<{a: string; b?: boolean} | {a?: string; b: boolean}>(test2); expectType(test3); -expectType(test4); +expectNever(test4);