UNPKG

3.04 kBTypeScriptView Raw
1import type {Except} from './except';
2import type {Simplify} from './simplify';
3
4/**
5Create a writable version of the given array type.
6*/
7type WritableArray<ArrayType extends readonly unknown[]> =
8 ArrayType extends readonly [] ? []
9 : ArrayType extends readonly [...infer U, infer V] ? [...U, V]
10 : ArrayType extends readonly [infer U, ...infer V] ? [U, ...V]
11 : ArrayType extends ReadonlyArray<infer U> ? U[]
12 : ArrayType;
13
14/**
15Create a type that strips `readonly` from the given type. Inverse of `Readonly<T>`.
16
17The 2nd argument will be ignored if the input type is not an object.
18
19Note: This type can make readonly `Set` and `Map` writable. This behavior is different from `Readonly<T>` (as of TypeScript 5.2.2). See: https://github.com/microsoft/TypeScript/issues/29655
20
21This 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 writable.
22
23@example
24```
25import type {Writable} from 'type-fest';
26
27type Foo = {
28 readonly a: number;
29 readonly b: readonly string[]; // To show that only the mutability status of the properties, not their values, are affected.
30 readonly c: boolean;
31};
32
33const writableFoo: Writable<Foo> = {a: 1, b: ['2'], c: true};
34writableFoo.a = 3;
35writableFoo.b[0] = 'new value'; // Will still fail as the value of property "b" is still a readonly type.
36writableFoo.b = ['something']; // Will work as the "b" property itself is no longer readonly.
37
38type SomeWritable = Writable<Foo, 'b' | 'c'>;
39// type SomeWritable = {
40// readonly a: number;
41// b: readonly string[]; // It's now writable. The type of the property remains unaffected.
42// c: boolean; // It's now writable.
43// }
44
45// Also supports array
46const readonlyArray: readonly number[] = [1, 2, 3];
47readonlyArray.push(4); // Will fail as the array itself is readonly.
48const writableArray: Writable<typeof readonlyArray> = readonlyArray as Writable<typeof readonlyArray>;
49writableArray.push(4); // Will work as the array itself is now writable.
50```
51
52@category Object
53*/
54export type Writable<BaseType, Keys extends keyof BaseType = keyof BaseType> =
55BaseType extends ReadonlyMap<infer KeyType, infer ValueType>
56 ? Map<KeyType, ValueType>
57 : BaseType extends ReadonlySet<infer ItemType>
58 ? Set<ItemType>
59 : BaseType extends readonly unknown[]
60 // Handle array
61 ? WritableArray<BaseType>
62 // Handle object
63 : Simplify<
64 // Pick just the keys that are not writable from the base type.
65 Except<BaseType, Keys> &
66 // Pick the keys that should be writable from the base type and make them writable by removing the `readonly` modifier from the key.
67 {-readonly [KeyType in keyof Pick<BaseType, Keys>]: Pick<BaseType, Keys>[KeyType]}
68 >;