@depup/type-fest 5.4.4-depup.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/README.md +25 -0
- package/index.d.ts +211 -0
- package/license-cc0 +121 -0
- package/license-mit +9 -0
- package/package.json +85 -0
- package/readme.md +1052 -0
- package/source/all-extend.d.ts +120 -0
- package/source/all-union-fields.d.ts +91 -0
- package/source/and.d.ts +81 -0
- package/source/array-element.d.ts +46 -0
- package/source/array-indices.d.ts +25 -0
- package/source/array-reverse.d.ts +84 -0
- package/source/array-slice.d.ts +132 -0
- package/source/array-splice.d.ts +104 -0
- package/source/array-tail.d.ts +70 -0
- package/source/array-values.d.ts +24 -0
- package/source/arrayable.d.ts +31 -0
- package/source/async-return-type.d.ts +28 -0
- package/source/asyncify.d.ts +25 -0
- package/source/basic.d.ts +38 -0
- package/source/camel-case.d.ts +92 -0
- package/source/camel-cased-properties-deep.d.ts +99 -0
- package/source/camel-cased-properties.d.ts +45 -0
- package/source/characters.d.ts +67 -0
- package/source/conditional-except.d.ts +47 -0
- package/source/conditional-keys.d.ts +63 -0
- package/source/conditional-pick-deep.d.ts +120 -0
- package/source/conditional-pick.d.ts +46 -0
- package/source/conditional-simplify-deep.d.ts +73 -0
- package/source/conditional-simplify.d.ts +50 -0
- package/source/delimiter-case.d.ts +77 -0
- package/source/delimiter-cased-properties-deep.d.ts +108 -0
- package/source/delimiter-cased-properties.d.ts +48 -0
- package/source/distributed-omit.d.ts +96 -0
- package/source/distributed-pick.d.ts +92 -0
- package/source/empty-object.d.ts +51 -0
- package/source/entries.d.ts +64 -0
- package/source/entry.d.ts +67 -0
- package/source/exact.d.ts +73 -0
- package/source/except.d.ts +112 -0
- package/source/exclude-rest-element.d.ts +40 -0
- package/source/exclude-strict.d.ts +51 -0
- package/source/exclusify-union.d.ts +147 -0
- package/source/extends-strict.d.ts +44 -0
- package/source/extract-rest-element.d.ts +30 -0
- package/source/extract-strict.d.ts +51 -0
- package/source/find-global-type.d.ts +68 -0
- package/source/fixed-length-array.d.ts +97 -0
- package/source/get.d.ts +227 -0
- package/source/global-this.d.ts +24 -0
- package/source/globals/index.d.ts +3 -0
- package/source/globals/observable-like.d.ts +78 -0
- package/source/greater-than-or-equal.d.ts +30 -0
- package/source/greater-than.d.ts +58 -0
- package/source/has-optional-keys.d.ts +23 -0
- package/source/has-readonly-keys.d.ts +23 -0
- package/source/has-required-keys.d.ts +61 -0
- package/source/has-writable-keys.d.ts +23 -0
- package/source/if-any.d.ts +28 -0
- package/source/if-empty-object.d.ts +30 -0
- package/source/if-never.d.ts +28 -0
- package/source/if-null.d.ts +28 -0
- package/source/if-unknown.d.ts +28 -0
- package/source/if.d.ts +102 -0
- package/source/includes.d.ts +24 -0
- package/source/int-closed-range.d.ts +47 -0
- package/source/int-range.d.ts +69 -0
- package/source/internal/array.d.ts +151 -0
- package/source/internal/characters.d.ts +65 -0
- package/source/internal/enforce-optional.d.ts +49 -0
- package/source/internal/index.d.ts +11 -0
- package/source/internal/keys.d.ts +100 -0
- package/source/internal/numeric.d.ts +151 -0
- package/source/internal/object.d.ts +292 -0
- package/source/internal/string.d.ts +202 -0
- package/source/internal/tuple.d.ts +79 -0
- package/source/internal/type.d.ts +164 -0
- package/source/invariant-of.d.ts +85 -0
- package/source/is-any.d.ts +31 -0
- package/source/is-equal.d.ts +42 -0
- package/source/is-float.d.ts +43 -0
- package/source/is-integer.d.ts +60 -0
- package/source/is-literal.d.ts +315 -0
- package/source/is-lowercase.d.ts +38 -0
- package/source/is-never.d.ts +56 -0
- package/source/is-null.d.ts +22 -0
- package/source/is-nullable.d.ts +30 -0
- package/source/is-optional-key-of.d.ts +51 -0
- package/source/is-optional.d.ts +28 -0
- package/source/is-readonly-key-of.d.ts +55 -0
- package/source/is-required-key-of.d.ts +51 -0
- package/source/is-tuple.d.ts +92 -0
- package/source/is-undefined.d.ts +22 -0
- package/source/is-union.d.ts +40 -0
- package/source/is-unknown.d.ts +43 -0
- package/source/is-uppercase.d.ts +38 -0
- package/source/is-writable-key-of.d.ts +51 -0
- package/source/iterable-element.d.ts +66 -0
- package/source/join.d.ts +79 -0
- package/source/json-value.d.ts +33 -0
- package/source/jsonifiable.d.ts +37 -0
- package/source/jsonify.d.ts +127 -0
- package/source/kebab-case.d.ts +46 -0
- package/source/kebab-cased-properties-deep.d.ts +65 -0
- package/source/kebab-cased-properties.d.ts +42 -0
- package/source/key-as-string.d.ts +27 -0
- package/source/keys-of-union.d.ts +44 -0
- package/source/last-array-element.d.ts +36 -0
- package/source/less-than-or-equal.d.ts +24 -0
- package/source/less-than.d.ts +28 -0
- package/source/literal-to-primitive-deep.d.ts +71 -0
- package/source/literal-to-primitive.d.ts +38 -0
- package/source/literal-union.d.ts +39 -0
- package/source/merge-deep.d.ts +495 -0
- package/source/merge-exclusive.d.ts +45 -0
- package/source/merge.d.ts +62 -0
- package/source/multidimensional-array.d.ts +38 -0
- package/source/multidimensional-readonly-array.d.ts +38 -0
- package/source/non-empty-object.d.ts +38 -0
- package/source/non-empty-string.d.ts +32 -0
- package/source/non-empty-tuple.d.ts +24 -0
- package/source/numeric.d.ts +226 -0
- package/source/object-merge.d.ts +194 -0
- package/source/omit-deep.d.ts +154 -0
- package/source/omit-index-signature.d.ts +98 -0
- package/source/opaque.d.ts +3 -0
- package/source/optional-keys-of.d.ts +46 -0
- package/source/or.d.ts +89 -0
- package/source/override-properties.d.ts +43 -0
- package/source/package-json.d.ts +710 -0
- package/source/partial-deep.d.ts +155 -0
- package/source/partial-on-undefined-deep.d.ts +81 -0
- package/source/pascal-case.d.ts +51 -0
- package/source/pascal-cased-properties-deep.d.ts +72 -0
- package/source/pascal-cased-properties.d.ts +42 -0
- package/source/paths.d.ts +241 -0
- package/source/pick-deep.d.ts +152 -0
- package/source/pick-index-signature.d.ts +52 -0
- package/source/primitive.d.ts +15 -0
- package/source/promisable.d.ts +27 -0
- package/source/readonly-deep.d.ts +116 -0
- package/source/readonly-keys-of.d.ts +38 -0
- package/source/readonly-tuple.d.ts +34 -0
- package/source/remove-prefix.d.ts +132 -0
- package/source/replace.d.ts +87 -0
- package/source/require-all-or-none.d.ts +54 -0
- package/source/require-at-least-one.d.ts +50 -0
- package/source/require-exactly-one.d.ts +48 -0
- package/source/require-one-or-none.d.ts +49 -0
- package/source/required-deep.d.ts +76 -0
- package/source/required-keys-of.d.ts +38 -0
- package/source/schema.d.ts +119 -0
- package/source/screaming-snake-case.d.ts +30 -0
- package/source/set-field-type.d.ts +67 -0
- package/source/set-non-nullable-deep.d.ts +85 -0
- package/source/set-non-nullable.d.ts +41 -0
- package/source/set-optional.d.ts +46 -0
- package/source/set-parameter-type.d.ts +125 -0
- package/source/set-readonly.d.ts +44 -0
- package/source/set-required-deep.d.ts +64 -0
- package/source/set-required.d.ts +79 -0
- package/source/set-return-type.d.ts +31 -0
- package/source/shared-union-fields-deep.d.ts +180 -0
- package/source/shared-union-fields.d.ts +79 -0
- package/source/simplify-deep.d.ts +117 -0
- package/source/simplify.d.ts +61 -0
- package/source/single-key-object.d.ts +28 -0
- package/source/snake-case.d.ts +47 -0
- package/source/snake-cased-properties-deep.d.ts +65 -0
- package/source/snake-cased-properties.d.ts +42 -0
- package/source/split-on-rest-element.d.ts +106 -0
- package/source/split.d.ts +90 -0
- package/source/spread.d.ts +82 -0
- package/source/string-repeat.d.ts +49 -0
- package/source/string-slice.d.ts +39 -0
- package/source/stringified.d.ts +25 -0
- package/source/structured-cloneable.d.ts +89 -0
- package/source/subtract.d.ts +86 -0
- package/source/sum.d.ts +81 -0
- package/source/tagged-union.d.ts +53 -0
- package/source/tagged.d.ts +263 -0
- package/source/trim.d.ts +29 -0
- package/source/tsconfig-json.d.ts +1324 -0
- package/source/tuple-of.d.ts +80 -0
- package/source/tuple-to-object.d.ts +47 -0
- package/source/tuple-to-union.d.ts +54 -0
- package/source/typed-array.d.ts +19 -0
- package/source/undefined-on-partial-deep.d.ts +83 -0
- package/source/union-to-intersection.d.ts +35 -0
- package/source/union-to-tuple.d.ts +58 -0
- package/source/unknown-array.d.ts +27 -0
- package/source/unknown-map.d.ts +26 -0
- package/source/unknown-record.d.ts +33 -0
- package/source/unknown-set.d.ts +26 -0
- package/source/unwrap-partial.d.ts +33 -0
- package/source/value-of.d.ts +24 -0
- package/source/words.d.ts +122 -0
- package/source/writable-deep.d.ts +84 -0
- package/source/writable-keys-of.d.ts +34 -0
- package/source/writable.d.ts +72 -0
- package/source/xor.d.ts +83 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type {If} from './if.d.ts';
|
|
2
|
+
import type {CollapseRestElement} from './internal/array.d.ts';
|
|
3
|
+
import type {ApplyDefaultOptions} from './internal/object.d.ts';
|
|
4
|
+
import type {IfNotAnyOrNever, Not} from './internal/type.d.ts';
|
|
5
|
+
import type {IsAny} from './is-any.d.ts';
|
|
6
|
+
import type {IsNever} from './is-never.d.ts';
|
|
7
|
+
import type {Or} from './or.d.ts';
|
|
8
|
+
import type {UnknownArray} from './unknown-array.d.ts';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
@see {@link AllExtend}
|
|
12
|
+
*/
|
|
13
|
+
export type AllExtendOptions = {
|
|
14
|
+
/**
|
|
15
|
+
Consider `never` elements to match the target type only if the target type itself is `never` (or `any`).
|
|
16
|
+
|
|
17
|
+
- When set to `true` (default), `never` is _not_ treated as a bottom type, instead, it is treated as a type that matches only itself (or `any`).
|
|
18
|
+
- When set to `false`, `never` is treated as a bottom type, and behaves as it normally would.
|
|
19
|
+
|
|
20
|
+
@default true
|
|
21
|
+
|
|
22
|
+
@example
|
|
23
|
+
```
|
|
24
|
+
import type {AllExtend} from 'type-fest';
|
|
25
|
+
|
|
26
|
+
type A = AllExtend<[1, 2, never], number, {strictNever: true}>;
|
|
27
|
+
//=> false
|
|
28
|
+
|
|
29
|
+
type B = AllExtend<[1, 2, never], number, {strictNever: false}>;
|
|
30
|
+
//=> true
|
|
31
|
+
|
|
32
|
+
type C = AllExtend<[never, never], never, {strictNever: true}>;
|
|
33
|
+
//=> true
|
|
34
|
+
|
|
35
|
+
type D = AllExtend<[never, never], never, {strictNever: false}>;
|
|
36
|
+
//=> true
|
|
37
|
+
|
|
38
|
+
type E = AllExtend<['a', 'b', never], any, {strictNever: true}>;
|
|
39
|
+
//=> true
|
|
40
|
+
|
|
41
|
+
type F = AllExtend<['a', 'b', never], any, {strictNever: false}>;
|
|
42
|
+
//=> true
|
|
43
|
+
|
|
44
|
+
type G = AllExtend<[never, 1], never, {strictNever: true}>;
|
|
45
|
+
//=> false
|
|
46
|
+
|
|
47
|
+
type H = AllExtend<[never, 1], never, {strictNever: false}>;
|
|
48
|
+
//=> false
|
|
49
|
+
```
|
|
50
|
+
*/
|
|
51
|
+
strictNever?: boolean;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
type DefaultAllExtendOptions = {
|
|
55
|
+
strictNever: true;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
Returns a boolean for whether every element in an array type extends another type.
|
|
60
|
+
|
|
61
|
+
@example
|
|
62
|
+
```
|
|
63
|
+
import type {AllExtend} from 'type-fest';
|
|
64
|
+
|
|
65
|
+
type A = AllExtend<[1, 2, 3], number>;
|
|
66
|
+
//=> true
|
|
67
|
+
|
|
68
|
+
type B = AllExtend<[1, 2, '3'], number>;
|
|
69
|
+
//=> false
|
|
70
|
+
|
|
71
|
+
type C = AllExtend<[number, number | string], number>;
|
|
72
|
+
//=> boolean
|
|
73
|
+
|
|
74
|
+
type D = AllExtend<[true, boolean, true], true>;
|
|
75
|
+
//=> boolean
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Note: Behaviour of optional elements depend on the `exactOptionalPropertyTypes` compiler option. When the option is disabled, the target type must include `undefined` for a successful match.
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
// @exactOptionalPropertyTypes: true
|
|
82
|
+
import type {AllExtend} from 'type-fest';
|
|
83
|
+
|
|
84
|
+
type A = AllExtend<[1?, 2?, 3?], number>;
|
|
85
|
+
//=> true
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
```
|
|
89
|
+
// @exactOptionalPropertyTypes: false
|
|
90
|
+
import type {AllExtend} from 'type-fest';
|
|
91
|
+
|
|
92
|
+
type A = AllExtend<[1?, 2?, 3?], number>;
|
|
93
|
+
//=> boolean
|
|
94
|
+
|
|
95
|
+
type B = AllExtend<[1?, 2?, 3?], number | undefined>;
|
|
96
|
+
//=> true
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
@see {@link AllExtendOptions}
|
|
100
|
+
|
|
101
|
+
@category Utilities
|
|
102
|
+
@category Array
|
|
103
|
+
*/
|
|
104
|
+
export type AllExtend<TArray extends UnknownArray, Type, Options extends AllExtendOptions = {}> =
|
|
105
|
+
_AllExtend<CollapseRestElement<TArray>, Type, ApplyDefaultOptions<AllExtendOptions, DefaultAllExtendOptions, Options>>;
|
|
106
|
+
|
|
107
|
+
type _AllExtend<TArray extends UnknownArray, Type, Options extends Required<AllExtendOptions>> = IfNotAnyOrNever<TArray, If<IsAny<Type>, true,
|
|
108
|
+
TArray extends readonly [infer First, ...infer Rest]
|
|
109
|
+
? IsNever<First> extends true
|
|
110
|
+
? Or<IsNever<Type>, Not<Options['strictNever']>> extends true
|
|
111
|
+
// If target `Type` is also `never` OR `strictNever` is disabled, recurse further.
|
|
112
|
+
? _AllExtend<Rest, Type, Options>
|
|
113
|
+
: false
|
|
114
|
+
: First extends Type
|
|
115
|
+
? _AllExtend<Rest, Type, Options>
|
|
116
|
+
: false
|
|
117
|
+
: true
|
|
118
|
+
>, false, false>;
|
|
119
|
+
|
|
120
|
+
export {};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type {NonRecursiveType, ReadonlyKeysOfUnion, ValueOfUnion} from './internal/index.d.ts';
|
|
2
|
+
import type {KeysOfUnion} from './keys-of-union.d.ts';
|
|
3
|
+
import type {SharedUnionFields} from './shared-union-fields.d.ts';
|
|
4
|
+
import type {Simplify} from './simplify.d.ts';
|
|
5
|
+
import type {UnknownArray} from './unknown-array.d.ts';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
Create a type with all fields from a union of object types.
|
|
9
|
+
|
|
10
|
+
Use-cases:
|
|
11
|
+
- You want a safe object type where each key exists in the union object.
|
|
12
|
+
|
|
13
|
+
@example
|
|
14
|
+
```
|
|
15
|
+
import type {AllUnionFields} from 'type-fest';
|
|
16
|
+
|
|
17
|
+
type Cat = {
|
|
18
|
+
name: string;
|
|
19
|
+
type: 'cat';
|
|
20
|
+
catType: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type Dog = {
|
|
24
|
+
name: string;
|
|
25
|
+
type: 'dog';
|
|
26
|
+
dogType: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function displayPetInfo(petInfo: Cat | Dog) {
|
|
30
|
+
// typeof petInfo =>
|
|
31
|
+
// {
|
|
32
|
+
// name: string;
|
|
33
|
+
// type: 'cat';
|
|
34
|
+
// catType: string;
|
|
35
|
+
// } | {
|
|
36
|
+
// name: string;
|
|
37
|
+
// type: 'dog';
|
|
38
|
+
// dogType: string;
|
|
39
|
+
// }
|
|
40
|
+
|
|
41
|
+
console.log('name:', petInfo.name);
|
|
42
|
+
console.log('type:', petInfo.type);
|
|
43
|
+
|
|
44
|
+
// TypeScript complains about `catType` and `dogType` not existing on type `Cat | Dog`.
|
|
45
|
+
// @ts-expect-error
|
|
46
|
+
console.log('animal type:', petInfo.catType ?? petInfo.dogType);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function displayPetInfoWithAllUnionFields(petInfo: AllUnionFields<Cat | Dog>) {
|
|
50
|
+
// typeof petInfo =>
|
|
51
|
+
// {
|
|
52
|
+
// name: string;
|
|
53
|
+
// type: 'cat' | 'dog';
|
|
54
|
+
// catType?: string;
|
|
55
|
+
// dogType?: string;
|
|
56
|
+
// }
|
|
57
|
+
|
|
58
|
+
console.log('name:', petInfo.name);
|
|
59
|
+
console.log('type:', petInfo.type);
|
|
60
|
+
|
|
61
|
+
// No TypeScript error.
|
|
62
|
+
console.log('animal type:', petInfo.catType ?? petInfo.dogType);
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
@see {@link SharedUnionFields}
|
|
67
|
+
|
|
68
|
+
@category Object
|
|
69
|
+
@category Union
|
|
70
|
+
*/
|
|
71
|
+
export type AllUnionFields<Union> =
|
|
72
|
+
Extract<Union, NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | UnknownArray> extends infer SkippedMembers
|
|
73
|
+
? Exclude<Union, SkippedMembers> extends infer RelevantMembers
|
|
74
|
+
?
|
|
75
|
+
| SkippedMembers
|
|
76
|
+
| Simplify<
|
|
77
|
+
// Include fields that are common in all union members
|
|
78
|
+
SharedUnionFields<RelevantMembers> &
|
|
79
|
+
// Include readonly fields present in any union member
|
|
80
|
+
{
|
|
81
|
+
readonly [P in ReadonlyKeysOfUnion<RelevantMembers>]?: ValueOfUnion<RelevantMembers, P & KeysOfUnion<RelevantMembers>>
|
|
82
|
+
} &
|
|
83
|
+
// Include remaining fields that are neither common nor readonly
|
|
84
|
+
{
|
|
85
|
+
[P in Exclude<KeysOfUnion<RelevantMembers>, ReadonlyKeysOfUnion<RelevantMembers> | keyof RelevantMembers>]?: ValueOfUnion<RelevantMembers, P>
|
|
86
|
+
}
|
|
87
|
+
>
|
|
88
|
+
: never
|
|
89
|
+
: never;
|
|
90
|
+
|
|
91
|
+
export {};
|
package/source/and.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type {AllExtend} from './all-extend.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Returns a boolean for whether two given types are both true.
|
|
5
|
+
|
|
6
|
+
Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import type {And} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
type TT = And<true, true>;
|
|
13
|
+
//=> true
|
|
14
|
+
|
|
15
|
+
type TF = And<true, false>;
|
|
16
|
+
//=> false
|
|
17
|
+
|
|
18
|
+
type FT = And<false, true>;
|
|
19
|
+
//=> false
|
|
20
|
+
|
|
21
|
+
type FF = And<false, false>;
|
|
22
|
+
//=> false
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Note: When `boolean` is passed as an argument, it is distributed into separate cases, and the final result is a union of those cases.
|
|
26
|
+
For example, `And<true, boolean>` expands to `And<true, true> | And<true, false>`, which simplifies to `true | false` (i.e., `boolean`).
|
|
27
|
+
|
|
28
|
+
@example
|
|
29
|
+
```
|
|
30
|
+
import type {And} from 'type-fest';
|
|
31
|
+
|
|
32
|
+
type A = And<true, boolean>;
|
|
33
|
+
//=> boolean
|
|
34
|
+
|
|
35
|
+
type B = And<boolean, true>;
|
|
36
|
+
//=> boolean
|
|
37
|
+
|
|
38
|
+
type C = And<false, boolean>;
|
|
39
|
+
//=> false
|
|
40
|
+
|
|
41
|
+
type D = And<boolean, false>;
|
|
42
|
+
//=> false
|
|
43
|
+
|
|
44
|
+
type E = And<boolean, boolean>;
|
|
45
|
+
//=> boolean
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Note: If either of the types is `never`, the result becomes `false`.
|
|
49
|
+
|
|
50
|
+
@example
|
|
51
|
+
```
|
|
52
|
+
import type {And} from 'type-fest';
|
|
53
|
+
|
|
54
|
+
type A = And<true, never>;
|
|
55
|
+
//=> false
|
|
56
|
+
|
|
57
|
+
type B = And<never, true>;
|
|
58
|
+
//=> false
|
|
59
|
+
|
|
60
|
+
type C = And<false, never>;
|
|
61
|
+
//=> false
|
|
62
|
+
|
|
63
|
+
type D = And<never, false>;
|
|
64
|
+
//=> false
|
|
65
|
+
|
|
66
|
+
type E = And<boolean, never>;
|
|
67
|
+
//=> false
|
|
68
|
+
|
|
69
|
+
type F = And<never, boolean>;
|
|
70
|
+
//=> false
|
|
71
|
+
|
|
72
|
+
type G = And<never, never>;
|
|
73
|
+
//=> false
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
@see {@link Or}
|
|
77
|
+
@see {@link Xor}
|
|
78
|
+
*/
|
|
79
|
+
export type And<A extends boolean, B extends boolean> = AllExtend<[A, B], true>;
|
|
80
|
+
|
|
81
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type {UnknownArray} from './unknown-array.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Extracts the element type of an array or tuple.
|
|
5
|
+
|
|
6
|
+
Use-cases:
|
|
7
|
+
- When you need type-safe element extraction that returns `never` for non-arrays.
|
|
8
|
+
- When extracting element types from generic array parameters in function signatures.
|
|
9
|
+
- For better readability and explicit intent over using `T[number]` directly.
|
|
10
|
+
|
|
11
|
+
Note: Returns `never` if the type is not an array.
|
|
12
|
+
|
|
13
|
+
@example
|
|
14
|
+
```
|
|
15
|
+
import type {ArrayElement} from 'type-fest';
|
|
16
|
+
|
|
17
|
+
// Arrays
|
|
18
|
+
type StringArray = ArrayElement<string[]>;
|
|
19
|
+
//=> string
|
|
20
|
+
|
|
21
|
+
// Tuples
|
|
22
|
+
type Tuple = ArrayElement<[1, 2, 3]>;
|
|
23
|
+
//=> 1 | 2 | 3
|
|
24
|
+
|
|
25
|
+
// Type-safe
|
|
26
|
+
type NotArray = ArrayElement<{a: string}>;
|
|
27
|
+
//=> never
|
|
28
|
+
|
|
29
|
+
// Practical example
|
|
30
|
+
declare function getRandomElement<T extends readonly unknown[]>(array: T): ArrayElement<T>;
|
|
31
|
+
|
|
32
|
+
getRandomElement(['foo', 'bar', 'baz'] as const);
|
|
33
|
+
//=> 'foo' | 'bar' | 'baz'
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
@see {@link ArrayValues} - For directly extracting values from a constant array type.
|
|
37
|
+
@see {@link IterableElement} - For iterables like `Set`, `Map`, and generators (not suitable for all use cases due to different inference behavior).
|
|
38
|
+
|
|
39
|
+
@category Array
|
|
40
|
+
*/
|
|
41
|
+
export type ArrayElement<T> =
|
|
42
|
+
T extends UnknownArray
|
|
43
|
+
? T[number]
|
|
44
|
+
: never;
|
|
45
|
+
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Provides valid indices for a constant array or tuple.
|
|
3
|
+
|
|
4
|
+
Use-case: This type is useful when working with constant arrays or tuples and you want to enforce type-safety for accessing elements by their indices.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {ArrayIndices, ArrayValues} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
const weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] as const;
|
|
11
|
+
|
|
12
|
+
type Weekday = ArrayIndices<typeof weekdays>;
|
|
13
|
+
type WeekdayName = ArrayValues<typeof weekdays>;
|
|
14
|
+
|
|
15
|
+
const getWeekdayName = (day: Weekday): WeekdayName => weekdays[day];
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
@see {@link ArrayValues}
|
|
19
|
+
|
|
20
|
+
@category Array
|
|
21
|
+
*/
|
|
22
|
+
export type ArrayIndices<Element extends readonly unknown[]> =
|
|
23
|
+
Exclude<Partial<Element>['length'], Element['length']>;
|
|
24
|
+
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type {If} from './if.d.ts';
|
|
2
|
+
import type {IsArrayReadonly} from './internal/array.d.ts';
|
|
3
|
+
import type {IfNotAnyOrNever, IsExactOptionalPropertyTypesEnabled} from './internal/type.d.ts';
|
|
4
|
+
import type {IsOptionalKeyOf} from './is-optional-key-of.d.ts';
|
|
5
|
+
import type {UnknownArray} from './unknown-array.d.ts';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
Reverse the order of elements in a tuple type.
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```ts
|
|
12
|
+
import type {ArrayReverse} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type A = ArrayReverse<[string, number, boolean]>;
|
|
15
|
+
//=> [boolean, number, string]
|
|
16
|
+
|
|
17
|
+
type B = ArrayReverse<readonly [string, number, ...boolean[]]>;
|
|
18
|
+
//=> readonly [...boolean[], number, string]
|
|
19
|
+
|
|
20
|
+
type C = ArrayReverse<['foo', 'bar'] | readonly [1, 2, 3]>;
|
|
21
|
+
//=> ['bar', 'foo'] | readonly [3, 2, 1]
|
|
22
|
+
|
|
23
|
+
type D = ArrayReverse<string[]>;
|
|
24
|
+
//=> string[]
|
|
25
|
+
|
|
26
|
+
type E = ArrayReverse<[]>;
|
|
27
|
+
//=> []
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Note: If the tuple contains optional elements, the result will be a union of tuples, refer to the examples below:
|
|
31
|
+
|
|
32
|
+
@example
|
|
33
|
+
```ts
|
|
34
|
+
import type {ArrayReverse} from 'type-fest';
|
|
35
|
+
|
|
36
|
+
type A = ArrayReverse<[string, number, boolean?]>;
|
|
37
|
+
//=> [number, string] | [boolean, number, string]
|
|
38
|
+
|
|
39
|
+
type B = ArrayReverse<[string, number?, boolean?]>;
|
|
40
|
+
//=> [string] | [number, string] | [boolean, number, string]
|
|
41
|
+
|
|
42
|
+
type C = ArrayReverse<[string?, number?, boolean?]>;
|
|
43
|
+
//=> [] | [string] | [number, string] | [boolean, number, string]
|
|
44
|
+
|
|
45
|
+
type D = ArrayReverse<[string, number?, ...boolean[]]>;
|
|
46
|
+
//=> [string] | [...boolean[], number, string]
|
|
47
|
+
|
|
48
|
+
type E = ArrayReverse<[string?, number?, ...boolean[]]>;
|
|
49
|
+
//=> [] | [string] | [...boolean[], number, string]
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
@category Array
|
|
53
|
+
*/
|
|
54
|
+
export type ArrayReverse<TArray extends UnknownArray> = IfNotAnyOrNever<TArray,
|
|
55
|
+
TArray extends unknown // For distributing `TArray`
|
|
56
|
+
? _ArrayReverse<TArray> extends infer Result
|
|
57
|
+
? If<IsArrayReadonly<TArray>, Readonly<Result>, Result>
|
|
58
|
+
: never // Should never happen
|
|
59
|
+
: never>; // Should never happen
|
|
60
|
+
|
|
61
|
+
type _ArrayReverse<
|
|
62
|
+
TArray extends UnknownArray,
|
|
63
|
+
BeforeRestAcc extends UnknownArray = [],
|
|
64
|
+
AfterRestAcc extends UnknownArray = [],
|
|
65
|
+
Result extends UnknownArray = never,
|
|
66
|
+
> =
|
|
67
|
+
keyof TArray & `${number}` extends never
|
|
68
|
+
// Enters this branch, if `TArray` is empty (e.g., `[]`),
|
|
69
|
+
// or `TArray` contains no non-rest elements preceding the rest element (e.g., `[...string[]]` or `[...string[], string]`).
|
|
70
|
+
? TArray extends readonly [...infer Rest, infer Last]
|
|
71
|
+
? _ArrayReverse<Rest, BeforeRestAcc, [...AfterRestAcc, Last], Result> // Accumulate elements that are present after the rest element in reverse order.
|
|
72
|
+
: Result | [...AfterRestAcc, ...TArray, ...BeforeRestAcc] // Add the rest element between the accumulated elements.
|
|
73
|
+
: TArray extends readonly [(infer First)?, ...infer Rest]
|
|
74
|
+
? IsOptionalKeyOf<TArray, '0'> extends true
|
|
75
|
+
? _ArrayReverse<
|
|
76
|
+
Rest,
|
|
77
|
+
[First | (If<IsExactOptionalPropertyTypesEnabled, never, undefined>), ...BeforeRestAcc], // Add `| undefined` for optional elements, if `exactOptionalPropertyTypes` is disabled.
|
|
78
|
+
AfterRestAcc,
|
|
79
|
+
Result | BeforeRestAcc
|
|
80
|
+
>
|
|
81
|
+
: _ArrayReverse<Rest, [First, ...BeforeRestAcc], AfterRestAcc, Result>
|
|
82
|
+
: never; // Should never happen, since `readonly [(infer First)?, ...infer Rest]` is a top-type for arrays.
|
|
83
|
+
|
|
84
|
+
export {};
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type {Sum} from './sum.d.ts';
|
|
2
|
+
import type {LessThanOrEqual} from './less-than-or-equal.d.ts';
|
|
3
|
+
import type {GreaterThanOrEqual} from './greater-than-or-equal.d.ts';
|
|
4
|
+
import type {GreaterThan} from './greater-than.d.ts';
|
|
5
|
+
import type {IsNegative} from './numeric.d.ts';
|
|
6
|
+
import type {Not, TupleMin} from './internal/index.d.ts';
|
|
7
|
+
import type {IsEqual} from './is-equal.d.ts';
|
|
8
|
+
import type {And} from './and.d.ts';
|
|
9
|
+
import type {ArraySplice} from './array-splice.d.ts';
|
|
10
|
+
import type {IsNever} from './is-never.d.ts';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
Returns an array slice of a given range, just like `Array#slice()`.
|
|
14
|
+
|
|
15
|
+
@example
|
|
16
|
+
```
|
|
17
|
+
import type {ArraySlice} from 'type-fest';
|
|
18
|
+
|
|
19
|
+
type T0 = ArraySlice<[0, 1, 2, 3, 4]>;
|
|
20
|
+
//=> [0, 1, 2, 3, 4]
|
|
21
|
+
|
|
22
|
+
type T1 = ArraySlice<[0, 1, 2, 3, 4], 0, -1>;
|
|
23
|
+
//=> [0, 1, 2, 3]
|
|
24
|
+
|
|
25
|
+
type T2 = ArraySlice<[0, 1, 2, 3, 4], 1, -2>;
|
|
26
|
+
//=> [1, 2]
|
|
27
|
+
|
|
28
|
+
type T3 = ArraySlice<[0, 1, 2, 3, 4], -2, 4>;
|
|
29
|
+
//=> [3]
|
|
30
|
+
|
|
31
|
+
type T4 = ArraySlice<[0, 1, 2, 3, 4], -2, -1>;
|
|
32
|
+
//=> [3]
|
|
33
|
+
|
|
34
|
+
type T5 = ArraySlice<[0, 1, 2, 3, 4], 0, -999>;
|
|
35
|
+
//=> []
|
|
36
|
+
|
|
37
|
+
function arraySlice<
|
|
38
|
+
const Array_ extends readonly unknown[],
|
|
39
|
+
Start extends number = 0,
|
|
40
|
+
End extends number = Array_['length'],
|
|
41
|
+
>(array: Array_, start?: Start, end?: End) {
|
|
42
|
+
return array.slice(start, end) as ArraySlice<Array_, Start, End>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const slice = arraySlice([1, '2', {a: 3}, [4, 5]], 0, -1);
|
|
46
|
+
|
|
47
|
+
type Slice = typeof slice;
|
|
48
|
+
//=> [1, '2', {readonly a: 3}]
|
|
49
|
+
|
|
50
|
+
const value = slice[2].a;
|
|
51
|
+
//=> 3
|
|
52
|
+
|
|
53
|
+
// @ts-expect-error -- TS2493: Tuple type '[1, "2", {readonly a: 3}]' of length '3' has no element at index '3'.
|
|
54
|
+
const invalidIndexAccess = slice[3];
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
@category Array
|
|
58
|
+
*/
|
|
59
|
+
export type ArraySlice<
|
|
60
|
+
Array_ extends readonly unknown[],
|
|
61
|
+
Start extends number = never,
|
|
62
|
+
End extends number = never,
|
|
63
|
+
> = Array_ extends unknown // To distributive type
|
|
64
|
+
? IsNever<Start> extends true
|
|
65
|
+
? IsNever<End> extends true
|
|
66
|
+
? _ArraySlice<Array_, Start, End>
|
|
67
|
+
: End extends unknown // To distribute `End`
|
|
68
|
+
? _ArraySlice<Array_, Start, End>
|
|
69
|
+
: never // Never happens
|
|
70
|
+
: IsNever<End> extends true
|
|
71
|
+
? Start extends unknown // To distribute `Start`
|
|
72
|
+
? _ArraySlice<Array_, Start, End>
|
|
73
|
+
: never // Never happens
|
|
74
|
+
: Start extends unknown // To distribute `Start`
|
|
75
|
+
? End extends unknown // To distribute `End`
|
|
76
|
+
? _ArraySlice<Array_, Start, End>
|
|
77
|
+
: never // Never happens
|
|
78
|
+
: never // Never happens
|
|
79
|
+
: never; // Never happens
|
|
80
|
+
|
|
81
|
+
type _ArraySlice<
|
|
82
|
+
Array_ extends readonly unknown[],
|
|
83
|
+
Start extends number = 0,
|
|
84
|
+
End extends number = Array_['length'],
|
|
85
|
+
> = And<IsEqual<Start, never>, IsEqual<End, never>> extends true
|
|
86
|
+
? Array_
|
|
87
|
+
: number extends Array_['length']
|
|
88
|
+
? VariableLengthArraySliceHelper<Array_, Start, End>
|
|
89
|
+
: ArraySliceHelper<Array_, IsEqual<Start, never> extends true ? 0 : Start, IsEqual<End, never> extends true ? Array_['length'] : End>;
|
|
90
|
+
|
|
91
|
+
type VariableLengthArraySliceHelper<
|
|
92
|
+
Array_ extends readonly unknown[],
|
|
93
|
+
Start extends number,
|
|
94
|
+
End extends number,
|
|
95
|
+
> = And<Not<IsNegative<Start>>, IsEqual<End, never>> extends true
|
|
96
|
+
? ArraySplice<Array_, 0, Start>
|
|
97
|
+
: And<
|
|
98
|
+
And<Not<IsNegative<Start>>, Not<IsNegative<End>>>,
|
|
99
|
+
IsEqual<GreaterThan<End, Start>, true>
|
|
100
|
+
> extends true
|
|
101
|
+
? ArraySliceByPositiveIndex<Array_, Start, End>
|
|
102
|
+
: [];
|
|
103
|
+
|
|
104
|
+
type ArraySliceHelper<
|
|
105
|
+
Array_ extends readonly unknown[],
|
|
106
|
+
Start extends number = 0,
|
|
107
|
+
End extends number = Array_['length'],
|
|
108
|
+
TraversedElement extends Array<Array_[number]> = [],
|
|
109
|
+
Result extends Array<Array_[number]> = [],
|
|
110
|
+
ArrayLength extends number = Array_['length'],
|
|
111
|
+
PositiveS extends number = IsNegative<Start> extends true
|
|
112
|
+
? Sum<ArrayLength, Start> extends infer AddResult extends number
|
|
113
|
+
? number extends AddResult // (ArrayLength + Start) < 0
|
|
114
|
+
? 0
|
|
115
|
+
: GreaterThan<AddResult, 0> extends true ? AddResult : 0
|
|
116
|
+
: never
|
|
117
|
+
: Start,
|
|
118
|
+
PositiveE extends number = IsNegative<End> extends true ? Sum<ArrayLength, End> : End,
|
|
119
|
+
> = true extends [IsNegative<PositiveS>, LessThanOrEqual<PositiveE, PositiveS>, GreaterThanOrEqual<PositiveS, ArrayLength>][number]
|
|
120
|
+
? []
|
|
121
|
+
: ArraySliceByPositiveIndex<Array_, TupleMin<[PositiveS, ArrayLength]>, TupleMin<[PositiveE, ArrayLength]>>;
|
|
122
|
+
|
|
123
|
+
type ArraySliceByPositiveIndex<
|
|
124
|
+
Array_ extends readonly unknown[],
|
|
125
|
+
Start extends number,
|
|
126
|
+
End extends number,
|
|
127
|
+
Result extends Array<Array_[number]> = [],
|
|
128
|
+
> = Start extends End
|
|
129
|
+
? Result
|
|
130
|
+
: ArraySliceByPositiveIndex<Array_, Sum<Start, 1>, End, [...Result, Array_[Start]]>;
|
|
131
|
+
|
|
132
|
+
export {};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type {StaticPartOfArray, VariablePartOfArray} from './internal/index.d.ts';
|
|
2
|
+
import type {GreaterThanOrEqual} from './greater-than-or-equal.d.ts';
|
|
3
|
+
import type {Subtract} from './subtract.d.ts';
|
|
4
|
+
import type {UnknownArray} from './unknown-array.d.ts';
|
|
5
|
+
import type {TupleOf} from './tuple-of.d.ts';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
The implementation of `SplitArrayByIndex` for fixed length arrays.
|
|
9
|
+
*/
|
|
10
|
+
type SplitFixedArrayByIndex<T extends UnknownArray, SplitIndex extends number> =
|
|
11
|
+
SplitIndex extends 0
|
|
12
|
+
? [[], T]
|
|
13
|
+
: T extends readonly [...TupleOf<SplitIndex>, ...infer V]
|
|
14
|
+
? T extends readonly [...infer U, ...V]
|
|
15
|
+
? [U, V]
|
|
16
|
+
: [never, never]
|
|
17
|
+
: [never, never];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
The implementation of `SplitArrayByIndex` for variable length arrays.
|
|
21
|
+
*/
|
|
22
|
+
type SplitVariableArrayByIndex<T extends UnknownArray,
|
|
23
|
+
SplitIndex extends number,
|
|
24
|
+
T1 = Subtract<SplitIndex, StaticPartOfArray<T>['length']>,
|
|
25
|
+
T2 = T1 extends number
|
|
26
|
+
? TupleOf<GreaterThanOrEqual<T1, 0> extends true ? T1 : number, VariablePartOfArray<T>[number]>
|
|
27
|
+
: [],
|
|
28
|
+
> =
|
|
29
|
+
SplitIndex extends 0
|
|
30
|
+
? [[], T]
|
|
31
|
+
: GreaterThanOrEqual<StaticPartOfArray<T>['length'], SplitIndex> extends true
|
|
32
|
+
? [
|
|
33
|
+
SplitFixedArrayByIndex<StaticPartOfArray<T>, SplitIndex>[0],
|
|
34
|
+
[
|
|
35
|
+
...SplitFixedArrayByIndex<StaticPartOfArray<T>, SplitIndex>[1],
|
|
36
|
+
...VariablePartOfArray<T>,
|
|
37
|
+
],
|
|
38
|
+
]
|
|
39
|
+
: [
|
|
40
|
+
[
|
|
41
|
+
...StaticPartOfArray<T>,
|
|
42
|
+
...(T2 extends UnknownArray ? T2 : []),
|
|
43
|
+
],
|
|
44
|
+
VariablePartOfArray<T>,
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
Split the given array `T` by the given `SplitIndex`.
|
|
49
|
+
|
|
50
|
+
@example
|
|
51
|
+
```
|
|
52
|
+
type A = SplitArrayByIndex<[1, 2, 3, 4], 2>;
|
|
53
|
+
// type A = [[1, 2], [3, 4]];
|
|
54
|
+
|
|
55
|
+
type B = SplitArrayByIndex<[1, 2, 3, 4], 0>;
|
|
56
|
+
// type B = [[], [1, 2, 3, 4]];
|
|
57
|
+
```
|
|
58
|
+
*/
|
|
59
|
+
type SplitArrayByIndex<T extends UnknownArray, SplitIndex extends number> =
|
|
60
|
+
SplitIndex extends 0
|
|
61
|
+
? [[], T]
|
|
62
|
+
: number extends T['length']
|
|
63
|
+
? SplitVariableArrayByIndex<T, SplitIndex>
|
|
64
|
+
: SplitFixedArrayByIndex<T, SplitIndex>;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
Create a new array type by adding or removing elements at a specified index range in the original array.
|
|
68
|
+
|
|
69
|
+
Use-case: Replace or insert items in an array type.
|
|
70
|
+
|
|
71
|
+
Like [`Array#splice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) but for types.
|
|
72
|
+
|
|
73
|
+
@example
|
|
74
|
+
```
|
|
75
|
+
import type {ArraySplice} from 'type-fest';
|
|
76
|
+
|
|
77
|
+
type SomeMonths0 = ['January', 'April', 'June'];
|
|
78
|
+
type Months0 = ArraySplice<SomeMonths0, 1, 0, ['Feb', 'March']>;
|
|
79
|
+
//=> ['January', 'Feb', 'March', 'April', 'June']
|
|
80
|
+
|
|
81
|
+
type SomeMonths1 = ['January', 'April', 'June'];
|
|
82
|
+
type Months1 = ArraySplice<SomeMonths1, 1, 1>;
|
|
83
|
+
//=> ['January', 'June']
|
|
84
|
+
|
|
85
|
+
type SomeMonths2 = ['January', 'Foo', 'April'];
|
|
86
|
+
type Months2 = ArraySplice<SomeMonths2, 1, 1, ['Feb', 'March']>;
|
|
87
|
+
//=> ['January', 'Feb', 'March', 'April']
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
@category Array
|
|
91
|
+
*/
|
|
92
|
+
export type ArraySplice<
|
|
93
|
+
T extends UnknownArray,
|
|
94
|
+
Start extends number,
|
|
95
|
+
DeleteCount extends number,
|
|
96
|
+
Items extends UnknownArray = [],
|
|
97
|
+
> =
|
|
98
|
+
SplitArrayByIndex<T, Start> extends [infer U extends UnknownArray, infer V extends UnknownArray]
|
|
99
|
+
? SplitArrayByIndex<V, DeleteCount> extends [infer _Deleted extends UnknownArray, infer X extends UnknownArray]
|
|
100
|
+
? [...U, ...Items, ...X]
|
|
101
|
+
: never // Should never happen
|
|
102
|
+
: never; // Should never happen
|
|
103
|
+
|
|
104
|
+
export {};
|