@depup/type-fest 5.5.0-depup.0 → 5.9.0-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.
Files changed (117) hide show
  1. package/README.md +2 -2
  2. package/changes.json +1 -1
  3. package/index.d.ts +16 -8
  4. package/package.json +8 -6
  5. package/readme.md +85 -61
  6. package/source/absolute.d.ts +52 -0
  7. package/source/all-extend.d.ts +6 -4
  8. package/source/all-union-fields.d.ts +18 -18
  9. package/source/and.d.ts +1 -1
  10. package/source/array-reverse.d.ts +4 -3
  11. package/source/array-splice.d.ts +24 -24
  12. package/source/array-tail.d.ts +4 -4
  13. package/source/camel-case.d.ts +38 -5
  14. package/source/camel-cased-properties-deep.d.ts +11 -4
  15. package/source/camel-cased-properties.d.ts +5 -1
  16. package/source/conditional-keys.d.ts +1 -1
  17. package/source/delimiter-case.d.ts +11 -9
  18. package/source/delimiter-cased-properties-deep.d.ts +8 -1
  19. package/source/delimiter-cased-properties.d.ts +5 -1
  20. package/source/empty-object.d.ts +1 -1
  21. package/source/entries.d.ts +1 -1
  22. package/source/entry.d.ts +1 -1
  23. package/source/exclude-exactly.d.ts +11 -11
  24. package/source/exclude-rest-element.d.ts +4 -4
  25. package/source/exclusify-union.d.ts +4 -4
  26. package/source/extends-strict.d.ts +129 -22
  27. package/source/extract-exactly.d.ts +56 -0
  28. package/source/get.d.ts +1 -1
  29. package/source/greater-than.d.ts +3 -2
  30. package/source/has-optional-keys.d.ts +1 -1
  31. package/source/has-readonly-keys.d.ts +1 -1
  32. package/source/has-required-keys.d.ts +1 -1
  33. package/source/has-writable-keys.d.ts +1 -1
  34. package/source/int-closed-range.d.ts +1 -3
  35. package/source/int-range.d.ts +3 -5
  36. package/source/internal/array.d.ts +8 -8
  37. package/source/internal/keys.d.ts +9 -9
  38. package/source/internal/numeric.d.ts +19 -27
  39. package/source/internal/object.d.ts +44 -6
  40. package/source/internal/string.d.ts +1 -76
  41. package/source/internal/tuple.d.ts +2 -2
  42. package/source/internal/type.d.ts +16 -10
  43. package/source/is-boolean-literal.d.ts +39 -0
  44. package/source/is-integer.d.ts +8 -8
  45. package/source/is-literal.d.ts +43 -267
  46. package/source/is-numeric-literal.d.ts +50 -0
  47. package/source/is-string-literal.d.ts +72 -0
  48. package/source/is-symbol-literal.d.ts +39 -0
  49. package/source/is-union.d.ts +12 -12
  50. package/source/iterable-element.d.ts +5 -5
  51. package/source/jsonify.d.ts +5 -9
  52. package/source/kebab-case.d.ts +1 -0
  53. package/source/kebab-cased-properties-deep.d.ts +7 -0
  54. package/source/kebab-cased-properties.d.ts +5 -1
  55. package/source/keys-of-union.d.ts +2 -2
  56. package/source/last-array-element.d.ts +66 -13
  57. package/source/less-than-or-equal.d.ts +1 -1
  58. package/source/literal-to-primitive.d.ts +1 -1
  59. package/source/literal-union.d.ts +1 -1
  60. package/source/merge-exclusive.d.ts +3 -3
  61. package/source/multidimensional-array.d.ts +1 -1
  62. package/source/multidimensional-readonly-array.d.ts +1 -1
  63. package/source/non-nullable-deep.d.ts +102 -0
  64. package/source/numeric.d.ts +3 -3
  65. package/source/object-merge.d.ts +21 -16
  66. package/source/omit-deep.d.ts +22 -22
  67. package/source/or.d.ts +1 -1
  68. package/source/package-json.d.ts +7 -7
  69. package/source/partial-deep.d.ts +3 -1
  70. package/source/pascal-case.d.ts +1 -0
  71. package/source/pascal-cased-properties-deep.d.ts +7 -0
  72. package/source/pascal-cased-properties.d.ts +5 -1
  73. package/source/pick-deep.d.ts +2 -0
  74. package/source/readonly-deep.d.ts +5 -3
  75. package/source/remove-prefix.d.ts +16 -34
  76. package/source/remove-suffix.d.ts +114 -0
  77. package/source/rename-keys.d.ts +163 -0
  78. package/source/replace.d.ts +2 -2
  79. package/source/require-all-or-none.d.ts +5 -5
  80. package/source/require-at-least-one.d.ts +9 -11
  81. package/source/require-exactly-one.d.ts +7 -7
  82. package/source/require-one-or-none.d.ts +5 -5
  83. package/source/required-deep.d.ts +3 -1
  84. package/source/schema.d.ts +16 -8
  85. package/source/screaming-snake-case.d.ts +1 -0
  86. package/source/set-non-nullable-deep.d.ts +8 -4
  87. package/source/set-non-nullable.d.ts +1 -1
  88. package/source/set-optional.d.ts +5 -5
  89. package/source/set-readonly.d.ts +3 -3
  90. package/source/set-required-deep.d.ts +3 -2
  91. package/source/set-required.d.ts +3 -3
  92. package/source/shared-union-fields-deep.d.ts +4 -3
  93. package/source/shared-union-fields.d.ts +9 -9
  94. package/source/snake-case.d.ts +1 -0
  95. package/source/snake-cased-properties-deep.d.ts +7 -0
  96. package/source/snake-cased-properties.d.ts +5 -1
  97. package/source/some-extend.d.ts +6 -4
  98. package/source/split-on-rest-element.d.ts +6 -4
  99. package/source/split.d.ts +1 -1
  100. package/source/string-length.d.ts +38 -0
  101. package/source/string-repeat.d.ts +48 -21
  102. package/source/string-slice.d.ts +1 -1
  103. package/source/string-to-array.d.ts +97 -0
  104. package/source/string-to-number.d.ts +67 -0
  105. package/source/subtract.d.ts +4 -3
  106. package/source/sum.d.ts +5 -4
  107. package/source/tagged.d.ts +3 -5
  108. package/source/tsconfig-json.d.ts +40 -8
  109. package/source/tuple-of.d.ts +42 -8
  110. package/source/typed-array.d.ts +1 -0
  111. package/source/union-length.d.ts +27 -0
  112. package/source/union-to-intersection.d.ts +1 -1
  113. package/source/union-to-tuple.d.ts +8 -4
  114. package/source/unwrap-required.d.ts +37 -0
  115. package/source/words.d.ts +30 -4
  116. package/source/writable.d.ts +14 -14
  117. package/source/xor.d.ts +1 -1
@@ -1,8 +1,9 @@
1
- import type {NumberAbsolute, PositiveNumericStringGt} from './internal/index.d.ts';
1
+ import type {PositiveNumericStringGt} from './internal/index.d.ts';
2
2
  import type {IsEqual} from './is-equal.d.ts';
3
3
  import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric.d.ts';
4
4
  import type {And} from './and.d.ts';
5
5
  import type {Or} from './or.d.ts';
6
+ import type {Absolute} from './absolute.d.ts';
6
7
 
7
8
  /**
8
9
  Returns a boolean for whether a given number is greater than another number.
@@ -82,7 +83,7 @@ export type GreaterThan<A extends number, B extends number> =
82
83
  ? true
83
84
  : [false, false] extends R
84
85
  ? PositiveNumericStringGt<`${A}`, `${B}`>
85
- : PositiveNumericStringGt<`${NumberAbsolute<B>}`, `${NumberAbsolute<A>}`>
86
+ : PositiveNumericStringGt<`${Absolute<B>}`, `${Absolute<A>}`>
86
87
  : never
87
88
  : never
88
89
  : never // Should never happen
@@ -1,7 +1,7 @@
1
1
  import type {OptionalKeysOf} from './optional-keys-of.d.ts';
2
2
 
3
3
  /**
4
- Creates a type that represents `true` or `false` depending on whether the given type has any optional fields.
4
+ Returns a boolean for whether the given type has any optional fields.
5
5
 
6
6
  This is useful when you want to create an API whose behavior depends on the presence or absence of optional fields.
7
7
 
@@ -1,7 +1,7 @@
1
1
  import type {ReadonlyKeysOf} from './readonly-keys-of.d.ts';
2
2
 
3
3
  /**
4
- Creates a type that represents `true` or `false` depending on whether the given type has any readonly fields.
4
+ Returns a boolean for whether the given type has any readonly fields.
5
5
 
6
6
  This is useful when you want to create an API whose behavior depends on the presence or absence of readonly fields.
7
7
 
@@ -1,7 +1,7 @@
1
1
  import type {RequiredKeysOf} from './required-keys-of.d.ts';
2
2
 
3
3
  /**
4
- Creates a type that represents `true` or `false` depending on whether the given type has any required fields.
4
+ Returns a boolean for whether the given type has any required fields.
5
5
 
6
6
  This is useful when you want to create an API whose behavior depends on the presence or absence of required fields.
7
7
 
@@ -1,7 +1,7 @@
1
1
  import type {WritableKeysOf} from './writable-keys-of.d.ts';
2
2
 
3
3
  /**
4
- Creates a type that represents `true` or `false` depending on whether the given type has any writable fields.
4
+ Returns a boolean for whether the given type has any writable fields.
5
5
 
6
6
  This is useful when you want to create an API whose behavior depends on the presence or absence of writable fields.
7
7
 
@@ -2,9 +2,7 @@ import type {IntRange} from './int-range.d.ts';
2
2
  import type {Sum} from './sum.d.ts';
3
3
 
4
4
  /**
5
- Generate a union of numbers.
6
-
7
- The numbers are created from the given `Start` (inclusive) parameter to the given `End` (inclusive) parameter.
5
+ Generate a union of numbers between a specified start and end (both inclusive), with an optional step.
8
6
 
9
7
  You skip over numbers using the `Step` parameter (defaults to `1`). For example, `IntClosedRange<0, 10, 2>` will create a union of `0 | 2 | 4 | 6 | 8 | 10`.
10
8
 
@@ -2,9 +2,7 @@ import type {TupleOf} from './tuple-of.d.ts';
2
2
  import type {Subtract} from './subtract.d.ts';
3
3
 
4
4
  /**
5
- Generate a union of numbers.
6
-
7
- The numbers are created from the given `Start` (inclusive) parameter to the given `End` (exclusive) parameter.
5
+ Generate a union of numbers between a specified start (inclusive) and end (exclusive), with an optional step.
8
6
 
9
7
  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
8
 
@@ -56,9 +54,9 @@ type PrivateIntRange<
56
54
  // The final `List` is `[...StartLengthTuple, ...[number, ...GapLengthTuple], ...[number, ...GapLengthTuple], ... ...]`, so can initialize the `List` with `[...StartLengthTuple]`
57
55
  List extends unknown[] = TupleOf<Start, never>,
58
56
  EndLengthTuple extends unknown[] = TupleOf<End>,
59
- > = Gap extends 0 ?
57
+ > = Gap extends 0
60
58
  // Handle the case that without `Step`
61
- List['length'] extends End // The result of "List[length] === End"
59
+ ? List['length'] extends End // The result of "List[length] === End"
62
60
  ? Exclude<List[number], never> // All unused elements are `never`, so exclude them
63
61
  : PrivateIntRange<Start, End, Step, Gap, [...List, List['length'] ]>
64
62
  // Handle the case that with `Step`
@@ -29,8 +29,8 @@ type B = StaticPartOfArray<A>;
29
29
  */
30
30
  export type StaticPartOfArray<T extends UnknownArray, Result extends UnknownArray = []> =
31
31
  T extends unknown
32
- ? number extends T['length'] ?
33
- T extends readonly [infer U, ...infer V]
32
+ ? number extends T['length']
33
+ ? T extends readonly [infer U, ...infer V]
34
34
  ? StaticPartOfArray<V, [...Result, U]>
35
35
  : Result
36
36
  : T
@@ -69,11 +69,11 @@ type NormalResult = SetArrayAccess<ReadonlyStringArray, false>;
69
69
  ```
70
70
  */
71
71
  export type SetArrayAccess<T extends UnknownArray, IsReadonly extends boolean> =
72
- T extends readonly [...infer U] ?
73
- IsReadonly extends true
74
- ? readonly [...U]
75
- : [...U]
76
- : T;
72
+ T extends readonly [...infer U]
73
+ ? IsReadonly extends true
74
+ ? readonly [...U]
75
+ : [...U]
76
+ : T;
77
77
 
78
78
  /**
79
79
  Returns whether the given array `T` is readonly.
@@ -111,7 +111,7 @@ type B = CollapseRestElement<[string?, string?, ...number[]]>;
111
111
  //=> [string | undefined, string | undefined, number]
112
112
  ```
113
113
  */
114
- export type CollapseRestElement<TArray extends UnknownArray> = IfNotAnyOrNever<TArray, _CollapseRestElement<TArray>>;
114
+ export type CollapseRestElement<TArray extends UnknownArray> = IfNotAnyOrNever<TArray, {ifNot: _CollapseRestElement<TArray>}>;
115
115
 
116
116
  type _CollapseRestElement<
117
117
  TArray extends UnknownArray,
@@ -87,14 +87,14 @@ type Key4 = ExactKey<Object, 1>;
87
87
  @category Object
88
88
  */
89
89
  export type ExactKey<T extends object, Key extends PropertyKey> =
90
- Key extends keyof T
91
- ? Key
92
- : ToString<Key> extends keyof T
93
- ? ToString<Key>
94
- : Key extends `${infer NumberKey extends number}`
95
- ? NumberKey extends keyof T
96
- ? NumberKey
97
- : never
98
- : never;
90
+ Key extends keyof T
91
+ ? Key
92
+ : ToString<Key> extends keyof T
93
+ ? ToString<Key>
94
+ : Key extends `${infer NumberKey extends number}`
95
+ ? NumberKey extends keyof T
96
+ ? NumberKey
97
+ : never
98
+ : never;
99
99
 
100
100
  export {};
@@ -1,26 +1,8 @@
1
1
  import type {IsNever} from '../is-never.d.ts';
2
2
  import type {Finite, NegativeInfinity, PositiveInfinity} from '../numeric.d.ts';
3
3
  import type {UnknownArray} from '../unknown-array.d.ts';
4
- import type {StringToNumber} from './string.d.ts';
5
4
  import type {IfNotAnyOrNever, IsAnyOrNever} from './type.d.ts';
6
5
 
7
- /**
8
- Returns the absolute value of a given value.
9
-
10
- @example
11
- ```
12
- type A = NumberAbsolute<-1>;
13
- //=> 1
14
-
15
- type B = NumberAbsolute<1>;
16
- //=> 1
17
-
18
- type C = NumberAbsolute<NegativeInfinity>;
19
- //=> PositiveInfinity
20
- ```
21
- */
22
- export type NumberAbsolute<N extends number> = `${N}` extends `-${infer StringPositiveN}` ? StringToNumber<StringPositiveN> : N;
23
-
24
6
  /**
25
7
  Check whether the given type is a number or a number string.
26
8
 
@@ -44,11 +26,13 @@ type E = IsNumberLike<'a'>;
44
26
  //=> false
45
27
  */
46
28
  export type IsNumberLike<N> =
47
- IfNotAnyOrNever<N,
48
- N extends number | `${number}`
29
+ IfNotAnyOrNever<N, {
30
+ ifNot: N extends number | `${number}`
49
31
  ? true
50
- : false,
51
- boolean, false>;
32
+ : false;
33
+ ifAny: boolean;
34
+ ifNever: false;
35
+ }>;
52
36
 
53
37
  /**
54
38
  Returns the minimum number in the given union of numbers.
@@ -142,10 +126,18 @@ type D = ReverseSign<PositiveInfinity>;
142
126
  */
143
127
  export type ReverseSign<N extends number> =
144
128
  // Handle edge cases
145
- N extends 0 ? 0 : N extends PositiveInfinity ? NegativeInfinity : N extends NegativeInfinity ? PositiveInfinity :
146
- // Handle negative numbers
147
- `${N}` extends `-${infer P extends number}` ? P
148
- // Handle positive numbers
149
- : `-${N}` extends `${infer R extends number}` ? R : never;
129
+ N extends 0
130
+ ? 0
131
+ : N extends PositiveInfinity
132
+ ? NegativeInfinity
133
+ : N extends NegativeInfinity
134
+ ? PositiveInfinity
135
+ // Handle negative numbers
136
+ : `${N}` extends `-${infer P extends number}`
137
+ ? P
138
+ // Handle positive numbers
139
+ : `-${N}` extends `${infer R extends number}`
140
+ ? R
141
+ : never;
150
142
 
151
143
  export {};
@@ -3,13 +3,14 @@ import type {IsEqual} from '../is-equal.d.ts';
3
3
  import type {KeysOfUnion} from '../keys-of-union.d.ts';
4
4
  import type {RequiredKeysOf} from '../required-keys-of.d.ts';
5
5
  import type {Merge} from '../merge.d.ts';
6
- import type {OptionalKeysOf} from '../optional-keys-of.d.ts';
7
6
  import type {IsAny} from '../is-any.d.ts';
8
7
  import type {If} from '../if.d.ts';
9
8
  import type {IsNever} from '../is-never.d.ts';
9
+ import type {StringToNumber} from '../string-to-number.d.ts';
10
+ import type {Primitive} from '../primitive.d.ts';
10
11
  import type {FilterDefinedKeys, FilterOptionalKeys} from './keys.d.ts';
11
- import type {MapsSetsOrArrays, NonRecursiveType} from './type.d.ts';
12
- import type {StringToNumber, ToString} from './string.d.ts';
12
+ import type {IfNotAnyOrNever, MapsSetsOrArrays, NonRecursiveType} from './type.d.ts';
13
+ import type {ToString} from './string.d.ts';
13
14
 
14
15
  /**
15
16
  Create an object type with the given key `<Key>` and value `<Value>`.
@@ -223,14 +224,23 @@ export type ApplyDefaultOptions<
223
224
  Options extends object,
224
225
  Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
225
226
  SpecifiedOptions extends Options,
227
+ > =
228
+ _ApplyDefaultOptions<Options, Defaults, SpecifiedOptions> extends infer Result extends Required<Options> // `extends Required<Options>` ensures that `ApplyDefaultOptions<SomeOption, ...>` is always assignable to `Required<SomeOption>`
229
+ ? Result
230
+ : never;
231
+
232
+ type _ApplyDefaultOptions<
233
+ Options,
234
+ Defaults,
235
+ SpecifiedOptions,
226
236
  > =
227
237
  If<IsAny<SpecifiedOptions>, Defaults,
228
238
  If<IsNever<SpecifiedOptions>, Defaults,
229
- Simplify<Merge<Defaults, {
239
+ Merge<Defaults, {
230
240
  [Key in keyof SpecifiedOptions
231
- as Key extends OptionalKeysOf<Options> ? undefined extends SpecifiedOptions[Key] ? never : Key : Key
241
+ as undefined extends Required<Options>[Key & keyof Options] ? Key : undefined extends SpecifiedOptions[Key] ? never : Key
232
242
  ]: SpecifiedOptions[Key]
233
- }> & Required<Options>>>>; // `& Required<Options>` ensures that `ApplyDefaultOptions<SomeOption, ...>` is always assignable to `Required<SomeOption>`
243
+ }>>>;
234
244
 
235
245
  /**
236
246
  Collapses literal types in a union into their corresponding primitive types, when possible. For example, `CollapseLiterals<'foo' | 'bar' | (string & {})>` returns `string`.
@@ -265,6 +275,34 @@ export type CollapseLiterals<T> = {} extends T
265
275
  ? U
266
276
  : T;
267
277
 
278
+ /**
279
+ Returns the base type of a branded type.
280
+
281
+ @example
282
+ ```
283
+ type Brand = {readonly __brand: unique symbol};
284
+
285
+ type A = UnwrapBrand<string & Brand>;
286
+ //=> string
287
+
288
+ type B = UnwrapBrand<number & Brand>;
289
+ //=> number
290
+
291
+ type C = UnwrapBrand<PropertyKey & Brand>;
292
+ //=> PropertyKey
293
+
294
+ type D = UnwrapBrand<(1 | 200n | 'foo' | 'bar') & Brand>;
295
+ //=> 1 | 200n | 'foo' | 'bar'
296
+ ```
297
+ */
298
+ export type UnwrapBrand<T> = IfNotAnyOrNever<T, {ifNot: _UnwrapBrand<T, Primitive>}>;
299
+
300
+ type _UnwrapBrand<T, Base> = T extends Primitive
301
+ ? T extends infer U & Pick<T, Exclude<keyof T, KeysOfUnion<Base>>>
302
+ ? U
303
+ : T
304
+ : T;
305
+
268
306
  /**
269
307
  Normalize keys by including string and number representations wherever applicable.
270
308
 
@@ -1,6 +1,6 @@
1
1
  import type {TupleOf} from '../tuple-of.d.ts';
2
- import type {NegativeInfinity, PositiveInfinity} from '../numeric.d.ts';
3
2
  import type {Trim} from '../trim.d.ts';
3
+ import type {StringLength} from '../string-length.d.ts';
4
4
  import type {Whitespace} from './characters.d.ts';
5
5
 
6
6
  /**
@@ -10,42 +10,6 @@ Note: This type is not the return type of the `.toString()` function.
10
10
  */
11
11
  export type ToString<T> = T extends string | number ? `${T}` : never;
12
12
 
13
- /**
14
- Converts a numeric string to a number.
15
-
16
- @example
17
- ```
18
- type PositiveInt = StringToNumber<'1234'>;
19
- //=> 1234
20
-
21
- type NegativeInt = StringToNumber<'-1234'>;
22
- //=> -1234
23
-
24
- type PositiveFloat = StringToNumber<'1234.56'>;
25
- //=> 1234.56
26
-
27
- type NegativeFloat = StringToNumber<'-1234.56'>;
28
- //=> -1234.56
29
-
30
- type PositiveInfinity = StringToNumber<'Infinity'>;
31
- //=> Infinity
32
-
33
- type NegativeInfinity = StringToNumber<'-Infinity'>;
34
- //=> -Infinity
35
- ```
36
-
37
- @category String
38
- @category Numeric
39
- @category Template literal
40
- */
41
- export type StringToNumber<S extends string> = S extends `${infer N extends number}`
42
- ? N
43
- : S extends 'Infinity'
44
- ? PositiveInfinity
45
- : S extends '-Infinity'
46
- ? NegativeInfinity
47
- : never;
48
-
49
13
  /**
50
14
  Returns a boolean for whether the given string `S` starts with the given string `SearchString`.
51
15
 
@@ -73,45 +37,6 @@ export type StartsWith<S extends string, SearchString extends string> = string e
73
37
  ? true
74
38
  : false;
75
39
 
76
- /**
77
- Returns an array of the characters of the string.
78
-
79
- @example
80
- ```
81
- type A = StringToArray<'abcde'>;
82
- //=> ['a', 'b', 'c', 'd', 'e']
83
-
84
- type B = StringToArray<string>;
85
- //=> never
86
- ```
87
-
88
- @category String
89
- */
90
- export type StringToArray<S extends string, Result extends string[] = []> = string extends S
91
- ? never
92
- : S extends `${infer F}${infer R}`
93
- ? StringToArray<R, [...Result, F]>
94
- : Result;
95
-
96
- /**
97
- Returns the length of the given string.
98
-
99
- @example
100
- ```
101
- type A = StringLength<'abcde'>;
102
- //=> 5
103
-
104
- type B = StringLength<string>;
105
- //=> never
106
- ```
107
-
108
- @category String
109
- @category Template literal
110
- */
111
- export type StringLength<S extends string> = string extends S
112
- ? never
113
- : StringToArray<S>['length'];
114
-
115
40
  /**
116
41
  Returns a boolean for whether a string is whitespace.
117
42
  */
@@ -46,8 +46,8 @@ type B = TupleMax<[1, 2, 5, 3, 99, -1]>;
46
46
  ```
47
47
  */
48
48
  export type TupleMax<A extends number[], Result extends number = NegativeInfinity> = number extends A[number]
49
- ? never :
50
- A extends [infer F extends number, ...infer R extends number[]]
49
+ ? never
50
+ : A extends [infer F extends number, ...infer R extends number[]]
51
51
  ? GreaterThan<F, Result> extends true
52
52
  ? TupleMax<R, F>
53
53
  : TupleMax<R, Result>
@@ -1,9 +1,7 @@
1
- import type {If} from '../if.d.ts';
2
1
  import type {IsAny} from '../is-any.d.ts';
3
2
  import type {IsNever} from '../is-never.d.ts';
4
3
  import type {Primitive} from '../primitive.d.ts';
5
4
  import type {UnknownArray} from '../unknown-array.d.ts';
6
- import type {UnionToIntersection} from '../union-to-intersection.d.ts';
7
5
 
8
6
  /**
9
7
  Matches any primitive, `void`, `Date`, or `RegExp` value.
@@ -91,16 +89,16 @@ An if-else-like type that resolves depending on whether the given type is `any`
91
89
 
92
90
  @example
93
91
  ```
94
- // When `T` is a NOT `any` or `never` (like `string`) => Returns `IfNotAnyOrNever` branch
95
- type A = IfNotAnyOrNever<string, 'VALID', 'IS_ANY', 'IS_NEVER'>;
92
+ // When `T` is neither `any` nor `never` (like `string`) => Returns `IfNot` branch
93
+ type A = IfNotAnyOrNever<string, {ifNot: 'VALID'; ifAny: 'IS_ANY'; ifNever: 'IS_NEVER'}>;
96
94
  //=> 'VALID'
97
95
 
98
96
  // When `T` is `any` => Returns `IfAny` branch
99
- type B = IfNotAnyOrNever<any, 'VALID', 'IS_ANY', 'IS_NEVER'>;
97
+ type B = IfNotAnyOrNever<any, {ifNot: 'VALID'; ifAny: 'IS_ANY'; ifNever: 'IS_NEVER'}>;
100
98
  //=> 'IS_ANY'
101
99
 
102
100
  // When `T` is `never` => Returns `IfNever` branch
103
- type C = IfNotAnyOrNever<never, 'VALID', 'IS_ANY', 'IS_NEVER'>;
101
+ type C = IfNotAnyOrNever<never, {ifNot: 'VALID'; ifAny: 'IS_ANY'; ifNever: 'IS_NEVER'}>;
104
102
  //=> 'IS_NEVER'
105
103
  ```
106
104
 
@@ -113,7 +111,7 @@ import type {StringRepeat} from 'type-fest';
113
111
  type NineHundredNinetyNineSpaces = StringRepeat<' ', 999>;
114
112
 
115
113
  // The following implementation is not tail recursive
116
- type TrimLeft<S extends string> = IfNotAnyOrNever<S, S extends ` ${infer R}` ? TrimLeft<R> : S>;
114
+ type TrimLeft<S extends string> = IfNotAnyOrNever<S, {ifNot: S extends ` ${infer R}` ? TrimLeft<R> : S}>;
117
115
 
118
116
  // Hence, instantiations with long strings will fail
119
117
  // @ts-expect-error
@@ -122,7 +120,7 @@ type T1 = TrimLeft<NineHundredNinetyNineSpaces>;
122
120
  // Error: Type instantiation is excessively deep and possibly infinite.
123
121
 
124
122
  // To fix this, move the recursion into a helper type
125
- type TrimLeftOptimised<S extends string> = IfNotAnyOrNever<S, _TrimLeftOptimised<S>>;
123
+ type TrimLeftOptimised<S extends string> = IfNotAnyOrNever<S, {ifNot: _TrimLeftOptimised<S>}>;
126
124
 
127
125
  type _TrimLeftOptimised<S extends string> = S extends ` ${infer R}` ? _TrimLeftOptimised<R> : S;
128
126
 
@@ -130,8 +128,16 @@ type T2 = TrimLeftOptimised<NineHundredNinetyNineSpaces>;
130
128
  //=> ''
131
129
  ```
132
130
  */
133
- export type IfNotAnyOrNever<T, IfNotAnyOrNever, IfAny = any, IfNever = never> =
134
- If<IsAny<T>, IfAny, If<IsNever<T>, IfNever, IfNotAnyOrNever>>;
131
+ export type IfNotAnyOrNever<T, Cases extends {ifNot: unknown; ifAny?: unknown; ifNever?: unknown}> =
132
+ IsAny<T> extends true
133
+ ? 'ifAny' extends keyof Cases
134
+ ? Cases['ifAny']
135
+ : any
136
+ : IsNever<T> extends true
137
+ ? 'ifNever' extends keyof Cases
138
+ ? Cases['ifNever']
139
+ : never
140
+ : Cases['ifNot'];
135
141
 
136
142
  /**
137
143
  Returns a boolean for whether the given type is `any` or `never`.
@@ -0,0 +1,39 @@
1
+ import type {CollapseLiterals, UnwrapBrand} from './internal/object.d.ts';
2
+ import type {IfNotAnyOrNever} from './internal/type.d.ts';
3
+
4
+ /**
5
+ Returns a boolean for whether the given type is a `true` or `false` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
6
+
7
+ @example
8
+ ```
9
+ import type {IsBooleanLiteral} from 'type-fest';
10
+
11
+ type A = IsBooleanLiteral<true>;
12
+ //=> true
13
+
14
+ type B = IsBooleanLiteral<false>;
15
+ //=> true
16
+
17
+ type C = IsBooleanLiteral<boolean>;
18
+ //=> false
19
+
20
+ type D = IsBooleanLiteral<true | false>;
21
+ //=> false
22
+ ```
23
+
24
+ @category Type Guard
25
+ @category Utilities
26
+ */
27
+ export type IsBooleanLiteral<T> = IfNotAnyOrNever<T, {
28
+ ifNot: _IsBooleanLiteral<CollapseLiterals<UnwrapBrand<T>>>;
29
+ ifAny: false;
30
+ ifNever: false;
31
+ }>;
32
+
33
+ type _IsBooleanLiteral<T> = boolean extends T
34
+ ? false
35
+ : T extends boolean
36
+ ? true
37
+ : false;
38
+
39
+ export {};
@@ -47,14 +47,14 @@ type J = IsInteger<1e-7>;
47
47
  @category Numeric
48
48
  */
49
49
  export type IsInteger<T> =
50
- T extends bigint
51
- ? true
52
- : T extends number
53
- ? number extends T
54
- ? false
55
- : T extends PositiveInfinity | NegativeInfinity
50
+ T extends bigint
51
+ ? true
52
+ : T extends number
53
+ ? number extends T
56
54
  ? false
57
- : Not<IsFloat<T>>
58
- : false;
55
+ : T extends PositiveInfinity | NegativeInfinity
56
+ ? false
57
+ : Not<IsFloat<T>>
58
+ : false;
59
59
 
60
60
  export {};