type-fest 4.3.2 → 4.3.3
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/internal.d.ts +17 -0
- package/source/is-literal.d.ts +5 -2
- package/source/jsonify.d.ts +13 -13
package/package.json
CHANGED
package/source/internal.d.ts
CHANGED
|
@@ -261,3 +261,20 @@ export type IsNull<T> = [T] extends [null] ? true : false;
|
|
|
261
261
|
Disallows any of the given keys.
|
|
262
262
|
*/
|
|
263
263
|
export type RequireNone<KeysType extends PropertyKey> = Partial<Record<KeysType, never>>;
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
Returns a boolean for whether the given type is primitive value or primitive type.
|
|
267
|
+
|
|
268
|
+
@example
|
|
269
|
+
```
|
|
270
|
+
IsPrimitive<'string'>
|
|
271
|
+
//=> true
|
|
272
|
+
|
|
273
|
+
IsPrimitive<string>
|
|
274
|
+
//=> true
|
|
275
|
+
|
|
276
|
+
IsPrimitive<Object>
|
|
277
|
+
//=> false
|
|
278
|
+
```
|
|
279
|
+
*/
|
|
280
|
+
export type IsPrimitive<T> = [T] extends [Primitive] ? true : false;
|
package/source/is-literal.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type {Primitive} from './primitive';
|
|
2
2
|
import type {Numeric} from './numeric';
|
|
3
|
-
import type {IsNotFalse} from './internal';
|
|
3
|
+
import type {IsNotFalse, IsPrimitive} from './internal';
|
|
4
4
|
import type {IsNever} from './is-never';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -250,4 +250,7 @@ stripLeading(str, 'abc');
|
|
|
250
250
|
@category Type Guard
|
|
251
251
|
@category Utilities
|
|
252
252
|
*/
|
|
253
|
-
export type IsLiteral<T
|
|
253
|
+
export type IsLiteral<T> =
|
|
254
|
+
IsPrimitive<T> extends true
|
|
255
|
+
? IsNotFalse<IsLiteralUnion<T>>
|
|
256
|
+
: false;
|
package/source/jsonify.d.ts
CHANGED
|
@@ -3,24 +3,24 @@ import type {EmptyObject} from './empty-object';
|
|
|
3
3
|
import type {UndefinedToOptional} from './internal';
|
|
4
4
|
import type {IsAny} from './is-any';
|
|
5
5
|
import type {IsNever} from './is-never';
|
|
6
|
+
import type {IsUnknown} from './is-unknown';
|
|
6
7
|
import type {NegativeInfinity, PositiveInfinity} from './numeric';
|
|
7
8
|
import type {TypedArray} from './typed-array';
|
|
9
|
+
import type {WritableDeep} from './writable-deep';
|
|
8
10
|
|
|
9
11
|
// Note: The return value has to be `any` and not `unknown` so it can match `void`.
|
|
10
12
|
type NotJsonable = ((...arguments_: any[]) => any) | undefined | symbol;
|
|
11
13
|
|
|
12
|
-
type
|
|
13
|
-
? IsNever<F> extends true
|
|
14
|
-
? FilterNonNever<R>
|
|
15
|
-
: [F, ...FilterNonNever<R>]
|
|
16
|
-
: IsNever<T[number]> extends true
|
|
17
|
-
? []
|
|
18
|
-
: T;
|
|
14
|
+
type NeverToNull<T> = IsNever<T> extends true ? null : T;
|
|
19
15
|
|
|
20
16
|
// Handles tuples and arrays
|
|
21
|
-
type JsonifyList<T extends unknown[]> = T extends [
|
|
22
|
-
?
|
|
23
|
-
:
|
|
17
|
+
type JsonifyList<T extends unknown[]> = T extends []
|
|
18
|
+
? []
|
|
19
|
+
: T extends [infer F, ...infer R]
|
|
20
|
+
? [NeverToNull<Jsonify<F>>, ...JsonifyList<R>]
|
|
21
|
+
: IsUnknown<T[number]> extends true
|
|
22
|
+
? []
|
|
23
|
+
: Array<T[number] extends NotJsonable ? null : Jsonify<T[number]>>;
|
|
24
24
|
|
|
25
25
|
type FilterJsonableKeys<T extends object> = {
|
|
26
26
|
[Key in keyof T]: T[Key] extends NotJsonable ? never : Key;
|
|
@@ -116,10 +116,10 @@ export type Jsonify<T> = IsAny<T> extends true
|
|
|
116
116
|
: Jsonify<J> // Maybe if we look a level deeper we'll find a JsonValue
|
|
117
117
|
: T extends []
|
|
118
118
|
? []
|
|
119
|
-
: T extends
|
|
119
|
+
: T extends unknown[]
|
|
120
120
|
? JsonifyList<T>
|
|
121
|
-
: T extends
|
|
122
|
-
?
|
|
121
|
+
: T extends readonly unknown[]
|
|
122
|
+
? JsonifyList<WritableDeep<T>>
|
|
123
123
|
: T extends object
|
|
124
124
|
? JsonifyObject<UndefinedToOptional<T>> // JsonifyObject recursive call for its children
|
|
125
125
|
: never; // Otherwise any other non-object is removed
|