source: trip-planner-front/node_modules/type-fest/source/mutable.d.ts@ bdd6491

Last change on this file since bdd6491 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.8 KB
Line 
1import {Except} from './except';
2import {Simplify} from './simplify';
3
4/**
5Create a type that strips `readonly` from all or some of an object's keys. Inverse of `Readonly<T>`.
6
7This 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.
8
9@example
10```
11import {Mutable} from 'type-fest';
12
13type Foo = {
14 readonly a: number;
15 readonly b: readonly string[]; // To show that only the mutability status of the properties, not their values, are affected.
16 readonly c: boolean;
17};
18
19const mutableFoo: Mutable<Foo> = {a: 1, b: ['2']};
20mutableFoo.a = 3;
21mutableFoo.b[0] = 'new value'; // Will still fail as the value of property "b" is still a readonly type.
22mutableFoo.b = ['something']; // Will work as the "b" property itself is no longer readonly.
23
24type SomeMutable = Mutable<Foo, 'b' | 'c'>;
25// type SomeMutable = {
26// readonly a: number;
27// b: readonly string[]; // It's now mutable. The type of the property remains unaffected.
28// c: boolean; // It's now mutable.
29// }
30```
31*/
32export type Mutable<BaseType, Keys extends keyof BaseType = keyof BaseType> =
33 Simplify<
34 // Pick just the keys that are not mutable from the base type.
35 Except<BaseType, Keys> &
36 // Pick the keys that should be mutable from the base type and make them mutable by removing the `readonly` modifier from the key.
37 {-readonly [KeyType in keyof Pick<BaseType, Keys>]: Pick<BaseType, Keys>[KeyType]}
38 >;
Note: See TracBrowser for help on using the repository browser.