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

Feature/issue 112 #127

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Rename Optional type to AugmentedPartial
  • Loading branch information
wbadam committed Dec 9, 2019
commit 90826228915f699ace5fa93f6910284f3348af3e
42 changes: 18 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ We are open for contributions. If you're planning to contribute please make sure
* [`ReadonlyKeys<T>`](#readonlykeyst)
* [`RequiredKeys<T>`](#requiredkeyst)
* [`OptionalKeys<T>`](#optionalkeyst)
* [`Optional<T, K>`](#optionalt-k)
* [`Partial<T>`](#partialt) _(built-in)_
* [`Partial<T, K>`](#partialt-k)
* [`DeepPartial<T>`](#deeppartialt)
* [`Required<T, K>`](#requiredt-k)
* [`DeepRequired<T>`](#deeprequiredt)
Expand Down Expand Up @@ -399,26 +398,6 @@ type Keys = OptionalKeys<Props>;

[⇧ back to top](#table-of-contents)

### `Optional<T, K>`

From `T` make a set of properties by key `K` become optional

**Usage:**

```ts
import { Optional } from 'utility-types';

type Props = { name: string; age: number; visible: boolean; };

// Expect: { name?: string; age?: number; visible?: boolean; }
type Props = Optional<Props>
// Expect: { name: string; age?: number; visible?: boolean; }
type Props = Optional<Props, 'age' | 'visible'>;
```

[⇧ back to top](#table-of-contents)


### `Pick<T, K>` _(built-in)_

From `T` pick a set of properties by key `K`
Expand Down Expand Up @@ -647,9 +626,24 @@ type BinaryItems = ValuesType<BinaryArray>;

[⇧ back to top](#table-of-contents)

### `Partial<T>`
### `Partial<T, K>`

From `T` make a set of properties by key `K` become optional

Alias: `Optional<T, K>`

**Usage:**

```ts
import { Partial } from 'utility-types';

type Props = { name: string; age: number; visible: boolean; };

Make all properties of object type optional
// Expect: { name?: string; age?: number; visible?: boolean; }
type Props = Partial<Props>
// Expect: { name: string; age?: number; visible?: boolean; }
type Props = Partial<Props, 'age' | 'visible'>;
```

[⇧ back to top](#table-of-contents)

Expand Down
11 changes: 7 additions & 4 deletions src/__snapshots__/mapped-types.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,16 @@ exports[`OmitByValueExact testType<OmitByValueExact<T, number>>() (type) should

exports[`OmitByValueExact testType<keyof OmitByValueExact<RequiredOptionalProps, number>>() (type) should match snapshot 1`] = `"\\"reqUndef\\" | \\"opt\\" | \\"optUndef\\""`;

exports[`Optional testType<Optional<Props, 'age' | 'visible'>>({ name: 'Yolo' }) (type) should match snapshot 1`] = `"Optional<Props, \\"age\\" | \\"visible\\">"`;
exports[`Optional testType<AugmentedPartial<Props, 'age' | 'visible'>>({
name: 'Yolo',
age: 99,
}) (type) should match snapshot 1`] = `"AugmentedPartial<Props, \\"age\\" | \\"visible\\">"`;

exports[`Optional testType<Optional<Props, 'age' | 'visible'>>({ name: 'Yolo', age: 99 }) (type) should match snapshot 1`] = `"Optional<Props, \\"age\\" | \\"visible\\">"`;
exports[`Optional testType<AugmentedPartial<Props, 'age' | 'visible'>>({ name: 'Yolo' }) (type) should match snapshot 1`] = `"AugmentedPartial<Props, \\"age\\" | \\"visible\\">"`;

exports[`Optional testType<Optional<Props>>({ age: 99 }) (type) should match snapshot 1`] = `"Optional<Props, \\"name\\" | \\"age\\" | \\"visible\\">"`;
exports[`Optional testType<AugmentedPartial<Props>>({ age: 99 }) (type) should match snapshot 1`] = `"AugmentedPartial<Props, \\"name\\" | \\"age\\" | \\"visible\\">"`;

exports[`Optional testType<Optional<Props>>({}) (type) should match snapshot 1`] = `"Optional<Props, \\"name\\" | \\"age\\" | \\"visible\\">"`;
exports[`Optional testType<AugmentedPartial<Props>>({}) (type) should match snapshot 1`] = `"AugmentedPartial<Props, \\"name\\" | \\"age\\" | \\"visible\\">"`;

exports[`OptionalKeys testType<OptionalKeys<RequiredOptionalProps>>() (type) should match snapshot 1`] = `"\\"opt\\" | \\"optUndef\\""`;

Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ export {
Omit,
OmitByValue,
OmitByValueExact,
Optional,
OptionalKeys,
Overwrite,
Optional,
AugmentedPartial as Partial,
PickByValue,
PickByValueExact,
PromiseType,
Expand Down
21 changes: 12 additions & 9 deletions src/mapped-types.spec.snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
OptionalKeys,
PickByValueExact,
OmitByValueExact,
Optional,
AugmentedPartial,
ValuesType,
AugmentedRequired,
UnionToIntersection,
Expand Down Expand Up @@ -498,15 +498,18 @@ type RequiredOptionalProps = {

// @dts-jest:group Optional
{
// @dts-jest:pass:snap -> Optional<Props, "name" | "age" | "visible">
testType<Optional<Props>>({});
// @dts-jest:pass:snap -> Optional<Props, "name" | "age" | "visible">
testType<Optional<Props>>({ age: 99 });
// @dts-jest:pass:snap -> AugmentedPartial<Props, "name" | "age" | "visible">
testType<AugmentedPartial<Props>>({});
// @dts-jest:pass:snap -> AugmentedPartial<Props, "name" | "age" | "visible">
testType<AugmentedPartial<Props>>({ age: 99 });

// @dts-jest:pass:snap -> Optional<Props, "age" | "visible">
testType<Optional<Props, 'age' | 'visible'>>({ name: 'Yolo' });
// @dts-jest:pass:snap -> Optional<Props, "age" | "visible">
testType<Optional<Props, 'age' | 'visible'>>({ name: 'Yolo', age: 99 });
// @dts-jest:pass:snap -> AugmentedPartial<Props, "age" | "visible">
testType<AugmentedPartial<Props, 'age' | 'visible'>>({ name: 'Yolo' });
// @dts-jest:pass:snap -> AugmentedPartial<Props, "age" | "visible">
testType<AugmentedPartial<Props, 'age' | 'visible'>>({
name: 'Yolo',
age: 99,
});
}

// @dts-jest:group ValuesType
Expand Down
13 changes: 8 additions & 5 deletions src/mapped-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
OptionalKeys,
PickByValueExact,
OmitByValueExact,
Optional,
AugmentedPartial,
ValuesType,
AugmentedRequired,
UnionToIntersection,
Expand Down Expand Up @@ -499,14 +499,17 @@ type RequiredOptionalProps = {
// @dts-jest:group Optional
{
// @dts-jest:pass:snap
testType<Optional<Props>>({});
testType<AugmentedPartial<Props>>({});
// @dts-jest:pass:snap
testType<Optional<Props>>({ age: 99 });
testType<AugmentedPartial<Props>>({ age: 99 });

// @dts-jest:pass:snap
testType<Optional<Props, 'age' | 'visible'>>({ name: 'Yolo' });
testType<AugmentedPartial<Props, 'age' | 'visible'>>({ name: 'Yolo' });
// @dts-jest:pass:snap
testType<Optional<Props, 'age' | 'visible'>>({ name: 'Yolo', age: 99 });
testType<AugmentedPartial<Props, 'age' | 'visible'>>({
name: 'Yolo',
age: 99,
});
}

// @dts-jest:group ValuesType
Expand Down
19 changes: 11 additions & 8 deletions src/mapped-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ export type _DeepPartialObject<T> = { [P in keyof T]?: DeepPartial<T[P]> };
export type Brand<T, U> = T & { __brand: U };

/**
* Optional
* Partial
* @desc From `T` make a set of properties by key `K` become optional
* @example
* type Props = {
Expand All @@ -547,16 +547,19 @@ export type Brand<T, U> = T & { __brand: U };
* };
*
* // Expect: { name?: string; age?: number; visible?: boolean; }
* type Props = Optional<Props>;
* type Props = Partial<Props>;
*
* // Expect: { name: string; age?: number; visible?: boolean; }
* type Props = Optional<Props, 'age' | 'visible'>;
* type Props = Partial<Props, 'age' | 'visible'>;
*/
export type Optional<T extends object, K extends keyof T = keyof T> = Omit<
T,
K
> &
Partial<Pick<T, K>>;
export type AugmentedPartial<
T extends object,
K extends keyof T = keyof T
> = Omit<T, K> & Partial<Pick<T, K>>;
export type Optional<
T extends object,
K extends keyof T = keyof T
> = AugmentedPartial<T, K>;

/**
* ValuesType
Expand Down