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 all 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
85 changes: 54 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,13 @@ 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)
* [`Readonly<T>`](#readonlyt) _(built-in)_
* [`Readonly<T>`](#readonlyt-k)
* [`DeepReadonly<T>`](#deepreadonlyt)
* [`Mutable<T>`](#mutablet)
* [`Mutable<T, K>`](#mutablet-k)
* [`Pick<T, K>` _(built-in)_](#pickt-k-built-in)
* [`Omit<T, K>`](#omitt-k) _(built-in)_
* [`PickByValue<T, ValueType>`](#pickbyvaluet-valuetype)
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';

Make all properties of object type optional
type Props = { name: string; age: number; visible: boolean; };

// 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 All @@ -672,17 +666,41 @@ type Props = Required<Props, 'age' | 'visible'>;

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

### `Readonly<T>`
### `Readonly<T, K>`

From `T`, make a set of properties, whose keys are in the union `K`, readonly

Make all properties of object type readonly
```ts
import { Readonly } from 'utility-types';

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

// Expect: {
// readonly name: string;
// readonly age: number;
// readonly visible: boolean;
// }
Readonly<Props>;

// Expect: {
// name: string;
// readonly age: number;
// readonly visible: boolean;
// }
Readonly<Props, 'age' | 'visible'>;
```

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

### `Mutable<T>`
### `Mutable<T, K>`

From `T` make all properties become mutable
From `T`, make a set of properties, whose keys are in the union `K`, mutable

Alias: `Writable<T>`
Alias: `Writable<T, K>`

```ts
import { Mutable } from 'utility-types';
Expand All @@ -695,6 +713,9 @@ type Props = {

// Expect: { name: string; age: number; visible: boolean; }
Mutable<Props>;

// Expect: { readonly name: string; age: number; visible: boolean; }
Mutable<Props, 'age' | 'visible'>;
```

[⇧ back to top](#table-of-contents)
Expand Down Expand Up @@ -834,6 +855,8 @@ type RequiredNestedProps = DeepNonNullable<NestedProps>;

Partial that works for deeply nested structures

Alias: `DeepOptional<T>`

**Usage:**

```ts
Expand Down
61 changes: 45 additions & 16 deletions src/__snapshots__/mapped-types.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,44 @@ exports[`Assign const result: Assign<{}, Omit<T, 'age'>> = rest (type) should ma

exports[`Assign testType<Assign<Props, NewProps>>() (type) should match snapshot 1`] = `"Pick<Pick<Props, \\"name\\" | \\"visible\\"> & Pick<NewProps, \\"age\\"> & Pick<NewProps, \\"other\\">, \\"name\\" | \\"age\\" | \\"visible\\" | \\"other\\">"`;

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

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

exports[`AugmentedMutable testType<AugmentedMutable<Readonly<Props>>['age']>(99) (type) should match snapshot 1`] = `"number"`;

exports[`AugmentedMutable testType<AugmentedMutable<Readonly<Props>>['name']>('Yolo') (type) should match snapshot 1`] = `"string"`;

exports[`AugmentedMutable testType<AugmentedMutable<Readonly<Props>>['visible']>(true) (type) should match snapshot 1`] = `"boolean"`;

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

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

exports[`AugmentedReadonly testType<AugmentedReadonly<Props>['age']>() (type) should match snapshot 1`] = `"number"`;

exports[`AugmentedReadonly testType<AugmentedReadonly<Props>['name']>() (type) should match snapshot 1`] = `"string"`;

exports[`AugmentedReadonly testType<AugmentedReadonly<Props>['visible']>() (type) should match snapshot 1`] = `"boolean"`;

exports[`AugmentedReadonly testType<ReadonlyKeys<AugmentedReadonly<Props, 'name' | 'age'>>>() (type) should match snapshot 1`] = `"\\"name\\" | \\"age\\""`;

exports[`AugmentedRequired testType<AugmentedRequired<Partial<Props>, 'age' | 'visible'>>({
age: 99,
visible: true,
Expand Down Expand Up @@ -111,18 +149,6 @@ exports[`Intersection testType<Intersection<Props | NewProps, DefaultProps>>() (

exports[`Intersection testType<Intersection<Props, DefaultProps>>() (type) should match snapshot 1`] = `"Pick<Props, \\"age\\">"`;

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

exports[`Mutable testType<Mutable<Readonly<Props>>['age']>(99) (type) should match snapshot 1`] = `"number"`;

exports[`Mutable testType<Mutable<Readonly<Props>>['name']>('Yolo') (type) should match snapshot 1`] = `"string"`;

exports[`Mutable testType<Mutable<Readonly<Props>>['visible']>(true) (type) should match snapshot 1`] = `"boolean"`;

exports[`MutableKeys testType<MutableKeys<ReadWriteProps>>() (type) should match snapshot 1`] = `"\\"b\\""`;

exports[`NonFunctionKeys testType<NonFunctionKeys<MixedProps>>() (type) should match snapshot 1`] = `"\\"name\\" | \\"someKeys\\""`;
Expand Down Expand Up @@ -159,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
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,28 @@ export {
Assign,
Brand,
DeepNonNullable,
DeepOptional,
DeepPartial,
DeepReadonly,
DeepRequired,
Diff,
FunctionKeys,
Intersection,
Mutable,
AugmentedMutable as Mutable,
MutableKeys,
NonFunctionKeys,
NonUndefined,
Omit,
OmitByValue,
OmitByValueExact,
Optional,
OptionalKeys,
Overwrite,
Optional,
AugmentedPartial as Partial,
PickByValue,
PickByValueExact,
PromiseType,
AugmentedReadonly as Readonly,
ReadonlyKeys,
AugmentedRequired as Required,
RequiredKeys,
Expand Down
72 changes: 56 additions & 16 deletions src/mapped-types.spec.snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ import {
OptionalKeys,
PickByValueExact,
OmitByValueExact,
Optional,
AugmentedPartial,
ValuesType,
AugmentedRequired,
UnionToIntersection,
Mutable,
AugmentedMutable,
AugmentedReadonly,
} from './mapped-types';

/**
Expand Down Expand Up @@ -497,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 Expand Up @@ -572,21 +576,57 @@ type RequiredOptionalProps = {
testType<UnionToIntersection<'name' | 'age'>>();
}

// @dts-jest:group Mutable
// @dts-jest:group AugmentedMutable
{
// @dts-jest:pass:snap -> Mutable<Readonly<Props>>
testType<Mutable<Readonly<Props>>>({
// @dts-jest:pass:snap -> AugmentedMutable<Readonly<Props>, "name" | "age" | "visible">
testType<AugmentedMutable<Readonly<Props>>>({
name: 'Yolo',
age: 99,
visible: true,
});

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

// @dts-jest:pass:snap -> string
testType<Mutable<Readonly<Props>>['name']>('Yolo');
testType<AugmentedMutable<Readonly<Props>>['name']>('Yolo');

// @dts-jest:pass:snap -> number
testType<AugmentedMutable<Readonly<Props>>['age']>(99);

// @dts-jest:pass:snap -> boolean
testType<AugmentedMutable<Readonly<Props>>['visible']>(true);
}

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

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

// @dts-jest:pass:snap -> "name" | "age"
testType<ReadonlyKeys<AugmentedReadonly<Props, 'name' | 'age'>>>();

// @dts-jest:pass:snap -> string
testType<AugmentedReadonly<Props>['name']>();

// @dts-jest:pass:snap -> number
testType<Mutable<Readonly<Props>>['age']>(99);
testType<AugmentedReadonly<Props>['age']>();

// @dts-jest:pass:snap -> boolean
testType<Mutable<Readonly<Props>>['visible']>(true);
testType<AugmentedReadonly<Props>['visible']>();
}
Loading