type-fest 4.20.0 → 4.21.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 +1 -0
- package/package.json +3 -2
- package/readme.md +8 -1
- package/source/camel-cased-properties-deep.d.ts +2 -1
- package/source/exact.d.ts +4 -5
- package/source/int-range.d.ts +1 -1
- package/source/iterable-element.d.ts +12 -2
- package/source/jsonify.d.ts +2 -1
- package/source/paths.d.ts +14 -8
- package/source/schema.d.ts +12 -12
- package/source/structured-cloneable.d.ts +89 -0
package/index.d.ts
CHANGED
|
@@ -64,6 +64,7 @@ export type {Simplify} from './source/simplify';
|
|
|
64
64
|
export type {SimplifyDeep} from './source/simplify-deep';
|
|
65
65
|
export type {Jsonify} from './source/jsonify';
|
|
66
66
|
export type {Jsonifiable} from './source/jsonifiable';
|
|
67
|
+
export type {StructuredCloneable} from './source/structured-cloneable';
|
|
67
68
|
export type {Schema} from './source/schema';
|
|
68
69
|
export type {LiteralToPrimitive} from './source/literal-to-primitive';
|
|
69
70
|
export type {LiteralToPrimitiveDeep} from './source/literal-to-primitive-deep';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "type-fest",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.21.0",
|
|
4
4
|
"description": "A collection of essential TypeScript types",
|
|
5
5
|
"license": "(MIT OR CC0-1.0)",
|
|
6
6
|
"repository": "sindresorhus/type-fest",
|
|
@@ -42,11 +42,12 @@
|
|
|
42
42
|
"expect-type": "^0.19.0",
|
|
43
43
|
"npm-run-all2": "^6.1.2",
|
|
44
44
|
"tsd": "^0.31.0",
|
|
45
|
-
"typescript": "~5.
|
|
45
|
+
"typescript": "~5.5.3",
|
|
46
46
|
"xo": "^0.58.0"
|
|
47
47
|
},
|
|
48
48
|
"xo": {
|
|
49
49
|
"rules": {
|
|
50
|
+
"@typescript-eslint/no-extraneous-class": "off",
|
|
50
51
|
"@typescript-eslint/ban-ts-comment": "off",
|
|
51
52
|
"@typescript-eslint/ban-types": "off",
|
|
52
53
|
"@typescript-eslint/naming-convention": "off",
|
package/readme.md
CHANGED
|
@@ -167,7 +167,7 @@ Click the type names for complete docs.
|
|
|
167
167
|
- [`LiteralToPrimitive`](source/literal-to-primitive.d.ts) - Convert a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types) to the [primitive type](source/primitive.d.ts) it belongs to.
|
|
168
168
|
- [`LiteralToPrimitiveDeep`](source/literal-to-primitive-deep.d.ts) - Like `LiteralToPrimitive` except it converts literal types inside an object or array deeply.
|
|
169
169
|
- [`Stringified`](source/stringified.d.ts) - Create a type with the keys of the given type changed to `string` type.
|
|
170
|
-
- [`IterableElement`](source/iterable-element.d.ts) - Get the element type of an `Iterable`/`AsyncIterable`. For example,
|
|
170
|
+
- [`IterableElement`](source/iterable-element.d.ts) - Get the element type of an `Iterable`/`AsyncIterable`. For example, `Array`, `Set`, `Map`, generator, stream, etc.
|
|
171
171
|
- [`Entry`](source/entry.d.ts) - Create a type that represents the type of an entry of a collection.
|
|
172
172
|
- [`Entries`](source/entries.d.ts) - Create a type that represents the type of the entries of a collection.
|
|
173
173
|
- [`SetReturnType`](source/set-return-type.d.ts) - Create a function type with a return type of your choice and the same parameters as the given function type.
|
|
@@ -249,6 +249,10 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
|
|
|
249
249
|
- [`JsonArray`](source/basic.d.ts) - Matches a JSON array.
|
|
250
250
|
- [`JsonValue`](source/basic.d.ts) - Matches any valid JSON value.
|
|
251
251
|
|
|
252
|
+
### Structured clone
|
|
253
|
+
|
|
254
|
+
- [`StructuredCloneable`](source/structured-cloneable.d.ts) - Matches a value that can be losslessly cloned using `structuredClone`.
|
|
255
|
+
|
|
252
256
|
### Async
|
|
253
257
|
|
|
254
258
|
- [`Promisable`](source/promisable.d.ts) - Create a type that represents either the value or the value wrapped in `PromiseLike`.
|
|
@@ -348,6 +352,9 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
|
|
|
348
352
|
- `AllKeys` - See [`KeysOfUnion`](source/keys-of-union.d.ts)
|
|
349
353
|
- `Branded` - See [`Tagged`](source/opaque.d.ts)
|
|
350
354
|
- `Opaque` - See [`Tagged`](source/opaque.d.ts)
|
|
355
|
+
- `SetElement` - See [`IterableElement`](source/iterable-element.d.ts)
|
|
356
|
+
- `SetEntry` - See [`IterableElement`](source/iterable-element.d.ts)
|
|
357
|
+
- `SetValues` - See [`IterableElement`](source/iterable-element.d.ts)
|
|
351
358
|
|
|
352
359
|
## Tips
|
|
353
360
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type {CamelCase, CamelCaseOptions} from './camel-case';
|
|
2
|
+
import type {NonRecursiveType} from './internal';
|
|
2
3
|
import type {UnknownArray} from './unknown-array';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -48,7 +49,7 @@ const result: CamelCasedPropertiesDeep<UserWithFriends> = {
|
|
|
48
49
|
export type CamelCasedPropertiesDeep<
|
|
49
50
|
Value,
|
|
50
51
|
Options extends CamelCaseOptions = {preserveConsecutiveUppercase: true},
|
|
51
|
-
> = Value extends
|
|
52
|
+
> = Value extends NonRecursiveType
|
|
52
53
|
? Value
|
|
53
54
|
: Value extends UnknownArray
|
|
54
55
|
? CamelCasedPropertiesArrayDeep<Value>
|
package/source/exact.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type {ArrayElement, ObjectValue} from './internal';
|
|
2
|
-
import type {Opaque, TagContainer} from './opaque';
|
|
3
2
|
import type {IsEqual} from './is-equal';
|
|
4
3
|
import type {KeysOfUnion} from './keys-of-union';
|
|
4
|
+
import type {JsonObject} from './basic';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
Create a type from `ParameterType` and `InputType` and change keys exclusive to `InputType` to `never`.
|
|
@@ -57,7 +57,6 @@ export type Exact<ParameterType, InputType> =
|
|
|
57
57
|
: ParameterType extends unknown[] ? Array<Exact<ArrayElement<ParameterType>, ArrayElement<InputType>>>
|
|
58
58
|
// In TypeScript, Array is a subtype of ReadonlyArray, so always test Array before ReadonlyArray.
|
|
59
59
|
: ParameterType extends readonly unknown[] ? ReadonlyArray<Exact<ArrayElement<ParameterType>, ArrayElement<InputType>>>
|
|
60
|
-
//
|
|
61
|
-
: ParameterType extends
|
|
62
|
-
: ParameterType
|
|
63
|
-
: ParameterType;
|
|
60
|
+
// Only apply Exact for pure object types. For types from a class, leave it unchanged to TypeScript to handle.
|
|
61
|
+
: ParameterType extends JsonObject ? ExactObject<ParameterType, InputType>
|
|
62
|
+
: ParameterType;
|
package/source/int-range.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ The numbers are created from the given `Start` (inclusive) parameter to the give
|
|
|
8
8
|
|
|
9
9
|
You skip over numbers using the `Step` parameter (defaults to `1`). For example, `IntRange<0, 10, 2>` will create a union of `0 | 2 | 4 | 6 | 8`.
|
|
10
10
|
|
|
11
|
-
Note: `Start` or `End` must smaller than `1000`.
|
|
11
|
+
Note: `Start` or `End` must be non-negative and smaller than `1000`.
|
|
12
12
|
|
|
13
13
|
Use-cases:
|
|
14
14
|
1. This can be used to define a set of valid input/output values. for example:
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
Get the element type of an `Iterable`/`AsyncIterable`. For example,
|
|
2
|
+
Get the element type of an `Iterable`/`AsyncIterable`. For example, `Array`, `Set`, `Map`, generator, stream, etc.
|
|
3
3
|
|
|
4
4
|
This can be useful, for example, if you want to get the type that is yielded in a generator function. Often the return type of those functions are not specified.
|
|
5
5
|
|
|
@@ -33,7 +33,7 @@ async function * iAmGeneratorAsync() {
|
|
|
33
33
|
type MeStringOrBoolean = IterableElement<ReturnType<typeof iAmGeneratorAsync>>
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
Many types in JavaScript/TypeScript are iterables. This type works on all types that implement those interfaces.
|
|
36
|
+
Many types in JavaScript/TypeScript are iterables. This type works on all types that implement those interfaces.
|
|
37
37
|
|
|
38
38
|
An example with an array of strings:
|
|
39
39
|
|
|
@@ -44,6 +44,16 @@ import type {IterableElement} from 'type-fest';
|
|
|
44
44
|
type MeString = IterableElement<string[]>
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
+
@example
|
|
48
|
+
```
|
|
49
|
+
import type {IterableElement} from 'type-fest';
|
|
50
|
+
|
|
51
|
+
const fruits = new Set(['🍎', '🍌', '🍉'] as const);
|
|
52
|
+
|
|
53
|
+
type Fruit = IterableElement<typeof fruits>;
|
|
54
|
+
//=> '🍎' | '🍌' | '🍉'
|
|
55
|
+
```
|
|
56
|
+
|
|
47
57
|
@category Iterable
|
|
48
58
|
*/
|
|
49
59
|
export type IterableElement<TargetIterable> =
|
package/source/jsonify.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {UnknownArray} from './unknown-array';
|
|
|
12
12
|
type NotJsonable = ((...arguments_: any[]) => any) | undefined | symbol;
|
|
13
13
|
|
|
14
14
|
type NeverToNull<T> = IsNever<T> extends true ? null : T;
|
|
15
|
+
type UndefinedToNull<T> = T extends undefined ? null : T;
|
|
15
16
|
|
|
16
17
|
// Handles tuples and arrays
|
|
17
18
|
type JsonifyList<T extends UnknownArray> = T extends readonly []
|
|
@@ -20,7 +21,7 @@ type JsonifyList<T extends UnknownArray> = T extends readonly []
|
|
|
20
21
|
? [NeverToNull<Jsonify<F>>, ...JsonifyList<R>]
|
|
21
22
|
: IsUnknown<T[number]> extends true
|
|
22
23
|
? []
|
|
23
|
-
: Array<T[number] extends NotJsonable ? null : Jsonify<T[number]
|
|
24
|
+
: Array<T[number] extends NotJsonable ? null : Jsonify<UndefinedToNull<T[number]>>>;
|
|
24
25
|
|
|
25
26
|
type FilterJsonableKeys<T extends object> = {
|
|
26
27
|
[Key in keyof T]: T[Key] extends NotJsonable ? never : Key;
|
package/source/paths.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ import type {EmptyObject} from './empty-object';
|
|
|
3
3
|
import type {IsAny} from './is-any';
|
|
4
4
|
import type {IsNever} from './is-never';
|
|
5
5
|
import type {UnknownArray} from './unknown-array';
|
|
6
|
+
import type {Sum} from './sum';
|
|
7
|
+
import type {LessThan} from './less-than';
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
10
|
Generate a union of all possible paths to properties in the given object.
|
|
@@ -45,7 +47,9 @@ open('listB.1'); // TypeError. Because listB only has one element.
|
|
|
45
47
|
@category Object
|
|
46
48
|
@category Array
|
|
47
49
|
*/
|
|
48
|
-
export type Paths<T> =
|
|
50
|
+
export type Paths<T> = Paths_<T>;
|
|
51
|
+
|
|
52
|
+
type Paths_<T, Depth extends number = 0> =
|
|
49
53
|
T extends NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
|
|
50
54
|
? never
|
|
51
55
|
: IsAny<T> extends true
|
|
@@ -53,14 +57,14 @@ export type Paths<T> =
|
|
|
53
57
|
: T extends UnknownArray
|
|
54
58
|
? number extends T['length']
|
|
55
59
|
// We need to handle the fixed and non-fixed index part of the array separately.
|
|
56
|
-
? InternalPaths<StaticPartOfArray<T
|
|
57
|
-
| InternalPaths<Array<VariablePartOfArray<T>[number]
|
|
58
|
-
: InternalPaths<T>
|
|
60
|
+
? InternalPaths<StaticPartOfArray<T>, Depth>
|
|
61
|
+
| InternalPaths<Array<VariablePartOfArray<T>[number]>, Depth>
|
|
62
|
+
: InternalPaths<T, Depth>
|
|
59
63
|
: T extends object
|
|
60
|
-
? InternalPaths<T>
|
|
64
|
+
? InternalPaths<T, Depth>
|
|
61
65
|
: never;
|
|
62
66
|
|
|
63
|
-
export type InternalPaths<_T, T = Required<_T>> =
|
|
67
|
+
export type InternalPaths<_T, Depth extends number = 0, T = Required<_T>> =
|
|
64
68
|
T extends EmptyObject | readonly []
|
|
65
69
|
? never
|
|
66
70
|
: {
|
|
@@ -71,8 +75,10 @@ export type InternalPaths<_T, T = Required<_T>> =
|
|
|
71
75
|
| Key
|
|
72
76
|
| ToString<Key>
|
|
73
77
|
| (
|
|
74
|
-
|
|
75
|
-
?
|
|
78
|
+
LessThan<Depth, 15> extends true // Limit the depth to prevent infinite recursion
|
|
79
|
+
? IsNever<Paths_<T[Key], Sum<Depth, 1>>> extends false
|
|
80
|
+
? `${Key}.${Paths_<T[Key], Sum<Depth, 1>>}`
|
|
81
|
+
: never
|
|
76
82
|
: never
|
|
77
83
|
)
|
|
78
84
|
: never
|
package/source/schema.d.ts
CHANGED
|
@@ -47,25 +47,25 @@ export type Schema<ObjectType, ValueType> = ObjectType extends string
|
|
|
47
47
|
? ValueType
|
|
48
48
|
: ObjectType extends ReadonlySet<unknown>
|
|
49
49
|
? ValueType
|
|
50
|
-
: ObjectType extends
|
|
51
|
-
? ValueType
|
|
52
|
-
: ObjectType extends unknown[]
|
|
50
|
+
: ObjectType extends Array<infer U>
|
|
51
|
+
? Array<Schema<U, ValueType>>
|
|
52
|
+
: ObjectType extends (...arguments_: unknown[]) => unknown
|
|
53
53
|
? ValueType
|
|
54
|
-
: ObjectType extends
|
|
54
|
+
: ObjectType extends Date
|
|
55
55
|
? ValueType
|
|
56
|
-
: ObjectType extends
|
|
56
|
+
: ObjectType extends Function
|
|
57
57
|
? ValueType
|
|
58
|
-
: ObjectType extends
|
|
58
|
+
: ObjectType extends RegExp
|
|
59
59
|
? ValueType
|
|
60
|
-
: ObjectType extends
|
|
61
|
-
? ValueType
|
|
62
|
-
:
|
|
63
|
-
? SchemaObject<ObjectType, ValueType>
|
|
64
|
-
: ValueType;
|
|
60
|
+
: ObjectType extends object
|
|
61
|
+
? SchemaObject<ObjectType, ValueType>
|
|
62
|
+
: ValueType;
|
|
65
63
|
|
|
66
64
|
/**
|
|
67
65
|
Same as `Schema`, but accepts only `object`s as inputs. Internal helper for `Schema`.
|
|
68
66
|
*/
|
|
69
67
|
type SchemaObject<ObjectType extends object, K> = {
|
|
70
|
-
[KeyType in keyof ObjectType]:
|
|
68
|
+
[KeyType in keyof ObjectType]: ObjectType[KeyType] extends readonly unknown[] | unknown[]
|
|
69
|
+
? Schema<ObjectType[KeyType], K>
|
|
70
|
+
: Schema<ObjectType[KeyType], K> | K;
|
|
71
71
|
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type {TypedArray} from './typed-array';
|
|
2
|
+
|
|
3
|
+
type StructuredCloneablePrimitive =
|
|
4
|
+
| string
|
|
5
|
+
| number
|
|
6
|
+
| bigint
|
|
7
|
+
| boolean
|
|
8
|
+
| null
|
|
9
|
+
| undefined
|
|
10
|
+
| Boolean
|
|
11
|
+
| Number
|
|
12
|
+
| String;
|
|
13
|
+
|
|
14
|
+
type StructuredCloneableData =
|
|
15
|
+
| ArrayBuffer
|
|
16
|
+
| DataView
|
|
17
|
+
| Date
|
|
18
|
+
| Error
|
|
19
|
+
| RegExp
|
|
20
|
+
| TypedArray
|
|
21
|
+
| Blob
|
|
22
|
+
| File;
|
|
23
|
+
// DOM exclusive types
|
|
24
|
+
// | AudioData
|
|
25
|
+
// | CropTarget
|
|
26
|
+
// | CryptoKey
|
|
27
|
+
// | DOMException
|
|
28
|
+
// | DOMMatrix
|
|
29
|
+
// | DOMMatrixReadOnly
|
|
30
|
+
// | DOMPoint
|
|
31
|
+
// | DOMPointReadOnly
|
|
32
|
+
// | DOMQuad
|
|
33
|
+
// | DOMRect
|
|
34
|
+
// | DOMRectReadOnly
|
|
35
|
+
// | FileList
|
|
36
|
+
// | FileSystemDirectoryHandle
|
|
37
|
+
// | FileSystemFileHandle
|
|
38
|
+
// | FileSystemHandle
|
|
39
|
+
// | GPUCompilationInfo
|
|
40
|
+
// | GPUCompilationMessage
|
|
41
|
+
// | ImageBitmap
|
|
42
|
+
// | ImageData
|
|
43
|
+
// | RTCCertificate
|
|
44
|
+
// | VideoFrame
|
|
45
|
+
|
|
46
|
+
type StructuredCloneableCollection =
|
|
47
|
+
| readonly StructuredCloneable[]
|
|
48
|
+
| {readonly [key: string]: StructuredCloneable; readonly [key: number]: StructuredCloneable}
|
|
49
|
+
| ReadonlyMap<StructuredCloneable, StructuredCloneable>
|
|
50
|
+
| ReadonlySet<StructuredCloneable>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
Matches a value that can be losslessly cloned using `structuredClone`.
|
|
54
|
+
|
|
55
|
+
Note:
|
|
56
|
+
- Custom error types will be cloned as the base `Error` type
|
|
57
|
+
- This type doesn't include types exclusive to the TypeScript DOM library (e.g. `DOMRect` and `VideoFrame`)
|
|
58
|
+
|
|
59
|
+
@see https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
|
|
60
|
+
|
|
61
|
+
@example
|
|
62
|
+
```
|
|
63
|
+
import type {StructuredCloneable} from 'type-fest';
|
|
64
|
+
|
|
65
|
+
class CustomClass {}
|
|
66
|
+
|
|
67
|
+
// @ts-expect-error
|
|
68
|
+
const error: StructuredCloneable = {
|
|
69
|
+
custom: new CustomClass(),
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
structuredClone(error);
|
|
73
|
+
//=> {custom: {}}
|
|
74
|
+
|
|
75
|
+
const good: StructuredCloneable = {
|
|
76
|
+
number: 3,
|
|
77
|
+
date: new Date(),
|
|
78
|
+
map: new Map<string, number>(),
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
good.map.set('key', 1);
|
|
82
|
+
|
|
83
|
+
structuredClone(good);
|
|
84
|
+
//=> {number: 3, date: Date(2022-10-17 22:22:35.920), map: Map {'key' -> 1}}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
@category Structured clone
|
|
88
|
+
*/
|
|
89
|
+
export type StructuredCloneable = StructuredCloneablePrimitive | StructuredCloneableData | StructuredCloneableCollection;
|