type-fest 4.1.0 → 4.2.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
@@ -6,6 +6,7 @@ export * from './source/observable-like';
6
6
 
7
7
  // Utilities
8
8
  export type {EmptyObject, IsEmptyObject} from './source/empty-object';
9
+ export type {UnknownRecord} from './source/unknown-record';
9
10
  export type {Except} from './source/except';
10
11
  export type {TaggedUnion} from './source/tagged-union';
11
12
  export type {Writable} from './source/writable';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "4.1.0",
3
+ "version": "4.2.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
@@ -110,6 +110,7 @@ Click the type names for complete docs.
110
110
 
111
111
  - [`EmptyObject`](source/empty-object.d.ts) - Represents a strictly empty plain object, the `{}` value.
112
112
  - [`IsEmptyObject`](source/empty-object.d.ts) - Returns a `boolean` for whether the type is strictly equal to an empty plain object, the `{}` value.
113
+ - [`UnknownRecord`](source/unknown-record.d.ts) - Represents an object with `unknown` value. You probably want this instead of `{}`.
113
114
  - [`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).
114
115
  - [`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>`.
115
116
  - [`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.
@@ -3,6 +3,9 @@ import type {Simplify} from './simplify';
3
3
  import type {Trim} from './trim';
4
4
  import type {IsAny} from './is-any';
5
5
 
6
+ // TODO: Remove for v5.
7
+ export type {UnknownRecord} from './unknown-record';
8
+
6
9
  /**
7
10
  Infer the length of the given array `<T>`.
8
11
 
@@ -77,11 +80,6 @@ export type Whitespace =
77
80
 
78
81
  export type WordSeparators = '-' | '_' | Whitespace;
79
82
 
80
- /**
81
- Matches any unknown record.
82
- */
83
- export type UnknownRecord = Record<PropertyKey, unknown>;
84
-
85
83
  /**
86
84
  Matches any unknown array or tuple.
87
85
  */
@@ -9,8 +9,8 @@ import type {
9
9
  IsBothExtends,
10
10
  NonEmptyTuple,
11
11
  UnknownArrayOrTuple,
12
- UnknownRecord,
13
12
  } from './internal';
13
+ import type {UnknownRecord} from './unknown-record';
14
14
 
15
15
  /**
16
16
  Deeply simplifies an object excluding iterables and functions. Used internally to improve the UX and accept both interfaces and type aliases as inputs.
@@ -48,9 +48,18 @@ export type ReadonlyDeep<T> = T extends BuiltIns
48
48
  ? ReadonlyMapDeep<KeyType, ValueType>
49
49
  : T extends Readonly<ReadonlySet<infer ItemType>>
50
50
  ? ReadonlySetDeep<ItemType>
51
- : T extends object
52
- ? ReadonlyObjectDeep<T>
53
- : unknown;
51
+ : // Identify tuples to avoid converting them to arrays inadvertently; special case `readonly [...never[]]`, as it emerges undesirably from recursive invocations of ReadonlyDeep below.
52
+ T extends readonly [] | readonly [...never[]]
53
+ ? readonly []
54
+ : T extends readonly [infer U, ...infer V]
55
+ ? readonly [ReadonlyDeep<U>, ...ReadonlyDeep<V>]
56
+ : T extends readonly [...infer U, infer V]
57
+ ? readonly [...ReadonlyDeep<U>, ReadonlyDeep<V>]
58
+ : T extends ReadonlyArray<infer ItemType>
59
+ ? ReadonlyArray<ReadonlyDeep<ItemType>>
60
+ : T extends object
61
+ ? ReadonlyObjectDeep<T>
62
+ : unknown;
54
63
 
55
64
  /**
56
65
  Same as `ReadonlyDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `ReadonlyDeep`.
@@ -0,0 +1,31 @@
1
+ /**
2
+ Represents an object with `unknown` value. You probably want this instead of `{}`.
3
+
4
+ Use case: You have an object whose keys and values are unknown to you.
5
+
6
+ @example
7
+ ```
8
+ import type {UnknownRecord} from 'type-fest';
9
+
10
+ function toJson(object: UnknownRecord) {
11
+ return JSON.stringify(object);
12
+ }
13
+
14
+ toJson({hello: 'world'});
15
+ //=> '{"hello":"world"}'
16
+
17
+ function isObject(value: unknown): value is UnknownRecord {
18
+ return typeof value === 'object' && value !== null;
19
+ }
20
+
21
+ isObject({hello: 'world'});
22
+ //=> true
23
+
24
+ isObject('hello');
25
+ //=> false
26
+ ```
27
+
28
+ @category Type
29
+ @category Object
30
+ */
31
+ export type UnknownRecord = Record<PropertyKey, unknown>;