Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Pelle Wessman <pelle@kodfabrik.se> Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
ce5b200
commit 673c1aa
Showing
6 changed files
with
62 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,38 @@ | ||
import {Except} from './except'; | ||
import {Simplify} from './simplify'; | ||
|
||
/** | ||
Convert an object with `readonly` keys into a mutable object. Inverse of `Readonly<T>`. | ||
Create a type that strips `readonly` from all or some of an object's keys. Inverse of `Readonly<T>`. | ||
This can be used to [store and mutate options within a class](https://github.com/sindresorhus/pageres/blob/4a5d05fca19a5fbd2f53842cbf3eb7b1b63bddd2/source/index.ts#L72), [edit `readonly` objects within tests](https://stackoverflow.com/questions/50703834), and [construct a `readonly` object within a function](https://github.com/Microsoft/TypeScript/issues/24509). | ||
This can be used to [store and mutate options within a class](https://github.com/sindresorhus/pageres/blob/4a5d05fca19a5fbd2f53842cbf3eb7b1b63bddd2/source/index.ts#L72), [edit `readonly` objects within tests](https://stackoverflow.com/questions/50703834), [construct a `readonly` object within a function](https://github.com/Microsoft/TypeScript/issues/24509), or to define a single model where the only thing that changes is whether or not some of the keys are mutable. | ||
@example | ||
``` | ||
import {Mutable} from 'type-fest'; | ||
type Foo = { | ||
readonly a: number; | ||
readonly b: string; | ||
readonly b: readonly string[]; // To show that only the mutability status of the properties, not their values, are affected. | ||
readonly c: boolean; | ||
}; | ||
const mutableFoo: Mutable<Foo> = {a: 1, b: '2'}; | ||
const mutableFoo: Mutable<Foo> = {a: 1, b: ['2']}; | ||
mutableFoo.a = 3; | ||
mutableFoo.b[0] = 'new value'; // Will still fail as the value of property "b" is still a readonly type. | ||
mutableFoo.b = ['something']; // Will work as the "b" property itself is no longer readonly. | ||
type SomeMutable = Mutable<Foo, 'b' | 'c'>; | ||
// type SomeMutable = { | ||
// readonly a: number; | ||
// b: readonly string[]; // It's now mutable. The type of the property remains unaffected. | ||
// c: boolean; // It's now mutable. | ||
// } | ||
``` | ||
*/ | ||
export type Mutable<ObjectType> = { | ||
// For each `Key` in the keys of `ObjectType`, make a mapped type by removing the `readonly` modifier from the key. | ||
-readonly [KeyType in keyof ObjectType]: ObjectType[KeyType]; | ||
}; | ||
export type Mutable<BaseType, Keys extends keyof BaseType = keyof BaseType> = | ||
Simplify< | ||
// Pick just the keys that are not mutable from the base type. | ||
Except<BaseType, Keys> & | ||
// Pick the keys that should be mutable from the base type and make them mutable by removing the `readonly` modifier from the key. | ||
{-readonly [KeyType in keyof Pick<BaseType, Keys>]: Pick<BaseType, Keys>[KeyType]} | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
Flatten the type output to improve type hints shown in editors. | ||
*/ | ||
export type Simplify<T> = {[KeyType in keyof T]: T[KeyType]}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters