type-fest 3.5.7 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export * from './source/observable-like';
8
8
  export type {EmptyObject, IsEmptyObject} from './source/empty-object';
9
9
  export type {Except} from './source/except';
10
10
  export type {Writable} from './source/writable';
11
+ export type {WritableDeep} from './source/writable-deep';
11
12
  export type {Merge} from './source/merge';
12
13
  export type {MergeDeep, MergeDeepOptions} from './source/merge-deep';
13
14
  export type {MergeExclusive} from './source/merge-exclusive';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "3.5.7",
3
+ "version": "3.6.0",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
package/readme.md CHANGED
@@ -129,6 +129,7 @@ Click the type names for complete docs.
129
129
  - [`IsEmptyObject`](source/empty-object.d.ts) - Returns a `boolean` for whether the type is strictly equal to an empty plain object, the `{}` value.
130
130
  - [`Except`](source/except.d.ts) - Create a type from an object type without certain keys. This is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys).
131
131
  - [`Writable`](source/writable.d.ts) - Create a type that strips `readonly` from all or some of an object's keys. The inverse of `Readonly<T>`.
132
+ - [`WritableDeep`](source/writable-deep.d.ts) - Create a deeply mutable version of an `object`/`ReadonlyMap`/`ReadonlySet`/`ReadonlyArray` type. The inverse of `ReadonlyDeep<T>`. Use `Writable<T>` if you only need one level deep.
132
133
  - [`Merge`](source/merge.d.ts) - Merge two types into a new type. Keys of the second type overrides keys of the first type.
133
134
  - [`MergeDeep`](source/merge-deep.d.ts) - Merge two objects or two arrays/tuples recursively into a new type.
134
135
  - [`MergeExclusive`](source/merge-exclusive.d.ts) - Create a type that has mutually exclusive keys.
@@ -242,3 +242,18 @@ type FilterOptionalKeys<T extends object> = Exclude<
242
242
  }[keyof T],
243
243
  undefined
244
244
  >;
245
+
246
+ /**
247
+ Test if the given function has multiple call signatures.
248
+
249
+ Needed to handle the case of a single call signature with properties.
250
+
251
+ Multiple call signatures cannot currently be supported due to a TypeScript limitation.
252
+ @see https://github.com/microsoft/TypeScript/issues/29732
253
+ */
254
+ export type HasMultipleCallSignatures<T extends (...arguments: any[]) => unknown> =
255
+ T extends {(...arguments: infer A): unknown; (...arguments: any[]): unknown}
256
+ ? unknown[] extends A
257
+ ? false
258
+ : true
259
+ : false;
@@ -224,7 +224,10 @@ declare namespace PackageJson {
224
224
  string
225
225
  >;
226
226
 
227
- type ExportConditions = {[condition in ExportCondition]: Exports};
227
+ /**
228
+ A mapping of conditions and the paths to which they resolve.
229
+ */
230
+ type ExportConditions = {[condition in ExportCondition]?: Exports};
228
231
 
229
232
  /**
230
233
  Entry points of a module, optionally with conditions and subpath exports.
@@ -233,14 +236,13 @@ declare namespace PackageJson {
233
236
  | null
234
237
  | string
235
238
  | Array<string | ExportConditions>
236
- | ExportConditions
237
- | {[path: string]: Exports};
239
+ | ExportConditions;
238
240
 
239
241
  /**
240
- Import map entries of a module, optionally with conditions.
242
+ Import map entries of a module, optionally with conditions and subpath imports.
241
243
  */
242
244
  export type Imports = { // eslint-disable-line @typescript-eslint/consistent-indexed-object-style
243
- [key: `#${string}`]: string | {[key in ExportCondition]?: Exports};
245
+ [key: `#${string}`]: Exports;
244
246
  };
245
247
 
246
248
  // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
@@ -1,4 +1,4 @@
1
- import type {BuiltIns} from './internal';
1
+ import type {BuiltIns, HasMultipleCallSignatures} from './internal';
2
2
 
3
3
  /**
4
4
  Convert `object`s, `Map`s, `Set`s, and `Array`s and all of their keys/elements into immutable structures recursively.
@@ -29,6 +29,8 @@ data.foo.push('bar');
29
29
  //=> error TS2339: Property 'push' does not exist on type 'readonly string[]'
30
30
  ```
31
31
 
32
+ Note that types containing overloaded functions are not made deeply readonly due to a [TypeScript limitation](https://github.com/microsoft/TypeScript/issues/29732).
33
+
32
34
  @category Object
33
35
  @category Array
34
36
  @category Set
@@ -66,18 +68,3 @@ Same as `ReadonlyDeep`, but accepts only `object`s as inputs. Internal helper fo
66
68
  type ReadonlyObjectDeep<ObjectType extends object> = {
67
69
  readonly [KeyType in keyof ObjectType]: ReadonlyDeep<ObjectType[KeyType]>
68
70
  };
69
-
70
- /**
71
- Test if the given function has multiple call signatures.
72
-
73
- Needed to handle the case of a single call signature with properties.
74
-
75
- Multiple call signatures cannot currently be supported due to a TypeScript limitation.
76
- @see https://github.com/microsoft/TypeScript/issues/29732
77
- */
78
- type HasMultipleCallSignatures<T extends (...arguments: any[]) => unknown> =
79
- T extends {(...arguments: infer A): unknown; (...arguments: any[]): unknown}
80
- ? unknown[] extends A
81
- ? false
82
- : true
83
- : false;
@@ -0,0 +1,64 @@
1
+ import type {BuiltIns, HasMultipleCallSignatures} from './internal';
2
+ import type {Writable} from './writable.js';
3
+
4
+ /**
5
+ Create a deeply mutable version of an `object`/`ReadonlyMap`/`ReadonlySet`/`ReadonlyArray` type. The inverse of `ReadonlyDeep<T>`. Use `Writable<T>` if you only need one level deep.
6
+
7
+ 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 writable.
8
+
9
+ @example
10
+ ```
11
+ import type {WritableDeep} from 'type-fest';
12
+
13
+ type Foo = {
14
+ readonly a: number;
15
+ readonly b: readonly string[]; // To show that mutability is deeply affected.
16
+ readonly c: boolean;
17
+ };
18
+
19
+ const writableDeepFoo: WritableDeep<Foo> = {a: 1, b: ['2'], c: true};
20
+ writableDeepFoo.a = 3;
21
+ writableDeepFoo.b[0] = 'new value';
22
+ writableDeepFoo.b = ['something'];
23
+ ```
24
+
25
+ Note that types containing overloaded functions are not made deeply writable due to a [TypeScript limitation](https://github.com/microsoft/TypeScript/issues/29732).
26
+
27
+ @see Writable
28
+ @category Object
29
+ @category Array
30
+ @category Set
31
+ @category Map
32
+ */
33
+ export type WritableDeep<T> = T extends BuiltIns
34
+ ? T
35
+ : T extends (...arguments: any[]) => unknown
36
+ ? {} extends WritableObjectDeep<T>
37
+ ? T
38
+ : HasMultipleCallSignatures<T> extends true
39
+ ? T
40
+ : ((...arguments: Parameters<T>) => ReturnType<T>) & WritableObjectDeep<T>
41
+ : T extends Readonly<ReadonlyMap<infer KeyType, infer ValueType>>
42
+ ? WritableMapDeep<KeyType, ValueType>
43
+ : T extends Readonly<ReadonlySet<infer ItemType>>
44
+ ? WritableSetDeep<ItemType>
45
+ : T extends object
46
+ ? WritableObjectDeep<T>
47
+ : unknown;
48
+
49
+ /**
50
+ Same as `WritableDeep`, but accepts only `Map`s as inputs. Internal helper for `WritableDeep`.
51
+ */
52
+ type WritableMapDeep<KeyType, ValueType> = {} & Writable<Map<WritableDeep<KeyType>, WritableDeep<ValueType>>>;
53
+
54
+ /**
55
+ Same as `WritableDeep`, but accepts only `Set`s as inputs. Internal helper for `WritableDeep`.
56
+ */
57
+ type WritableSetDeep<ItemType> = {} & Writable<Set<WritableDeep<ItemType>>>;
58
+
59
+ /**
60
+ Same as `WritableDeep`, but accepts only `object`s as inputs. Internal helper for `WritableDeep`.
61
+ */
62
+ type WritableObjectDeep<ObjectType extends object> = {
63
+ -readonly [KeyType in keyof ObjectType]: WritableDeep<ObjectType[KeyType]>
64
+ };