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 RequireExactlyOne type #55

Merged
merged 9 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {Mutable} from './source/mutable';
export {Merge} from './source/merge';
export {MergeExclusive} from './source/merge-exclusive';
export {RequireAtLeastOne} from './source/require-at-least-one';
export {RequireOne} from './source/require-one';
export {ReadonlyDeep} from './source/readonly-deep';
export {LiteralUnion} from './source/literal-union';

Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Click the type names for complete docs.
- [`Merge`](source/merge.d.ts) - Merge two types into a new type. Keys of the second type overrides keys of the first type.
- [`MergeExclusive`](source/merge-exclusive.d.ts) - Create a type that has mutually exclusive properties.
- [`RequireAtLeastOne`](source/require-at-least-one.d.ts) - Create a type that requires at least one of the given properties.
- [`RequireOne`](source/require-one.d.ts) - Create a type that requires a single property of the given properties, but not more.
kainiedziela marked this conversation as resolved.
Show resolved Hide resolved
- [`ReadonlyDeep`](source/readonly-deep.d.ts) - Create a deeply immutable version of a `object`/`Map`/`Set`/`Array` type.
- [`LiteralUnion`](source/literal-union.d.ts) - Create a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union. Workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729).

Expand Down
30 changes: 30 additions & 0 deletions source/require-one.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
Create a type that requires only one of the given properties, but not more. The remaining properties are kept as is.
kainiedziela marked this conversation as resolved.
Show resolved Hide resolved

Use cases:
- Creating interfaces for components that only need one of the properties to display properly.
- Declaring generic properties in a single place for a single use case that get narrowed down via `RequireOne`.

The caveat with `RequireOne` is that TypeScript doesn't always know at compile time every property that will exist at runtime. Therefore `RequireOne` can't do anything to prevent extra properties it doesn't know about.

kainiedziela marked this conversation as resolved.
Show resolved Hide resolved
@example
```
import {RequireOne} from 'type-fest';

type Responder = {
text: () => string;
json: () => string;
secure: boolean;
};

const responder: RequireOne<Responder, 'text' | 'json'> = {
json: () => '{"message": "ok"}',
secure: true
};
```
*/
export type RequireOne<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> =
{[Key in KeysType]: (
Required<Pick<ObjectType, Key>> &
Partial<Record<Exclude<KeysType, Key>, never>>
)}[KeysType] & Omit<ObjectType, KeysType>;
24 changes: 24 additions & 0 deletions test-d/require-one.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {expectType, expectError} from 'tsd';
import {RequireOne} from '..';

type SystemMessages = {
default: string;
kainiedziela marked this conversation as resolved.
Show resolved Hide resolved

macos: string;
linux: string;

optional?: string;
};

type ValidMessages = RequireOne<SystemMessages, 'macos' | 'linux'>;
const test = (_: ValidMessages): void => {};

test({macos: 'hey', default: 'hello'});
test({linux: 'sup', optional: 'howdy', default: 'hello'});

expectError(test({}));
expectError(test({macos: 'hey', linux: 'sup', default: 'hello'}));
kainiedziela marked this conversation as resolved.
Show resolved Hide resolved

declare const oneWithoutKeys: RequireOne<{a: number; b: number}>;
expectType<{a: number} | {b: number}>(oneWithoutKeys);
expectError(expectType<{a: number; b: number}>(oneWithoutKeys));