forked from sindresorhus/type-fest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close sindresorhus#621
- Loading branch information
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,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] |
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,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); |