type-fest 2.14.0 → 2.15.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/package.json +1 -1
- package/source/jsonify.d.ts +17 -12
- package/source/numeric.d.ts +2 -0
package/package.json
CHANGED
package/source/jsonify.d.ts
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
import type {JsonPrimitive, JsonValue} from './basic';
|
|
2
|
+
import {Finite, NegativeInfinity, PositiveInfinity} from './numeric';
|
|
3
|
+
import {TypedArray} from './typed-array';
|
|
2
4
|
|
|
3
5
|
// Note: The return value has to be `any` and not `unknown` so it can match `void`.
|
|
4
|
-
type NotJsonable = ((...args: any[]) => any) | undefined;
|
|
5
|
-
|
|
6
|
-
// Note: Handles special case where Arrays with `undefined` are transformed to `'null'` by `JSON.stringify()`
|
|
7
|
-
// Only use with array members
|
|
8
|
-
type JsonifyArrayMember<T> =
|
|
9
|
-
T extends undefined ?
|
|
10
|
-
null | Exclude<T, undefined> :
|
|
11
|
-
Jsonify<T>;
|
|
6
|
+
type NotJsonable = ((...args: any[]) => any) | undefined | symbol;
|
|
12
7
|
|
|
13
8
|
/**
|
|
14
9
|
Transform a type to one that is assignable to the `JsonValue` type.
|
|
@@ -71,16 +66,26 @@ type Jsonify<T> =
|
|
|
71
66
|
// Check if there are any non-JSONable types represented in the union.
|
|
72
67
|
// Note: The use of tuples in this first condition side-steps distributive conditional types
|
|
73
68
|
// (see https://github.com/microsoft/TypeScript/issues/29368#issuecomment-453529532)
|
|
74
|
-
[Extract<T, NotJsonable>] extends [never]
|
|
75
|
-
? T extends
|
|
69
|
+
[Extract<T, NotJsonable | BigInt>] extends [never]
|
|
70
|
+
? T extends PositiveInfinity | NegativeInfinity ? null
|
|
71
|
+
: T extends JsonPrimitive
|
|
76
72
|
? T // Primitive is acceptable
|
|
73
|
+
: T extends Number ? number
|
|
74
|
+
: T extends String ? string
|
|
75
|
+
: T extends Boolean ? boolean
|
|
76
|
+
: T extends Map<any, any> | Set<any> ? {}
|
|
77
|
+
: T extends TypedArray ? Record<string, number>
|
|
77
78
|
: T extends Array<infer U>
|
|
78
|
-
? Array<
|
|
79
|
+
? Array<Jsonify<U extends NotJsonable ? null : U>> // It's an array: recursive call for its children
|
|
79
80
|
: T extends object
|
|
80
81
|
? T extends {toJSON(): infer J}
|
|
81
82
|
? (() => J) extends (() => JsonValue) // Is J assignable to JsonValue?
|
|
82
83
|
? J // Then T is Jsonable and its Jsonable value is J
|
|
83
84
|
: never // Not Jsonable because its toJSON() method does not return JsonValue
|
|
84
|
-
: {[P in keyof T
|
|
85
|
+
: {[P in keyof T as P extends symbol
|
|
86
|
+
? never
|
|
87
|
+
: T[P] extends NotJsonable
|
|
88
|
+
? never
|
|
89
|
+
: P]: Jsonify<Required<T>[P]>} // It's an object: recursive call for its children
|
|
85
90
|
: never // Otherwise any other non-object is removed
|
|
86
91
|
: never; // Otherwise non-JSONable type union was found not empty
|
package/source/numeric.d.ts
CHANGED
|
@@ -34,6 +34,8 @@ You can't pass a `bigint` as they are already guaranteed to be finite.
|
|
|
34
34
|
|
|
35
35
|
Use-case: Validating and documenting parameters.
|
|
36
36
|
|
|
37
|
+
Note: This can't detect `NaN`, please upvote [this issue](https://github.com/microsoft/TypeScript/issues/28682) if you want to have this type as a built-in in TypeScript.
|
|
38
|
+
|
|
37
39
|
@example
|
|
38
40
|
```
|
|
39
41
|
import type {Finite} from 'type-fest';
|