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
Next Next commit
Add AugmentedMutable type
  • Loading branch information
wbadam committed Dec 9, 2019
commit 8801dfd56b2324c4af8fa418e21e308fe45d5667
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ We are open for contributions. If you're planning to contribute please make sure
* [`DeepRequired<T>`](#deeprequiredt)
* [`Readonly<T>`](#readonlyt) _(built-in)_
* [`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 @@ -678,11 +678,11 @@ Make all properties of object type readonly

[⇧ 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 +695,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
30 changes: 18 additions & 12 deletions src/__snapshots__/mapped-types.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ 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[`AugmentedRequired testType<AugmentedRequired<Partial<Props>, 'age' | 'visible'>>({
age: 99,
visible: true,
Expand Down Expand Up @@ -111,18 +129,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
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export {
Diff,
FunctionKeys,
Intersection,
Mutable,
AugmentedMutable as Mutable,
MutableKeys,
NonFunctionKeys,
NonUndefined,
Expand Down
21 changes: 14 additions & 7 deletions src/mapped-types.spec.snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
ValuesType,
AugmentedRequired,
UnionToIntersection,
Mutable,
AugmentedMutable,
} from './mapped-types';

/**
Expand Down Expand Up @@ -572,21 +572,28 @@ 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<Mutable<Readonly<Props>>['age']>(99);
testType<AugmentedMutable<Readonly<Props>>['age']>(99);

// @dts-jest:pass:snap -> boolean
testType<Mutable<Readonly<Props>>['visible']>(true);
testType<AugmentedMutable<Readonly<Props>>['visible']>(true);
}
19 changes: 13 additions & 6 deletions src/mapped-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
ValuesType,
AugmentedRequired,
UnionToIntersection,
Mutable,
AugmentedMutable,
} from './mapped-types';

/**
Expand Down Expand Up @@ -572,21 +572,28 @@ type RequiredOptionalProps = {
testType<UnionToIntersection<'name' | 'age'>>();
}

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

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

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

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

// @dts-jest:pass:snap
testType<Mutable<Readonly<Props>>['visible']>(true);
testType<AugmentedMutable<Readonly<Props>>['visible']>(true);
}
12 changes: 9 additions & 3 deletions src/mapped-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,11 @@ export type UnionToIntersection<U> = (U extends any
? I
: never;

type Mutable<T> = { -readonly [P in keyof T]: T[P] };

/**
* Mutable
* @desc From `T` make all properties become mutable
* @desc From `T`, make a set of properties, whose keys are in the union `K`, mutable
* @example
* type Props = {
* readonly name: string;
Expand All @@ -644,6 +646,10 @@ export type UnionToIntersection<U> = (U extends any
*
* // Expect: { name: string; age: number; visible: boolean; }
* Mutable<Props>;
*
* // Expect: { readonly name: string; age: number; visible: boolean; }
* Mutable<Props, 'age' | 'visible'>;
*/
export type Mutable<T> = { -readonly [P in keyof T]: T[P] };
export type Writable<T> = Mutable<T>;
export type AugmentedMutable<T, K extends keyof T = keyof T> = Omit<T, K> &
Mutable<Pick<T, K>>;
export type Writable<T, K extends keyof T = keyof T> = AugmentedMutable<T, K>;