-
-
Notifications
You must be signed in to change notification settings - Fork 554
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
1d4e122
commit 98bb74d
Showing
4 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |