Skip to content
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 8 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type {KeysOfUnion} from './source/keys-of-union';
export type {DistributedOmit} from './source/distributed-omit';
export type {DistributedPick} from './source/distributed-pick';
export type {EmptyObject, IsEmptyObject} from './source/empty-object';
export type {IfEmptyObject} from './source/if-empty-object';
export type {NonEmptyObject} from './source/non-empty-object';
export type {UnknownRecord} from './source/unknown-record';
export type {UnknownArray} from './source/unknown-array';
Expand All @@ -23,6 +24,7 @@ export type {RequireAtLeastOne} from './source/require-at-least-one';
export type {RequireExactlyOne} from './source/require-exactly-one';
export type {RequireAllOrNone} from './source/require-all-or-none';
export type {RequireOneOrNone} from './source/require-one-or-none';
export type {SingleKey} from './source/single-key';
export type {OmitIndexSignature} from './source/omit-index-signature';
export type {PickIndexSignature} from './source/pick-index-signature';
export type {PartialDeep, PartialDeepOptions} from './source/partial-deep';
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ Click the type names for complete docs.
### Utilities

- [`EmptyObject`](source/empty-object.d.ts) - Represents a strictly empty plain object, the `{}` value.
- [`IsEmptyObject`](source/empty-object.d.ts) - Returns a `boolean` for whether the type is strictly equal to an empty plain object, the `{}` value.
- [`NonEmptyObject`](source/non-empty-object.d.ts) - Represents an object with at least 1 non-optional key.
- [`UnknownRecord`](source/unknown-record.d.ts) - Represents an object with `unknown` value. You probably want this instead of `{}`.
- [`UnknownArray`](source/unknown-array.d.ts) - Represents an array with `unknown` value.
Expand All @@ -125,6 +124,7 @@ Click the type names for complete docs.
- [`RequireExactlyOne`](source/require-exactly-one.d.ts) - Create a type that requires exactly a single key of the given keys and disallows more.
- [`RequireAllOrNone`](source/require-all-or-none.d.ts) - Create a type that requires all of the given keys or none of the given keys.
- [`RequireOneOrNone`](source/require-one-or-none.d.ts) - Create a type that requires exactly a single key of the given keys and disallows more, or none of the given keys.
- [`SingleKey`](source/single-key.d.ts) - Create a type that only accepts an object with a single key.
- [`RequiredDeep`](source/required-deep.d.ts) - Create a deeply required version of another type. Use [`Required<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#requiredtype) if you only need one level deep.
- [`PickDeep`](source/pick-deep.d.ts) - Pick properties from a deeply-nested object. Use [`Pick<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys) if you only need one level deep.
- [`OmitDeep`](source/omit-deep.d.ts) - Omit properties from a deeply-nested object. Use [`Omit<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys) if you only need one level deep.
Expand Down Expand Up @@ -220,6 +220,7 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
- [`IsAny`](source/is-any.d.ts) - Returns a boolean for whether the given type is `any`. (Conditional version: [`IfAny`](source/if-any.d.ts).)
- [`IsNever`](source/is-never.d.ts) - Returns a boolean for whether the given type is `never`. (Conditional version: [`IfNever`](source/if-never.d.ts).)
- [`IsUnknown`](source/is-unknown.d.ts) - Returns a boolean for whether the given type is `unknown`. (Conditional version: [`IfUnknown`](source/if-unknown.d.ts).)
- [`IsEmptyObject`](source/empty-object.d.ts) - Returns a boolean for whether the type is strictly equal to an empty plain object, the `{}` value. (Conditional version: [`IfEmptyObject`](source/if-empty-object.d.ts))

### JSON

Expand Down
27 changes: 27 additions & 0 deletions source/if-empty-object.d.ts
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;
26 changes: 26 additions & 0 deletions source/single-key.d.ts
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
*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
*/


// export type SingleKey<ObjectType> =
// IsUnion<keyof ObjectType> extends true ? never : {} extends ObjectType ? never : ObjectType;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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>;
15 changes: 15 additions & 0 deletions test-d/single-key.ts
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);