-
-
Notifications
You must be signed in to change notification settings - Fork 513
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 <sindresorhus@gmail.com>
- Loading branch information
1 parent
2ab1c51
commit fa1c3f3
Showing
5 changed files
with
74 additions
and
1 deletion.
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,26 @@ | ||
import type {IsEmptyObject} from './empty-object'; | ||
|
||
/** | ||
An if-else-like type that resolves depending on whether the given type is `{}`. | ||
@see {@link IsEmptyObject} | ||
@example | ||
``` | ||
import type {IfEmptyObject} from 'type-fest'; | ||
type ShouldBeTrue = IfEmptyObject<{}>; | ||
//=> true | ||
type ShouldBeBar = IfEmptyObject<{key: any}, 'foo', 'bar'>; | ||
//=> 'bar' | ||
``` | ||
@category Type Guard | ||
@category Utilities | ||
*/ | ||
export type IfEmptyObject< | ||
T, | ||
TypeIfEmptyObject = true, | ||
TypeIfNotEmptyObject = false, | ||
> = IsEmptyObject<T> extends true ? TypeIfEmptyObject : TypeIfNotEmptyObject; |
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 type {IfEmptyObject} from '../index'; | ||
import type {IsUnion} from './internal'; | ||
|
||
/** | ||
Create a type that only accepts an object with a single key. | ||
@example | ||
``` | ||
import type {SingleKeyObject} from 'type-fest'; | ||
const someFunction = <T>(parameter: SingleKeyObject<T>) => {}; | ||
someFunction({ | ||
value: true | ||
}); | ||
someFunction({ | ||
value: true, | ||
otherKey: true | ||
}); | ||
// Error: Argument of type '{value: boolean; otherKey: boolean}' is not assignable to parameter of type 'never'.ts(2345) | ||
``` | ||
@category Object | ||
*/ | ||
export type SingleKeyObject<ObjectType> = | ||
IsUnion<keyof ObjectType> extends true | ||
? never | ||
: IfEmptyObject<ObjectType, never, ObjectType>; |
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,15 @@ | ||
import {expectNever, expectError, expectAssignable} from 'tsd'; | ||
import type {SingleKeyObject} from '../index'; | ||
|
||
const test = <T>(_: SingleKeyObject<T>): void => {}; // eslint-disable-line @typescript-eslint/no-empty-function | ||
|
||
test({key: 'value'}); | ||
|
||
expectError(test({})); | ||
expectError(test({key: 'value', otherKey: 'other value'})); | ||
|
||
declare const validObject: SingleKeyObject<{key: string}>; | ||
expectAssignable<{key: string}>(validObject); | ||
|
||
declare const invalidObject: SingleKeyObject<{key1: string; key2: number}>; | ||
expectNever(invalidObject); |