-
-
Notifications
You must be signed in to change notification settings - Fork 569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SingleKey and IfEmptyObject #849
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a1920ce
Add SingleKey and IfEmptyObject
a2b3599
Merge branch 'main' into feat-single-key
sindresorhus d8131c7
Update single-key.d.ts
sindresorhus ff7ccdc
Update single-key.d.ts
sindresorhus 2c2d17c
Rename to SingleKeyObject and clean up documentation
bc00c5e
Update if-empty-object.d.ts
sindresorhus 0e09c48
Update single-key-object.d.ts
sindresorhus d6fa1d0
Update single-key-object.d.ts
sindresorhus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,27 @@ | ||
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,26 @@ | ||
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 {SingleKey} from 'type-fest'; | ||
|
||
const processOperation = <T>(operation: SingleKey<T>) => {}; | ||
|
||
processOperation({ operation: { name: 'add' } }); | ||
|
||
processOperation({ operation: { name: 'add' }, 'values': [1, 2] }); // Compliation error | ||
``` | ||
|
||
@category Object | ||
*/ | ||
|
||
// export type SingleKey<ObjectType> = | ||
// IsUnion<keyof ObjectType> extends true ? never : {} extends ObjectType ? never : ObjectType; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leftover There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops I thought I removed them all |
||
export type SingleKey<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 {SingleKey} from '../index'; | ||
|
||
const test = <T>(_: SingleKey<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: SingleKey<{key: string}>; | ||
expectAssignable<{key: string}>(validObject); | ||
|
||
declare const invalidObject: SingleKey<{key1: string; key2: number}>; | ||
expectNever(invalidObject); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.