main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
860 bytes
|
Line | |
---|
1 | /**
|
---|
2 | Convert an object with `readonly` keys into a mutable object. Inverse of `Readonly<T>`.
|
---|
3 |
|
---|
4 | 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).
|
---|
5 |
|
---|
6 | @example
|
---|
7 | ```
|
---|
8 | import {Mutable} from 'type-fest';
|
---|
9 |
|
---|
10 | type Foo = {
|
---|
11 | readonly a: number;
|
---|
12 | readonly b: string;
|
---|
13 | };
|
---|
14 |
|
---|
15 | const mutableFoo: Mutable<Foo> = {a: 1, b: '2'};
|
---|
16 | mutableFoo.a = 3;
|
---|
17 | ```
|
---|
18 | */
|
---|
19 | export type Mutable<ObjectType> = {
|
---|
20 | // For each `Key` in the keys of `ObjectType`, make a mapped type by removing the `readonly` modifier from the key.
|
---|
21 | -readonly [KeyType in keyof ObjectType]: ObjectType[KeyType];
|
---|
22 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.