Skip to content

Commit

Permalink
code review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Aug 18, 2023
1 parent 5c357fc commit d0f7964
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
11 changes: 7 additions & 4 deletions source/non-empty-object.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity extends object> = NonEmptyObject<Entity>;
Expand All @@ -20,10 +20,13 @@ const update1: UpdateRequest<User> = {
surname: 'Acme',
};
const update2: UpdateRequest<User> = {}; // that's a bug!
// at least 1 key is required, therefore this will report a 2322 error
// Type '{}' is not assignable to type 'UpdateRequest<User>'
// you can use "IsEmptyObject" to check if a object is empty
const update2: UpdateRequest<User> = {};
```
@category Utilities
@category Object
*/
export type NonEmptyObject<T> = {
[K in keyof T]-?: Pick<Required<T>, K>
Expand Down
17 changes: 6 additions & 11 deletions test-d/non-empty-object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectType} from 'tsd';
import {expectType, expectNever} from 'tsd';
import type {NonEmptyObject} from '../index';

type TestType1 = {
Expand All @@ -18,17 +18,12 @@ type TestType3 = {

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;
declare const test1: NonEmptyObject<TestType1>;
declare const test2: NonEmptyObject<TestType2>;
declare const test3: NonEmptyObject<TestType3>;
declare const test4: NonEmptyObject<TestType4>;

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

0 comments on commit d0f7964

Please sign in to comment.