type-fest 5.4.3 → 5.5.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.
Files changed (40) hide show
  1. package/index.d.ts +7 -0
  2. package/package.json +1 -1
  3. package/readme.md +14 -4
  4. package/source/all-extend.d.ts +5 -6
  5. package/source/and-all.d.ts +76 -0
  6. package/source/and.d.ts +3 -2
  7. package/source/array-length.d.ts +36 -0
  8. package/source/array-splice.d.ts +2 -2
  9. package/source/conditional-pick-deep.d.ts +5 -3
  10. package/source/conditional-pick.d.ts +6 -4
  11. package/source/exclude-exactly.d.ts +57 -0
  12. package/source/greater-than-or-equal.d.ts +34 -1
  13. package/source/greater-than.d.ts +34 -1
  14. package/source/internal/array.d.ts +0 -7
  15. package/source/internal/tuple.d.ts +1 -1
  16. package/source/internal/type.d.ts +1 -0
  17. package/source/is-equal.d.ts +0 -1
  18. package/source/is-union.d.ts +2 -1
  19. package/source/less-than-or-equal.d.ts +39 -3
  20. package/source/less-than.d.ts +35 -3
  21. package/source/merge.d.ts +25 -0
  22. package/source/omit-deep.d.ts +1 -2
  23. package/source/optional.d.ts +31 -0
  24. package/source/or-all.d.ts +73 -0
  25. package/source/or.d.ts +3 -10
  26. package/source/package-json.d.ts +2 -2
  27. package/source/paths.d.ts +40 -57
  28. package/source/pick-deep.d.ts +6 -21
  29. package/source/set-non-nullable.d.ts +3 -10
  30. package/source/set-optional.d.ts +1 -5
  31. package/source/set-parameter-type.d.ts +2 -2
  32. package/source/set-readonly.d.ts +1 -5
  33. package/source/set-required.d.ts +1 -5
  34. package/source/shared-union-fields-deep.d.ts +1 -1
  35. package/source/some-extend.d.ts +113 -0
  36. package/source/spread.d.ts +1 -5
  37. package/source/tagged.d.ts +2 -2
  38. package/source/union-member.d.ts +65 -0
  39. package/source/union-to-tuple.d.ts +4 -17
  40. package/source/writable.d.ts +1 -5
package/index.d.ts CHANGED
@@ -43,6 +43,7 @@ export type {ReadonlyDeep} from './source/readonly-deep.d.ts';
43
43
  export type {LiteralUnion} from './source/literal-union.d.ts';
44
44
  export type {Promisable} from './source/promisable.d.ts';
45
45
  export type {Arrayable} from './source/arrayable.d.ts';
46
+ export type {Optional} from './source/optional.d.ts';
46
47
  export type {Opaque, UnwrapOpaque, Tagged, GetTagMetadata, UnwrapTagged} from './source/tagged.d.ts';
47
48
  export type {InvariantOf} from './source/invariant-of.d.ts';
48
49
  export type {SetOptional} from './source/set-optional.d.ts';
@@ -144,6 +145,7 @@ export type {ArraySlice} from './source/array-slice.d.ts';
144
145
  export type {ArraySplice} from './source/array-splice.d.ts';
145
146
  export type {ArrayTail} from './source/array-tail.d.ts';
146
147
  export type {ArrayElement} from './source/array-element.d.ts';
148
+ export type {ArrayLength} from './source/array-length.d.ts';
147
149
  export type {SetFieldType, SetFieldTypeOptions} from './source/set-field-type.d.ts';
148
150
  export type {Paths, PathsOptions} from './source/paths.d.ts';
149
151
  export type {AllUnionFields} from './source/all-union-fields.d.ts';
@@ -153,9 +155,12 @@ export type {IsNull} from './source/is-null.d.ts';
153
155
  export type {IfNull} from './source/if-null.d.ts';
154
156
  export type {IsUndefined} from './source/is-undefined.d.ts';
155
157
  export type {And} from './source/and.d.ts';
158
+ export type {AndAll} from './source/and-all.d.ts';
156
159
  export type {Or} from './source/or.d.ts';
160
+ export type {OrAll} from './source/or-all.d.ts';
157
161
  export type {Xor} from './source/xor.d.ts';
158
162
  export type {AllExtend, AllExtendOptions} from './source/all-extend.d.ts';
163
+ export type {SomeExtend, SomeExtendOptions} from './source/some-extend.d.ts';
159
164
  export type {NonEmptyTuple} from './source/non-empty-tuple.d.ts';
160
165
  export type {FindGlobalInstanceType, FindGlobalType} from './source/find-global-type.d.ts';
161
166
  export type {If} from './source/if.d.ts';
@@ -167,6 +172,7 @@ export type {IsNullable} from './source/is-nullable.d.ts';
167
172
  export type {TupleOf} from './source/tuple-of.d.ts';
168
173
  export type {ExclusifyUnion} from './source/exclusify-union.d.ts';
169
174
  export type {ArrayReverse} from './source/array-reverse.d.ts';
175
+ export type {UnionMember} from './source/union-member.d.ts';
170
176
 
171
177
  // Template literal types
172
178
  export type {CamelCase, CamelCaseOptions} from './source/camel-case.d.ts';
@@ -207,5 +213,6 @@ export type {TsConfigJson} from './source/tsconfig-json.d.ts';
207
213
  export type {ExtendsStrict} from './source/extends-strict.d.ts';
208
214
  export type {ExtractStrict} from './source/extract-strict.d.ts';
209
215
  export type {ExcludeStrict} from './source/exclude-strict.d.ts';
216
+ export type {ExcludeExactly} from './source/exclude-exactly.d.ts';
210
217
 
211
218
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "5.4.3",
3
+ "version": "5.5.0",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
package/readme.md CHANGED
@@ -180,10 +180,13 @@ Click the type names for complete docs.
180
180
  - [`AllUnionFields`](source/all-union-fields.d.ts) - Create a type with all fields from a union of object types.
181
181
  - [`DistributedOmit`](source/distributed-omit.d.ts) - Omits keys from a type, distributing the operation over a union.
182
182
  - [`DistributedPick`](source/distributed-pick.d.ts) - Picks keys from a type, distributing the operation over a union.
183
- - [`And`](source/and.d.ts) - Returns a boolean for whether two given types are both true.
184
- - [`Or`](source/or.d.ts) - Returns a boolean for whether either of two given types is true.
185
- - [`Xor`](source/xor.d.ts) - Returns a boolean for whether only one of two given types is true.
183
+ - [`And`](source/and.d.ts) - Returns a boolean for whether two given types are both `true`.
184
+ - [`Or`](source/or.d.ts) - Returns a boolean for whether either of two given types is `true`.
185
+ - [`Xor`](source/xor.d.ts) - Returns a boolean for whether only one of two given types is `true`.
186
+ - [`AndAll`](source/and-all.d.ts) - Returns a boolean for whether all of the given elements are `true`.
187
+ - [`OrAll`](source/or-all.d.ts) - Returns a boolean for whether any of the given elements is `true`.
186
188
  - [`AllExtend`](source/all-extend.d.ts) - Returns a boolean for whether every element in an array type extends another type.
189
+ - [`SomeExtend`](source/some-extend.d.ts) - Returns a boolean for whether some element in an array type extends another type.
187
190
  - [`NonEmptyTuple`](source/non-empty-tuple.d.ts) - Matches any non-empty tuple.
188
191
  - [`NonEmptyString`](source/non-empty-string.d.ts) - Matches any non-empty string.
189
192
  - [`FindGlobalType`](source/find-global-type.d.ts) - Tries to find the type of a global with the given name.
@@ -191,6 +194,8 @@ Click the type names for complete docs.
191
194
  - [`ConditionalSimplify`](source/conditional-simplify.d.ts) - Simplifies a type while including and/or excluding certain types from being simplified.
192
195
  - [`ConditionalSimplifyDeep`](source/conditional-simplify-deep.d.ts) - Recursively simplifies a type while including and/or excluding certain types from being simplified.
193
196
  - [`ExclusifyUnion`](source/exclusify-union.d.ts) - Ensure mutual exclusivity in object unions by adding other members’ keys as `?: never`.
197
+ - [`Optional`](source/optional.d.ts) - Create a type that represents either the value or `undefined`, while stripping `null` from the type.
198
+ - [`UnionMember`](source/union-member.d.ts) - Returns an arbitrary member of a union type.
194
199
 
195
200
  ### Type Guard
196
201
 
@@ -266,6 +271,7 @@ Click the type names for complete docs.
266
271
  - [`ExtractRestElement`](source/extract-rest-element.d.ts) - Extract the [`rest`](https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types) element type from an array.
267
272
  - [`ExcludeRestElement`](source/exclude-rest-element.d.ts) - Create a tuple with the [`rest`](https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types) element removed.
268
273
  - [`ArrayReverse`](source/array-reverse.d.ts) - Reverse the order of elements in a tuple type.
274
+ - [`ArrayLength`](source/array-length.d.ts) - Return the length of an array. Equivalent to `T['length']` where `T` extends any array.
269
275
 
270
276
  ### Numeric
271
277
 
@@ -281,7 +287,7 @@ Click the type names for complete docs.
281
287
  - [`NonNegativeInteger`](source/numeric.d.ts) - A non-negative (`0 <= x < ∞`) `number` that is an integer.
282
288
  - [`IsNegative`](source/numeric.d.ts) - Returns a boolean for whether the given number is a negative number.
283
289
  - [`IsFloat`](source/is-float.d.ts) - Returns a boolean for whether the given number is a float, like `1.5` or `-1.5`.
284
- - [`IsInteger`](source/is-integer.d.ts) - Returns a boolean for whether the given number is a integer, like `-5`, `1.0` or `100`.
290
+ - [`IsInteger`](source/is-integer.d.ts) - Returns a boolean for whether the given number is an integer, like `-5`, `1.0` or `100`.
285
291
  - [`GreaterThan`](source/greater-than.d.ts) - Returns a boolean for whether a given number is greater than another number.
286
292
  - [`GreaterThanOrEqual`](source/greater-than-or-equal.d.ts) - Returns a boolean for whether a given number is greater than or equal to another number.
287
293
  - [`LessThan`](source/less-than.d.ts) - Returns a boolean for whether a given number is less than another number.
@@ -319,6 +325,7 @@ Click the type names for complete docs.
319
325
  - [`ExtendsStrict`](source/extends-strict.d.ts) - A stricter, non-distributive version of `extends` for checking whether one type is assignable to another.
320
326
  - [`ExtractStrict`](source/extract-strict.d.ts) - A stricter version of `Extract<T, U>` that ensures every member of `U` can successfully extract something from `T`.
321
327
  - [`ExcludeStrict`](source/exclude-strict.d.ts) - A stricter version of `Exclude<T, U>` that ensures every member of `U` can successfully exclude something from `T`.
328
+ - [`ExcludeExactly`](source/exclude-exactly.d.ts) - A stricter version of `Exclude<T, U>` that excludes types only when they are exactly identical.
322
329
 
323
330
  ## Declined types
324
331
 
@@ -353,8 +360,11 @@ Click the type names for complete docs.
353
360
  - `PickByTypes` - See [`ConditionalPick`](source/conditional-pick.d.ts)
354
361
  - `HomomorphicOmit` - See [`Except`](source/except.d.ts)
355
362
  - `IfAny`, `IfNever`, `If*` - See [`If`](source/if.d.ts)
363
+ - `Maybe`, `Option` - See [`Optional`](source/optional.d.ts)
356
364
  - `MaybePromise` - See [`Promisable`](source/promisable.d.ts)
357
365
  - `ReadonlyTuple` - See [`TupleOf`](source/tuple-of.d.ts)
366
+ - `LastOfUnion` - See [`UnionMember`](source/union-member.d.ts)
367
+ - `FirstOfUnion` - See [`UnionMember`](source/union-member.d.ts)
358
368
 
359
369
  ## Tips
360
370
 
@@ -1,4 +1,3 @@
1
- import type {If} from './if.d.ts';
2
1
  import type {CollapseRestElement} from './internal/array.d.ts';
3
2
  import type {ApplyDefaultOptions} from './internal/object.d.ts';
4
3
  import type {IfNotAnyOrNever, Not} from './internal/type.d.ts';
@@ -104,17 +103,17 @@ type B = AllExtend<[1?, 2?, 3?], number | undefined>;
104
103
  export type AllExtend<TArray extends UnknownArray, Type, Options extends AllExtendOptions = {}> =
105
104
  _AllExtend<CollapseRestElement<TArray>, Type, ApplyDefaultOptions<AllExtendOptions, DefaultAllExtendOptions, Options>>;
106
105
 
107
- type _AllExtend<TArray extends UnknownArray, Type, Options extends Required<AllExtendOptions>> = IfNotAnyOrNever<TArray, If<IsAny<Type>, true,
106
+ type _AllExtend<TArray extends UnknownArray, Type, Options extends Required<AllExtendOptions>> = IfNotAnyOrNever<TArray,
108
107
  TArray extends readonly [infer First, ...infer Rest]
109
108
  ? 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.
109
+ ? Or<Or<IsNever<Type>, IsAny<Type>>, Not<Options['strictNever']>> extends true
110
+ // If target `Type` is also `never`, or is `any`, or `strictNever` is disabled, recurse further.
112
111
  ? _AllExtend<Rest, Type, Options>
113
112
  : false
114
113
  : First extends Type
115
114
  ? _AllExtend<Rest, Type, Options>
116
115
  : false
117
- : true
118
- >, false, false>;
116
+ : true,
117
+ false, false>;
119
118
 
120
119
  export {};
@@ -0,0 +1,76 @@
1
+ import type {AllExtend} from './all-extend.d.ts';
2
+
3
+ /**
4
+ Returns a boolean for whether all of the given elements are `true`.
5
+
6
+ Use-cases:
7
+ - Check if all conditions in a list of booleans are met.
8
+
9
+ @example
10
+ ```
11
+ import type {AndAll} from 'type-fest';
12
+
13
+ type TTT = AndAll<[true, true, true]>;
14
+ //=> true
15
+
16
+ type TTF = AndAll<[true, true, false]>;
17
+ //=> false
18
+
19
+ type TFT = AndAll<[true, false, true]>;
20
+ //=> false
21
+ ```
22
+
23
+ Note: When `boolean` is passed as an element, it is distributed into separate cases, and the final result is a union of those cases.
24
+ For example, `AndAll<[true, boolean]>` expands to `AndAll<[true, true]> | AndAll<[true, false]>`, which simplifies to `true | false` (i.e., `boolean`).
25
+
26
+ @example
27
+ ```
28
+ import type {AndAll} from 'type-fest';
29
+
30
+ type A = AndAll<[true, boolean]>;
31
+ //=> boolean
32
+
33
+ type B = AndAll<[false, boolean]>;
34
+ //=> false
35
+ ```
36
+
37
+ Note: If any of the elements is `never`, the result becomes `false`.
38
+
39
+ @example
40
+ ```
41
+ import type {AndAll} from 'type-fest';
42
+
43
+ type A = AndAll<[true, true, never]>;
44
+ //=> false
45
+
46
+ type B = AndAll<[false, never, never]>;
47
+ //=> false
48
+
49
+ type C = AndAll<[never, never, never]>;
50
+ //=> false
51
+
52
+ type D = AndAll<[boolean, true, never]>;
53
+ //=> false
54
+ ```
55
+
56
+ Note: If `any` is passed as an element, it is treated as `boolean` and the result is computed accordingly.
57
+
58
+ @example
59
+ ```
60
+ import type {AndAll} from 'type-fest';
61
+
62
+ type A = AndAll<[false, any]>;
63
+ //=> false
64
+
65
+ type B = AndAll<[true, any]>;
66
+ //=> boolean
67
+ ```
68
+
69
+ Note: `AndAll<[]>` evaluates to `true` due to the concept of [vacuous truth](https://en.wikipedia.org/wiki/Logical_conjunction#:~:text=In%20keeping%20with%20the%20concept%20of%20vacuous%20truth%2C%20when%20conjunction%20is%20defined%20as%20an%20operator%20or%20function%20of%20arbitrary%20arity%2C%20the%20empty%20conjunction%20(AND%2Ding%20over%20an%20empty%20set%20of%20operands)%20is%20often%20defined%20as%20having%20the%20result%20true.), i.e., there are no `false` elements in an empty tuple.
70
+
71
+ @see {@link And}
72
+ @see {@link OrAll}
73
+ */
74
+ export type AndAll<T extends readonly boolean[]> = AllExtend<T, true>;
75
+
76
+ export {};
package/source/and.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type {AllExtend} from './all-extend.d.ts';
1
+ import type {AndAll} from './and-all.d.ts';
2
2
 
3
3
  /**
4
4
  Returns a boolean for whether two given types are both true.
@@ -73,9 +73,10 @@ type G = And<never, never>;
73
73
  //=> false
74
74
  ```
75
75
 
76
+ @see {@link AndAll}
76
77
  @see {@link Or}
77
78
  @see {@link Xor}
78
79
  */
79
- export type And<A extends boolean, B extends boolean> = AllExtend<[A, B], true>;
80
+ export type And<A extends boolean, B extends boolean> = AndAll<[A, B]>;
80
81
 
81
82
  export {};
@@ -0,0 +1,36 @@
1
+ /**
2
+ Return the length of an array. Equivalent to `T['length']` where `T` extends any array.
3
+
4
+ Tuples resolve to numeric literals, while non-tuples resolve to the `number` type.
5
+
6
+ @example
7
+ ```
8
+ import type {ArrayLength} from 'type-fest';
9
+
10
+ type TupleLength = ArrayLength<[1, 2, 3]>;
11
+ //=> 3
12
+
13
+ type TupleWithOptionalMembersLength = ArrayLength<[1, 2, number?]>;
14
+ //=> 2 | 3
15
+
16
+ type NonTupleArrayLength = ArrayLength<string[]>;
17
+ //=> number
18
+
19
+ type TupleWithRestElementLength = ArrayLength<[1, 2, ...string[]]>;
20
+ //=> number
21
+
22
+ // Distinguish between arrays with fixed and non-fixed lengths
23
+ type IsFixedLengthArray<T extends readonly unknown[]> = number extends ArrayLength<T> ? false : true;
24
+
25
+ type A = IsFixedLengthArray<number[]>;
26
+ //=> false
27
+
28
+ type B = IsFixedLengthArray<[1, 2, 3]>;
29
+ //=> true
30
+ ```
31
+
32
+ @category Array
33
+ */
34
+ export type ArrayLength<T extends readonly unknown[]> = T['length'];
35
+
36
+ export {};
@@ -50,10 +50,10 @@ Split the given array `T` by the given `SplitIndex`.
50
50
  @example
51
51
  ```
52
52
  type A = SplitArrayByIndex<[1, 2, 3, 4], 2>;
53
- // type A = [[1, 2], [3, 4]];
53
+ //=> [[1, 2], [3, 4]];
54
54
 
55
55
  type B = SplitArrayByIndex<[1, 2, 3, 4], 0>;
56
- // type B = [[], [1, 2, 3, 4]];
56
+ //=> [[], [1, 2, 3, 4]];
57
57
  ```
58
58
  */
59
59
  type SplitArrayByIndex<T extends UnknownArray, SplitIndex extends number> =
@@ -74,7 +74,7 @@ type BooleanPick = ConditionalPickDeep<Example, boolean | undefined>;
74
74
  //=> {c: {e: {g?: boolean}; j: boolean}}
75
75
 
76
76
  type NumberPick = ConditionalPickDeep<Example, number>;
77
- //=> {}
77
+ //=> never
78
78
 
79
79
  type StringOrBooleanPick = ConditionalPickDeep<Example, string | boolean>;
80
80
  //=> {
@@ -99,11 +99,13 @@ export type ConditionalPickDeep<
99
99
  Type,
100
100
  Condition,
101
101
  Options extends ConditionalPickDeepOptions = {},
102
- > = _ConditionalPickDeep<
102
+ > = _NeverIfEmpty<_ConditionalPickDeep<
103
103
  Type,
104
104
  Condition,
105
105
  ApplyDefaultOptions<ConditionalPickDeepOptions, DefaultConditionalPickDeepOptions, Options>
106
- >;
106
+ >>;
107
+
108
+ type _NeverIfEmpty<Type> = Type extends EmptyObject ? never : Type;
107
109
 
108
110
  type _ConditionalPickDeep<
109
111
  Type,
@@ -1,4 +1,5 @@
1
1
  import type {ConditionalKeys} from './conditional-keys.d.ts';
2
+ import type {IsNever} from './is-never.d.ts';
2
3
 
3
4
  /**
4
5
  Pick keys from the shape that matches the given `Condition`.
@@ -38,9 +39,10 @@ type StringKeysOnly = ConditionalPick<Example, string>;
38
39
 
39
40
  @category Object
40
41
  */
41
- export type ConditionalPick<Base, Condition> = Pick<
42
- Base,
43
- ConditionalKeys<Base, Condition>
44
- >;
42
+ export type ConditionalPick<Base, Condition> = ConditionalKeys<Base, Condition> extends infer Keys
43
+ ? IsNever<Keys> extends true
44
+ ? never
45
+ : Pick<Base, Keys & keyof Base>
46
+ : never;
45
47
 
46
48
  export {};
@@ -0,0 +1,57 @@
1
+ import type {IsNever} from './is-never.d.ts';
2
+ import type {IsAny} from './is-any.d.ts';
3
+ import type {If} from './if.d.ts';
4
+ import type {IsEqual} from './is-equal.d.ts';
5
+ import type {IfNotAnyOrNever} from './internal/type.d.ts';
6
+
7
+ /**
8
+ A stricter version of `Exclude<T, U>` that excludes types only when they are exactly identical.
9
+
10
+ @example
11
+ ```
12
+ import type {ExcludeExactly} from 'type-fest';
13
+
14
+ type TestExclude1 = Exclude<'a' | 'b' | 'c' | 1 | 2 | 3, string>;
15
+ //=> 1 | 2 | 3
16
+
17
+ type TestExcludeExactly1 = ExcludeExactly<'a' | 'b' | 'c' | 1 | 2 | 3, string>;
18
+ //=> 'a' | 'b' | 'c' | 1 | 2 | 3
19
+
20
+ type TestExclude2 = Exclude<'a' | 'b' | 'c' | 1 | 2 | 3, any>;
21
+ //=> never
22
+
23
+ type TestExcludeExactly2 = ExcludeExactly<'a' | 'b' | 'c' | 1 | 2 | 3, any>;
24
+ //=> 'a' | 'b' | 'c' | 1 | 2 | 3
25
+
26
+ type TestExclude3 = Exclude<{a: string} | {a: string; b: string}, {a: string}>;
27
+ //=> never
28
+
29
+ type TestExcludeExactly3 = ExcludeExactly<{a: string} | {a: string; b: string}, {a: string}>;
30
+ //=> {a: string; b: string}
31
+ ```
32
+
33
+ @category Improved Built-in
34
+ */
35
+ export type ExcludeExactly<Union, Delete> =
36
+ IfNotAnyOrNever<
37
+ Union,
38
+ _ExcludeExactly<Union, Delete>,
39
+ // If `Union` is `any`, then if `Delete` is `any`, return `never`, else return `Union`.
40
+ If<IsAny<Delete>, never, Union>,
41
+ // If `Union` is `never`, then if `Delete` is `never`, return `never`, else return `Union`.
42
+ If<IsNever<Delete>, never, Union>
43
+ >;
44
+
45
+ type _ExcludeExactly<Union, Delete> =
46
+ IfNotAnyOrNever<Delete,
47
+ Union extends unknown // For distributing `Union`
48
+ ? [Delete extends unknown // For distributing `Delete`
49
+ ? If<IsEqual<Union, Delete>, true, never>
50
+ : never] extends [never] ? Union : never
51
+ : never,
52
+ // If `Delete` is `any` or `never`, then return `Union`,
53
+ // because `Union` cannot be `any` or `never` here.
54
+ Union, Union
55
+ >;
56
+
57
+ export {};
@@ -16,9 +16,42 @@ type B = GreaterThanOrEqual<1, 1>;
16
16
  type C = GreaterThanOrEqual<1, 5>;
17
17
  //=> false
18
18
  ```
19
+
20
+ Note: If either argument is the non-literal `number` type, the result is `boolean`.
21
+
22
+ @example
23
+ ```
24
+ import type {GreaterThanOrEqual} from 'type-fest';
25
+
26
+ type A = GreaterThanOrEqual<number, 1>;
27
+ //=> boolean
28
+
29
+ type B = GreaterThanOrEqual<1, number>;
30
+ //=> boolean
31
+
32
+ type C = GreaterThanOrEqual<number, number>;
33
+ //=> boolean
34
+ ```
35
+
36
+ @example
37
+ ```
38
+ import type {GreaterThanOrEqual} from 'type-fest';
39
+
40
+ // Use `GreaterThanOrEqual` to constrain a function parameter to non-negative numbers.
41
+ declare function setNonNegative<N extends number>(value: GreaterThanOrEqual<N, 0> extends true ? N : never): void;
42
+
43
+ setNonNegative(0); // ✅ Allowed
44
+ setNonNegative(1); // ✅ Allowed
45
+
46
+ // @ts-expect-error
47
+ setNonNegative(-1);
48
+
49
+ // @ts-expect-error
50
+ setNonNegative(-2);
51
+ ```
19
52
  */
20
53
  export type GreaterThanOrEqual<A extends number, B extends number> = number extends A | B
21
- ? never
54
+ ? boolean
22
55
  : A extends number // For distributing `A`
23
56
  ? B extends number // For distributing `B`
24
57
  ? A extends B
@@ -20,12 +20,45 @@ type B = GreaterThan<1, 1>;
20
20
  type C = GreaterThan<1, 5>;
21
21
  //=> false
22
22
  ```
23
+
24
+ Note: If either argument is the non-literal `number` type, the result is `boolean`.
25
+
26
+ @example
27
+ ```
28
+ import type {GreaterThan} from 'type-fest';
29
+
30
+ type A = GreaterThan<number, 1>;
31
+ //=> boolean
32
+
33
+ type B = GreaterThan<1, number>;
34
+ //=> boolean
35
+
36
+ type C = GreaterThan<number, number>;
37
+ //=> boolean
38
+ ```
39
+
40
+ @example
41
+ ```
42
+ import type {GreaterThan} from 'type-fest';
43
+
44
+ // Use `GreaterThan` to constrain a function parameter to positive numbers.
45
+ declare function setPositive<N extends number>(value: GreaterThan<N, 0> extends true ? N : never): void;
46
+
47
+ setPositive(1); // ✅ Allowed
48
+ setPositive(2); // ✅ Allowed
49
+
50
+ // @ts-expect-error
51
+ setPositive(0);
52
+
53
+ // @ts-expect-error
54
+ setPositive(-1);
55
+ ```
23
56
  */
24
57
  export type GreaterThan<A extends number, B extends number> =
25
58
  A extends number // For distributing `A`
26
59
  ? B extends number // For distributing `B`
27
60
  ? number extends A | B
28
- ? never
61
+ ? boolean
29
62
  : [
30
63
  IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
31
64
  IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
@@ -4,13 +4,6 @@ import type {OptionalKeysOf} from '../optional-keys-of.d.ts';
4
4
  import type {UnknownArray} from '../unknown-array.d.ts';
5
5
  import type {IsExactOptionalPropertyTypesEnabled, IfNotAnyOrNever} from './type.d.ts';
6
6
 
7
- /**
8
- Infer the length of the given array `<T>`.
9
-
10
- @link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
11
- */
12
- type ArrayLength<T extends readonly unknown[]> = T extends {readonly length: infer L} ? L : never;
13
-
14
7
  /**
15
8
  Matches any unknown array or tuple.
16
9
  */
@@ -23,7 +23,7 @@ type Union = TupleLength<[] | [1, 2, 3] | number[]>;
23
23
  */
24
24
  export type TupleLength<T extends UnknownArray> =
25
25
  // `extends unknown` is used to convert `T` (if `T` is a union type) to
26
- // a [distributive conditionaltype](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types))
26
+ // a [distributive conditional type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types))
27
27
  T extends unknown
28
28
  ? number extends T['length']
29
29
  ? never // Return never if the given type is an non-flexed-length array like `Array<string>`
@@ -3,6 +3,7 @@ import type {IsAny} from '../is-any.d.ts';
3
3
  import type {IsNever} from '../is-never.d.ts';
4
4
  import type {Primitive} from '../primitive.d.ts';
5
5
  import type {UnknownArray} from '../unknown-array.d.ts';
6
+ import type {UnionToIntersection} from '../union-to-intersection.d.ts';
6
7
 
7
8
  /**
8
9
  Matches any primitive, `void`, `Date`, or `RegExp` value.
@@ -1,4 +1,3 @@
1
- import type {IsNever} from './is-never.d.ts';
2
1
  /**
3
2
  Returns a boolean for whether the two given types are equal.
4
3
 
@@ -1,4 +1,5 @@
1
1
  import type {IsNever} from './is-never.d.ts';
2
+ import type {IsEqual} from './is-equal.d.ts';
2
3
 
3
4
  /**
4
5
  Returns a boolean for whether the given type is a union.
@@ -24,7 +25,7 @@ type InternalIsUnion<T, U = T> =
24
25
  IsNever<T> extends true
25
26
  ? false
26
27
  : T extends any
27
- ? [U] extends [T]
28
+ ? IsEqual<U, T> extends true
28
29
  ? false
29
30
  : true
30
31
  : never
@@ -16,9 +16,45 @@ type B = LessThanOrEqual<1, 1>;
16
16
  type C = LessThanOrEqual<1, 5>;
17
17
  //=> true
18
18
  ```
19
+
20
+ Note: If either argument is the non-literal `number` type, the result is `boolean`.
21
+
22
+ @example
23
+ ```
24
+ import type {LessThanOrEqual} from 'type-fest';
25
+
26
+ type A = LessThanOrEqual<number, 1>;
27
+ //=> boolean
28
+
29
+ type B = LessThanOrEqual<1, number>;
30
+ //=> boolean
31
+
32
+ type C = LessThanOrEqual<number, number>;
33
+ //=> boolean
34
+ ```
35
+
36
+ @example
37
+ ```
38
+ import type {LessThanOrEqual} from 'type-fest';
39
+
40
+ // Use `LessThanOrEqual` to constrain a function parameter to non-positive numbers.
41
+ declare function setNonPositive<N extends number>(value: LessThanOrEqual<N, 0> extends true ? N : never): void;
42
+
43
+ setNonPositive(0); // ✅ Allowed
44
+ setNonPositive(-1); // ✅ Allowed
45
+
46
+ // @ts-expect-error
47
+ setNonPositive(1);
48
+
49
+ // @ts-expect-error
50
+ setNonPositive(2);
51
+ ```
19
52
  */
20
- export type LessThanOrEqual<A extends number, B extends number> = number extends A | B
21
- ? never
22
- : GreaterThan<A, B> extends true ? false : true;
53
+ export type LessThanOrEqual<A extends number, B extends number> =
54
+ GreaterThan<A, B> extends infer Result
55
+ ? Result extends true
56
+ ? false
57
+ : true
58
+ : never; // Should never happen
23
59
 
24
60
  export {};
@@ -16,10 +16,42 @@ type B = LessThan<1, 1>;
16
16
  type C = LessThan<1, 5>;
17
17
  //=> true
18
18
  ```
19
+
20
+ Note: If either argument is the non-literal `number` type, the result is `boolean`.
21
+
22
+ @example
23
+ ```
24
+ import type {LessThan} from 'type-fest';
25
+
26
+ type A = LessThan<number, 1>;
27
+ //=> boolean
28
+
29
+ type B = LessThan<1, number>;
30
+ //=> boolean
31
+
32
+ type C = LessThan<number, number>;
33
+ //=> boolean
34
+ ```
35
+
36
+ @example
37
+ ```
38
+ import type {LessThan} from 'type-fest';
39
+
40
+ // Use `LessThan` to constrain a function parameter to negative numbers.
41
+ declare function setNegative<N extends number>(value: LessThan<N, 0> extends true ? N : never): void;
42
+
43
+ setNegative(-1); // ✅ Allowed
44
+ setNegative(-2); // ✅ Allowed
45
+
46
+ // @ts-expect-error
47
+ setNegative(0);
48
+
49
+ // @ts-expect-error
50
+ setNegative(1);
51
+ ```
19
52
  */
20
- export type LessThan<A extends number, B extends number> = number extends A | B
21
- ? never
22
- : GreaterThanOrEqual<A, B> extends infer Result
53
+ export type LessThan<A extends number, B extends number> =
54
+ GreaterThanOrEqual<A, B> extends infer Result
23
55
  ? Result extends true
24
56
  ? false
25
57
  : true