type-fest 4.3.1 → 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/readme.md +4 -1
- package/source/internal.d.ts +27 -3
- package/source/is-literal.d.ts +5 -2
- package/source/jsonify.d.ts +13 -13
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -77,6 +77,8 @@ npm install type-fest
|
|
|
77
77
|
|
|
78
78
|
*Requires TypeScript >=5.1*
|
|
79
79
|
|
|
80
|
+
*Works best with [`{strict: true}`](https://www.typescriptlang.org/tsconfig#strict) in your tsconfig.*
|
|
81
|
+
|
|
80
82
|
## Usage
|
|
81
83
|
|
|
82
84
|
```ts
|
|
@@ -291,10 +293,11 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
|
|
|
291
293
|
|
|
292
294
|
*If you know one of our types by a different name, add it here for discovery.*
|
|
293
295
|
|
|
296
|
+
- `Prettify`- See [`Simplify`](https://github.com/sindresorhus/type-fest/blob/main/source/simplify.d.ts)
|
|
297
|
+
- `Expand`- See [`Simplify`](https://github.com/sindresorhus/type-fest/blob/main/source/simplify.d.ts)
|
|
294
298
|
- `PartialBy` - See [`SetOptional`](https://github.com/sindresorhus/type-fest/blob/main/source/set-optional.d.ts)
|
|
295
299
|
- `RecordDeep`- See [`Schema`](https://github.com/sindresorhus/type-fest/blob/main/source/schema.d.ts)
|
|
296
300
|
- `Mutable`- See [`Writable`](https://github.com/sindresorhus/type-fest/blob/main/source/writable.d.ts)
|
|
297
|
-
- `Prettify`- See [`Simplify`](https://github.com/sindresorhus/type-fest/blob/main/source/simplify.d.ts)
|
|
298
301
|
- `RequireOnlyOne` - See [`RequireExactlyOne`](https://github.com/sindresorhus/type-fest/blob/main/source/require-exactly-one.d.ts)
|
|
299
302
|
- `AtMostOne` - See [`RequireOneOrNone`](https://github.com/sindresorhus/type-fest/blob/main/source/require-one-or-none.d.ts)
|
|
300
303
|
|
package/source/internal.d.ts
CHANGED
|
@@ -189,9 +189,16 @@ type BaseKeyFilter<Type, Key extends keyof Type> = Key extends symbol
|
|
|
189
189
|
? never
|
|
190
190
|
: Type[Key] extends symbol
|
|
191
191
|
? never
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
192
|
+
/*
|
|
193
|
+
To prevent a problem where an object with only a `name` property is incorrectly treated as assignable to a function, we first check if the property is a record.
|
|
194
|
+
This check is necessary, because without it, if we don't verify whether the property is a record, an object with a type of `{name: any}` would return `never` due to its potential assignability to a function.
|
|
195
|
+
See: https://github.com/sindresorhus/type-fest/issues/657
|
|
196
|
+
*/
|
|
197
|
+
: Type[Key] extends Record<string, unknown>
|
|
198
|
+
? Key
|
|
199
|
+
: [(...arguments_: any[]) => any] extends [Type[Key]]
|
|
200
|
+
? never
|
|
201
|
+
: Key;
|
|
195
202
|
|
|
196
203
|
/**
|
|
197
204
|
Returns the required keys.
|
|
@@ -254,3 +261,20 @@ export type IsNull<T> = [T] extends [null] ? true : false;
|
|
|
254
261
|
Disallows any of the given keys.
|
|
255
262
|
*/
|
|
256
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
|