@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.
Files changed (201) hide show
  1. package/README.md +25 -0
  2. package/index.d.ts +211 -0
  3. package/license-cc0 +121 -0
  4. package/license-mit +9 -0
  5. package/package.json +85 -0
  6. package/readme.md +1052 -0
  7. package/source/all-extend.d.ts +120 -0
  8. package/source/all-union-fields.d.ts +91 -0
  9. package/source/and.d.ts +81 -0
  10. package/source/array-element.d.ts +46 -0
  11. package/source/array-indices.d.ts +25 -0
  12. package/source/array-reverse.d.ts +84 -0
  13. package/source/array-slice.d.ts +132 -0
  14. package/source/array-splice.d.ts +104 -0
  15. package/source/array-tail.d.ts +70 -0
  16. package/source/array-values.d.ts +24 -0
  17. package/source/arrayable.d.ts +31 -0
  18. package/source/async-return-type.d.ts +28 -0
  19. package/source/asyncify.d.ts +25 -0
  20. package/source/basic.d.ts +38 -0
  21. package/source/camel-case.d.ts +92 -0
  22. package/source/camel-cased-properties-deep.d.ts +99 -0
  23. package/source/camel-cased-properties.d.ts +45 -0
  24. package/source/characters.d.ts +67 -0
  25. package/source/conditional-except.d.ts +47 -0
  26. package/source/conditional-keys.d.ts +63 -0
  27. package/source/conditional-pick-deep.d.ts +120 -0
  28. package/source/conditional-pick.d.ts +46 -0
  29. package/source/conditional-simplify-deep.d.ts +73 -0
  30. package/source/conditional-simplify.d.ts +50 -0
  31. package/source/delimiter-case.d.ts +77 -0
  32. package/source/delimiter-cased-properties-deep.d.ts +108 -0
  33. package/source/delimiter-cased-properties.d.ts +48 -0
  34. package/source/distributed-omit.d.ts +96 -0
  35. package/source/distributed-pick.d.ts +92 -0
  36. package/source/empty-object.d.ts +51 -0
  37. package/source/entries.d.ts +64 -0
  38. package/source/entry.d.ts +67 -0
  39. package/source/exact.d.ts +73 -0
  40. package/source/except.d.ts +112 -0
  41. package/source/exclude-rest-element.d.ts +40 -0
  42. package/source/exclude-strict.d.ts +51 -0
  43. package/source/exclusify-union.d.ts +147 -0
  44. package/source/extends-strict.d.ts +44 -0
  45. package/source/extract-rest-element.d.ts +30 -0
  46. package/source/extract-strict.d.ts +51 -0
  47. package/source/find-global-type.d.ts +68 -0
  48. package/source/fixed-length-array.d.ts +97 -0
  49. package/source/get.d.ts +227 -0
  50. package/source/global-this.d.ts +24 -0
  51. package/source/globals/index.d.ts +3 -0
  52. package/source/globals/observable-like.d.ts +78 -0
  53. package/source/greater-than-or-equal.d.ts +30 -0
  54. package/source/greater-than.d.ts +58 -0
  55. package/source/has-optional-keys.d.ts +23 -0
  56. package/source/has-readonly-keys.d.ts +23 -0
  57. package/source/has-required-keys.d.ts +61 -0
  58. package/source/has-writable-keys.d.ts +23 -0
  59. package/source/if-any.d.ts +28 -0
  60. package/source/if-empty-object.d.ts +30 -0
  61. package/source/if-never.d.ts +28 -0
  62. package/source/if-null.d.ts +28 -0
  63. package/source/if-unknown.d.ts +28 -0
  64. package/source/if.d.ts +102 -0
  65. package/source/includes.d.ts +24 -0
  66. package/source/int-closed-range.d.ts +47 -0
  67. package/source/int-range.d.ts +69 -0
  68. package/source/internal/array.d.ts +151 -0
  69. package/source/internal/characters.d.ts +65 -0
  70. package/source/internal/enforce-optional.d.ts +49 -0
  71. package/source/internal/index.d.ts +11 -0
  72. package/source/internal/keys.d.ts +100 -0
  73. package/source/internal/numeric.d.ts +151 -0
  74. package/source/internal/object.d.ts +292 -0
  75. package/source/internal/string.d.ts +202 -0
  76. package/source/internal/tuple.d.ts +79 -0
  77. package/source/internal/type.d.ts +164 -0
  78. package/source/invariant-of.d.ts +85 -0
  79. package/source/is-any.d.ts +31 -0
  80. package/source/is-equal.d.ts +42 -0
  81. package/source/is-float.d.ts +43 -0
  82. package/source/is-integer.d.ts +60 -0
  83. package/source/is-literal.d.ts +315 -0
  84. package/source/is-lowercase.d.ts +38 -0
  85. package/source/is-never.d.ts +56 -0
  86. package/source/is-null.d.ts +22 -0
  87. package/source/is-nullable.d.ts +30 -0
  88. package/source/is-optional-key-of.d.ts +51 -0
  89. package/source/is-optional.d.ts +28 -0
  90. package/source/is-readonly-key-of.d.ts +55 -0
  91. package/source/is-required-key-of.d.ts +51 -0
  92. package/source/is-tuple.d.ts +92 -0
  93. package/source/is-undefined.d.ts +22 -0
  94. package/source/is-union.d.ts +40 -0
  95. package/source/is-unknown.d.ts +43 -0
  96. package/source/is-uppercase.d.ts +38 -0
  97. package/source/is-writable-key-of.d.ts +51 -0
  98. package/source/iterable-element.d.ts +66 -0
  99. package/source/join.d.ts +79 -0
  100. package/source/json-value.d.ts +33 -0
  101. package/source/jsonifiable.d.ts +37 -0
  102. package/source/jsonify.d.ts +127 -0
  103. package/source/kebab-case.d.ts +46 -0
  104. package/source/kebab-cased-properties-deep.d.ts +65 -0
  105. package/source/kebab-cased-properties.d.ts +42 -0
  106. package/source/key-as-string.d.ts +27 -0
  107. package/source/keys-of-union.d.ts +44 -0
  108. package/source/last-array-element.d.ts +36 -0
  109. package/source/less-than-or-equal.d.ts +24 -0
  110. package/source/less-than.d.ts +28 -0
  111. package/source/literal-to-primitive-deep.d.ts +71 -0
  112. package/source/literal-to-primitive.d.ts +38 -0
  113. package/source/literal-union.d.ts +39 -0
  114. package/source/merge-deep.d.ts +495 -0
  115. package/source/merge-exclusive.d.ts +45 -0
  116. package/source/merge.d.ts +62 -0
  117. package/source/multidimensional-array.d.ts +38 -0
  118. package/source/multidimensional-readonly-array.d.ts +38 -0
  119. package/source/non-empty-object.d.ts +38 -0
  120. package/source/non-empty-string.d.ts +32 -0
  121. package/source/non-empty-tuple.d.ts +24 -0
  122. package/source/numeric.d.ts +226 -0
  123. package/source/object-merge.d.ts +194 -0
  124. package/source/omit-deep.d.ts +154 -0
  125. package/source/omit-index-signature.d.ts +98 -0
  126. package/source/opaque.d.ts +3 -0
  127. package/source/optional-keys-of.d.ts +46 -0
  128. package/source/or.d.ts +89 -0
  129. package/source/override-properties.d.ts +43 -0
  130. package/source/package-json.d.ts +710 -0
  131. package/source/partial-deep.d.ts +155 -0
  132. package/source/partial-on-undefined-deep.d.ts +81 -0
  133. package/source/pascal-case.d.ts +51 -0
  134. package/source/pascal-cased-properties-deep.d.ts +72 -0
  135. package/source/pascal-cased-properties.d.ts +42 -0
  136. package/source/paths.d.ts +241 -0
  137. package/source/pick-deep.d.ts +152 -0
  138. package/source/pick-index-signature.d.ts +52 -0
  139. package/source/primitive.d.ts +15 -0
  140. package/source/promisable.d.ts +27 -0
  141. package/source/readonly-deep.d.ts +116 -0
  142. package/source/readonly-keys-of.d.ts +38 -0
  143. package/source/readonly-tuple.d.ts +34 -0
  144. package/source/remove-prefix.d.ts +132 -0
  145. package/source/replace.d.ts +87 -0
  146. package/source/require-all-or-none.d.ts +54 -0
  147. package/source/require-at-least-one.d.ts +50 -0
  148. package/source/require-exactly-one.d.ts +48 -0
  149. package/source/require-one-or-none.d.ts +49 -0
  150. package/source/required-deep.d.ts +76 -0
  151. package/source/required-keys-of.d.ts +38 -0
  152. package/source/schema.d.ts +119 -0
  153. package/source/screaming-snake-case.d.ts +30 -0
  154. package/source/set-field-type.d.ts +67 -0
  155. package/source/set-non-nullable-deep.d.ts +85 -0
  156. package/source/set-non-nullable.d.ts +41 -0
  157. package/source/set-optional.d.ts +46 -0
  158. package/source/set-parameter-type.d.ts +125 -0
  159. package/source/set-readonly.d.ts +44 -0
  160. package/source/set-required-deep.d.ts +64 -0
  161. package/source/set-required.d.ts +79 -0
  162. package/source/set-return-type.d.ts +31 -0
  163. package/source/shared-union-fields-deep.d.ts +180 -0
  164. package/source/shared-union-fields.d.ts +79 -0
  165. package/source/simplify-deep.d.ts +117 -0
  166. package/source/simplify.d.ts +61 -0
  167. package/source/single-key-object.d.ts +28 -0
  168. package/source/snake-case.d.ts +47 -0
  169. package/source/snake-cased-properties-deep.d.ts +65 -0
  170. package/source/snake-cased-properties.d.ts +42 -0
  171. package/source/split-on-rest-element.d.ts +106 -0
  172. package/source/split.d.ts +90 -0
  173. package/source/spread.d.ts +82 -0
  174. package/source/string-repeat.d.ts +49 -0
  175. package/source/string-slice.d.ts +39 -0
  176. package/source/stringified.d.ts +25 -0
  177. package/source/structured-cloneable.d.ts +89 -0
  178. package/source/subtract.d.ts +86 -0
  179. package/source/sum.d.ts +81 -0
  180. package/source/tagged-union.d.ts +53 -0
  181. package/source/tagged.d.ts +263 -0
  182. package/source/trim.d.ts +29 -0
  183. package/source/tsconfig-json.d.ts +1324 -0
  184. package/source/tuple-of.d.ts +80 -0
  185. package/source/tuple-to-object.d.ts +47 -0
  186. package/source/tuple-to-union.d.ts +54 -0
  187. package/source/typed-array.d.ts +19 -0
  188. package/source/undefined-on-partial-deep.d.ts +83 -0
  189. package/source/union-to-intersection.d.ts +35 -0
  190. package/source/union-to-tuple.d.ts +58 -0
  191. package/source/unknown-array.d.ts +27 -0
  192. package/source/unknown-map.d.ts +26 -0
  193. package/source/unknown-record.d.ts +33 -0
  194. package/source/unknown-set.d.ts +26 -0
  195. package/source/unwrap-partial.d.ts +33 -0
  196. package/source/value-of.d.ts +24 -0
  197. package/source/words.d.ts +122 -0
  198. package/source/writable-deep.d.ts +84 -0
  199. package/source/writable-keys-of.d.ts +34 -0
  200. package/source/writable.d.ts +72 -0
  201. package/source/xor.d.ts +83 -0
@@ -0,0 +1,69 @@
1
+ import type {TupleOf} from './tuple-of.d.ts';
2
+ import type {Subtract} from './subtract.d.ts';
3
+
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.
8
+
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
+
11
+ Note: `Start` or `End` must be non-negative and smaller than `1000`.
12
+
13
+ Use-cases:
14
+ 1. This can be used to define a set of valid input/output values. for example:
15
+
16
+ @example
17
+ ```
18
+ import type {IntRange} from 'type-fest';
19
+
20
+ type Age = IntRange<0, 20>;
21
+ //=> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19
22
+
23
+ type FontSize = IntRange<10, 20>;
24
+ //=> 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19
25
+
26
+ type EvenNumber = IntRange<0, 11, 2>;
27
+ //=> 0 | 2 | 4 | 6 | 8 | 10
28
+ ```
29
+
30
+ 2. This can be used to define random numbers in a range. For example, `type RandomNumber = IntRange<0, 100>;`
31
+
32
+ @example
33
+ ```
34
+ import type {IntRange} from 'type-fest';
35
+
36
+ type ZeroToNine = IntRange<0, 10>;
37
+ //=> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
38
+
39
+ type Hundreds = IntRange<100, 901, 100>;
40
+ //=> 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900
41
+ ```
42
+
43
+ @see {@link IntClosedRange}
44
+ */
45
+ export type IntRange<Start extends number, End extends number, Step extends number = 1> = PrivateIntRange<Start, End, Step>;
46
+
47
+ /**
48
+ The actual implementation of `IntRange`. It's private because it has some arguments that don't need to be exposed.
49
+ */
50
+ type PrivateIntRange<
51
+ Start extends number,
52
+ End extends number,
53
+ Step extends number,
54
+ // The gap between each number, gap = step - 1
55
+ Gap extends number = Subtract<Step, 1>,
56
+ // The final `List` is `[...StartLengthTuple, ...[number, ...GapLengthTuple], ...[number, ...GapLengthTuple], ... ...]`, so can initialize the `List` with `[...StartLengthTuple]`
57
+ List extends unknown[] = TupleOf<Start, never>,
58
+ EndLengthTuple extends unknown[] = TupleOf<End>,
59
+ > = Gap extends 0 ?
60
+ // Handle the case that without `Step`
61
+ List['length'] extends End // The result of "List[length] === End"
62
+ ? Exclude<List[number], never> // All unused elements are `never`, so exclude them
63
+ : PrivateIntRange<Start, End, Step, Gap, [...List, List['length'] ]>
64
+ // Handle the case that with `Step`
65
+ : List extends [...(infer U), ...EndLengthTuple] // The result of "List[length] >= End", because the `...TupleOf<Gap, never>` maybe make `List` too long.
66
+ ? Exclude<List[number], never>
67
+ : PrivateIntRange<Start, End, Step, Gap, [...List, List['length'], ...TupleOf<Gap, never>]>;
68
+
69
+ export {};
@@ -0,0 +1,151 @@
1
+ import type {If} from '../if.d.ts';
2
+ import type {IsNever} from '../is-never.d.ts';
3
+ import type {OptionalKeysOf} from '../optional-keys-of.d.ts';
4
+ import type {UnknownArray} from '../unknown-array.d.ts';
5
+ import type {IsExactOptionalPropertyTypesEnabled, IfNotAnyOrNever} from './type.d.ts';
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
+ /**
15
+ Matches any unknown array or tuple.
16
+ */
17
+ export type UnknownArrayOrTuple = readonly [...unknown[]];
18
+ // TODO: should unknown-array be updated?
19
+
20
+ /**
21
+ Extracts the type of the first element of an array or tuple.
22
+ */
23
+ export type FirstArrayElement<TArray extends UnknownArrayOrTuple> = TArray extends readonly [infer THead, ...unknown[]]
24
+ ? THead
25
+ : never;
26
+
27
+ /**
28
+ Returns the static, fixed-length portion of the given array, excluding variable-length parts.
29
+
30
+ @example
31
+ ```
32
+ type A = [string, number, boolean, ...string[]];
33
+ type B = StaticPartOfArray<A>;
34
+ //=> [string, number, boolean]
35
+ ```
36
+ */
37
+ export type StaticPartOfArray<T extends UnknownArray, Result extends UnknownArray = []> =
38
+ T extends unknown
39
+ ? number extends T['length'] ?
40
+ T extends readonly [infer U, ...infer V]
41
+ ? StaticPartOfArray<V, [...Result, U]>
42
+ : Result
43
+ : T
44
+ : never; // Should never happen
45
+
46
+ /**
47
+ Returns the variable, non-fixed-length portion of the given array, excluding static-length parts.
48
+
49
+ @example
50
+ ```
51
+ type A = [string, number, boolean, ...string[]];
52
+ type B = VariablePartOfArray<A>;
53
+ //=> string[]
54
+ ```
55
+ */
56
+ export type VariablePartOfArray<T extends UnknownArray> =
57
+ T extends unknown
58
+ ? T extends readonly [...StaticPartOfArray<T>, ...infer U]
59
+ ? U
60
+ : []
61
+ : never; // Should never happen
62
+
63
+ /**
64
+ Set the given array to readonly if `IsReadonly` is `true`, otherwise set the given array to normal, then return the result.
65
+
66
+ @example
67
+ ```
68
+ type ReadonlyStringArray = readonly string[];
69
+ type NormalStringArray = string[];
70
+
71
+ type ReadonlyResult = SetArrayAccess<NormalStringArray, true>;
72
+ //=> readonly string[]
73
+
74
+ type NormalResult = SetArrayAccess<ReadonlyStringArray, false>;
75
+ //=> string[]
76
+ ```
77
+ */
78
+ export type SetArrayAccess<T extends UnknownArray, IsReadonly extends boolean> =
79
+ T extends readonly [...infer U] ?
80
+ IsReadonly extends true
81
+ ? readonly [...U]
82
+ : [...U]
83
+ : T;
84
+
85
+ /**
86
+ Returns whether the given array `T` is readonly.
87
+ */
88
+ export type IsArrayReadonly<T extends UnknownArray> = If<IsNever<T>, false, T extends unknown[] ? false : true>;
89
+
90
+ /**
91
+ Transforms a tuple type by replacing it's rest element with a single element that has the same type as the rest element, while keeping all the non-rest elements intact.
92
+
93
+ @example
94
+ ```
95
+ type A = CollapseRestElement<[string, string, ...number[]]>;
96
+ //=> [string, string, number]
97
+
98
+ type B = CollapseRestElement<[...string[], number, number]>;
99
+ //=> [string, number, number]
100
+
101
+ type C = CollapseRestElement<[string, string, ...Array<number | bigint>]>;
102
+ //=> [string, string, number | bigint]
103
+
104
+ type D = CollapseRestElement<[string, number]>;
105
+ //=> [string, number]
106
+ ```
107
+
108
+ Note: Optional modifiers (`?`) are removed from elements unless the `exactOptionalPropertyTypes` compiler option is disabled. When disabled, there's an additional `| undefined` for optional elements.
109
+
110
+ @example
111
+ ```
112
+ // `exactOptionalPropertyTypes` enabled
113
+ type A = CollapseRestElement<[string?, string?, ...number[]]>;
114
+ //=> [string, string, number]
115
+
116
+ // `exactOptionalPropertyTypes` disabled
117
+ type B = CollapseRestElement<[string?, string?, ...number[]]>;
118
+ //=> [string | undefined, string | undefined, number]
119
+ ```
120
+ */
121
+ export type CollapseRestElement<TArray extends UnknownArray> = IfNotAnyOrNever<TArray, _CollapseRestElement<TArray>>;
122
+
123
+ type _CollapseRestElement<
124
+ TArray extends UnknownArray,
125
+ ForwardAccumulator extends UnknownArray = [],
126
+ BackwardAccumulator extends UnknownArray = [],
127
+ > =
128
+ TArray extends UnknownArray // For distributing `TArray`
129
+ ? keyof TArray & `${number}` extends never
130
+ // Enters this branch, if `TArray` is empty (e.g., []),
131
+ // or `TArray` contains no non-rest elements preceding the rest element (e.g., `[...string[]]` or `[...string[], string]`).
132
+ ? TArray extends readonly [...infer Rest, infer Last]
133
+ ? _CollapseRestElement<Rest, ForwardAccumulator, [Last, ...BackwardAccumulator]> // Accumulate elements that are present after the rest element.
134
+ : TArray extends readonly []
135
+ ? [...ForwardAccumulator, ...BackwardAccumulator]
136
+ : [...ForwardAccumulator, TArray[number], ...BackwardAccumulator] // Add the rest element between the accumulated elements.
137
+ : TArray extends readonly [(infer First)?, ...infer Rest]
138
+ ? _CollapseRestElement<
139
+ Rest,
140
+ [
141
+ ...ForwardAccumulator,
142
+ '0' extends OptionalKeysOf<TArray>
143
+ ? If<IsExactOptionalPropertyTypesEnabled, First, First | undefined> // Add `| undefined` for optional elements, if `exactOptionalPropertyTypes` is disabled.
144
+ : First,
145
+ ],
146
+ BackwardAccumulator
147
+ >
148
+ : never // Should never happen, since `[(infer First)?, ...infer Rest]` is a top-type for arrays.
149
+ : never; // Should never happen
150
+
151
+ export {};
@@ -0,0 +1,65 @@
1
+ export type Whitespace =
2
+ | '\u{9}' // '\t'
3
+ | '\u{A}' // '\n'
4
+ | '\u{B}' // '\v'
5
+ | '\u{C}' // '\f'
6
+ | '\u{D}' // '\r'
7
+ | '\u{20}' // ' '
8
+ | '\u{85}'
9
+ | '\u{A0}'
10
+ | '\u{1680}'
11
+ | '\u{2000}'
12
+ | '\u{2001}'
13
+ | '\u{2002}'
14
+ | '\u{2003}'
15
+ | '\u{2004}'
16
+ | '\u{2005}'
17
+ | '\u{2006}'
18
+ | '\u{2007}'
19
+ | '\u{2008}'
20
+ | '\u{2009}'
21
+ | '\u{200A}'
22
+ | '\u{2028}'
23
+ | '\u{2029}'
24
+ | '\u{202F}'
25
+ | '\u{205F}'
26
+ | '\u{3000}'
27
+ | '\u{FEFF}';
28
+
29
+ export type WordSeparators = '-' | '_' | Whitespace;
30
+
31
+ export type AsciiPunctuation =
32
+ | '!'
33
+ | '"'
34
+ | '#'
35
+ | '$'
36
+ | '%'
37
+ | '&'
38
+ | '\''
39
+ | '('
40
+ | ')'
41
+ | '*'
42
+ | '+'
43
+ | ','
44
+ | '-'
45
+ | '.'
46
+ | '/'
47
+ | ':'
48
+ | ';'
49
+ | '<'
50
+ | '='
51
+ | '>'
52
+ | '?'
53
+ | '@'
54
+ | '['
55
+ | '\\'
56
+ | ']'
57
+ | '^'
58
+ | '_'
59
+ | '`'
60
+ | '{'
61
+ | '|'
62
+ | '}'
63
+ | '~';
64
+
65
+ export {};
@@ -0,0 +1,49 @@
1
+ import type {Simplify} from '../simplify.d.ts';
2
+
3
+ // Returns `never` if the key is optional otherwise return the key type.
4
+ type RequiredFilter<Type, Key extends keyof Type> = undefined extends Type[Key]
5
+ ? Type[Key] extends undefined
6
+ ? Key
7
+ : never
8
+ : Key;
9
+
10
+ // Returns `never` if the key is required otherwise return the key type.
11
+ type OptionalFilter<Type, Key extends keyof Type> = undefined extends Type[Key]
12
+ ? Type[Key] extends undefined
13
+ ? never
14
+ : Key
15
+ : never;
16
+
17
+ /**
18
+ Enforce optional keys (by adding the `?` operator) for keys that have a union with `undefined`.
19
+
20
+ @example
21
+ ```
22
+ import type {EnforceOptional} from 'type-fest';
23
+
24
+ type Foo = {
25
+ a: string;
26
+ b?: string;
27
+ c: undefined;
28
+ d: number | undefined;
29
+ };
30
+
31
+ type FooBar = EnforceOptional<Foo>;
32
+ // => {
33
+ // a: string;
34
+ // b?: string;
35
+ // c: undefined;
36
+ // d?: number;
37
+ // }
38
+ ```
39
+
40
+ @internal
41
+ @category Object
42
+ */
43
+ export type EnforceOptional<ObjectType> = Simplify<{
44
+ [Key in keyof ObjectType as RequiredFilter<ObjectType, Key>]: ObjectType[Key]
45
+ } & {
46
+ [Key in keyof ObjectType as OptionalFilter<ObjectType, Key>]?: Exclude<ObjectType[Key], undefined>
47
+ }>;
48
+
49
+ export {};
@@ -0,0 +1,11 @@
1
+ export type * from './array.d.ts';
2
+ export type * from './characters.d.ts';
3
+ export type * from './keys.d.ts';
4
+ export type * from './numeric.d.ts';
5
+ export type * from './object.d.ts';
6
+ export type * from './string.d.ts';
7
+ export type * from './tuple.d.ts';
8
+ export type * from './type.d.ts';
9
+ export type * from './enforce-optional.d.ts';
10
+
11
+ export {};
@@ -0,0 +1,100 @@
1
+ import type {IsAny} from '../is-any.d.ts';
2
+ import type {IsLiteral} from '../is-literal.d.ts';
3
+ import type {IsUnknown} from '../is-unknown.d.ts';
4
+ import type {ToString} from './string.d.ts';
5
+
6
+ // Returns `never` if the key or property is not jsonable without testing whether the property is required or optional otherwise return the key.
7
+ type BaseKeyFilter<Type, Key extends keyof Type> = Key extends symbol
8
+ ? never
9
+ : Type[Key] extends symbol
10
+ ? never
11
+ /*
12
+ To prevent a problem where an object with only a `name` property is incorrectly treated as assignable to a function, we first check if the property is a record.
13
+ This check is necessary, because without it, if we don't verify whether the property is a record, an object with a type of `{name: any}` would return `never` due to its potential assignability to a function.
14
+ See: https://github.com/sindresorhus/type-fest/issues/657
15
+ */
16
+ : Type[Key] extends Record<string, unknown>
17
+ ? Key
18
+ : [(...arguments_: any[]) => any] extends [Type[Key]]
19
+ ? never
20
+ : Key;
21
+
22
+ /**
23
+ Returns the required keys.
24
+ */
25
+ export type FilterDefinedKeys<T extends object> = Exclude<
26
+ {
27
+ [Key in keyof T]: IsAny<T[Key]> extends true
28
+ ? Key
29
+ : IsUnknown<T[Key]> extends true ? Key : undefined extends T[Key]
30
+ ? never
31
+ : T[Key] extends undefined
32
+ ? never
33
+ : BaseKeyFilter<T, Key>;
34
+ }[keyof T],
35
+ undefined
36
+ >;
37
+
38
+ /**
39
+ Returns the optional keys.
40
+ */
41
+ export type FilterOptionalKeys<T extends object> = Exclude<
42
+ {
43
+ [Key in keyof T]: IsAny<T[Key]> extends true
44
+ ? never
45
+ : undefined extends T[Key]
46
+ ? T[Key] extends undefined
47
+ ? never
48
+ : BaseKeyFilter<T, Key>
49
+ : never;
50
+ }[keyof T],
51
+ undefined
52
+ >;
53
+
54
+ /**
55
+ Disallows any of the given keys.
56
+ */
57
+ export type RequireNone<KeysType extends PropertyKey> = Partial<Record<KeysType, never>>;
58
+
59
+ /**
60
+ Utility type to retrieve only literal keys from type.
61
+ */
62
+ export type LiteralKeyOf<T> = keyof {[K in keyof T as IsLiteral<K> extends true ? K : never]-?: never};
63
+
64
+ /**
65
+ Get the exact version of the given `Key` in the given object `T`.
66
+
67
+ Use-case: You know that a number key (e.g. 10) is in an object, but you don't know how it is defined in the object, as a string or as a number (e.g. 10 or '10'). You can use this type to get the exact version of the key. See the example.
68
+
69
+ @example
70
+ ```
71
+ type Object = {
72
+ 0: number;
73
+ '1': string;
74
+ };
75
+
76
+ type Key1 = ExactKey<Object, '0'>;
77
+ //=> 0
78
+ type Key2 = ExactKey<Object, 0>;
79
+ //=> 0
80
+
81
+ type Key3 = ExactKey<Object, '1'>;
82
+ //=> '1'
83
+ type Key4 = ExactKey<Object, 1>;
84
+ //=> '1'
85
+ ```
86
+
87
+ @category Object
88
+ */
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;
99
+
100
+ export {};
@@ -0,0 +1,151 @@
1
+ import type {IsNever} from '../is-never.d.ts';
2
+ import type {Finite, NegativeInfinity, PositiveInfinity} from '../numeric.d.ts';
3
+ import type {UnknownArray} from '../unknown-array.d.ts';
4
+ import type {StringToNumber} from './string.d.ts';
5
+ import type {IfNotAnyOrNever, IsAnyOrNever} from './type.d.ts';
6
+
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
+ /**
25
+ Check whether the given type is a number or a number string.
26
+
27
+ Supports floating-point as a string.
28
+
29
+ @example
30
+ ```
31
+ type A = IsNumberLike<'1'>;
32
+ //=> true
33
+
34
+ type B = IsNumberLike<'-1.1'>;
35
+ //=> true
36
+
37
+ type C = IsNumberLike<'5e-20'>;
38
+ //=> true
39
+
40
+ type D = IsNumberLike<1>;
41
+ //=> true
42
+
43
+ type E = IsNumberLike<'a'>;
44
+ //=> false
45
+ */
46
+ export type IsNumberLike<N> =
47
+ IfNotAnyOrNever<N,
48
+ N extends number | `${number}`
49
+ ? true
50
+ : false,
51
+ boolean, false>;
52
+
53
+ /**
54
+ Returns the minimum number in the given union of numbers.
55
+
56
+ Note: Just supports numbers from 0 to 999.
57
+
58
+ @example
59
+ ```
60
+ type A = UnionMin<1 | 3 | 2>;
61
+ //=> 1
62
+
63
+ type B = UnionMin<number>;
64
+ //=> number
65
+
66
+ type C = UnionMin<any>;
67
+ //=> any
68
+
69
+ type D = UnionMin<never>;
70
+ //=> never
71
+ ```
72
+ */
73
+ export type UnionMin<N extends number> =
74
+ IsAnyOrNever<N> extends true ? N
75
+ : number extends N ? number
76
+ : NegativeInfinity extends N ? NegativeInfinity
77
+ : [N] extends [PositiveInfinity] ? PositiveInfinity
78
+ : InternalUnionMin<Finite<N>>;
79
+
80
+ /**
81
+ The actual implementation of `UnionMin`. It's private because it has some arguments that don't need to be exposed.
82
+ */
83
+ type InternalUnionMin<N extends number, T extends UnknownArray = []> =
84
+ T['length'] extends N
85
+ ? T['length']
86
+ : InternalUnionMin<N, [...T, unknown]>;
87
+
88
+ /**
89
+ Returns the maximum number in the given union of numbers.
90
+
91
+ Note: Just supports numbers from 0 to 999.
92
+
93
+ @example
94
+ ```
95
+ type A = UnionMax<1 | 3 | 2>;
96
+ //=> 3
97
+
98
+ type B = UnionMax<number>;
99
+ //=> number
100
+
101
+ type C = UnionMax<any>;
102
+ //=> any
103
+
104
+ type D = UnionMax<never>;
105
+ //=> never
106
+ ```
107
+ */
108
+ export type UnionMax<N extends number> =
109
+ IsAnyOrNever<N> extends true ? N
110
+ : number extends N ? number
111
+ : PositiveInfinity extends N ? PositiveInfinity
112
+ : [N] extends [NegativeInfinity] ? NegativeInfinity
113
+ : InternalUnionMax<Finite<N>>;
114
+
115
+ /**
116
+ The actual implementation of `UnionMax`. It's private because it has some arguments that don't need to be exposed.
117
+ */
118
+ type InternalUnionMax<N extends number, T extends UnknownArray = []> =
119
+ IsNever<N> extends true
120
+ ? T['length']
121
+ : T['length'] extends N
122
+ ? InternalUnionMax<Exclude<N, T['length']>, T>
123
+ : InternalUnionMax<N, [...T, unknown]>;
124
+
125
+ /**
126
+ Returns the number with reversed sign.
127
+
128
+ @example
129
+ ```
130
+ type A = ReverseSign<-1>;
131
+ //=> 1
132
+
133
+ type B = ReverseSign<1>;
134
+ //=> -1
135
+
136
+ type C = ReverseSign<NegativeInfinity>;
137
+ //=> PositiveInfinity
138
+
139
+ type D = ReverseSign<PositiveInfinity>;
140
+ //=> NegativeInfinity
141
+ ```
142
+ */
143
+ export type ReverseSign<N extends number> =
144
+ // 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;
150
+
151
+ export {};