type-fest 4.41.0 → 5.0.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 (156) hide show
  1. package/index.d.ts +174 -153
  2. package/package.json +20 -44
  3. package/readme.md +45 -68
  4. package/source/all-extend.d.ts +115 -0
  5. package/source/all-union-fields.d.ts +12 -12
  6. package/source/and.d.ts +60 -8
  7. package/source/array-slice.d.ts +11 -11
  8. package/source/array-splice.d.ts +4 -4
  9. package/source/array-tail.d.ts +40 -48
  10. package/source/arrayable.d.ts +2 -2
  11. package/source/asyncify.d.ts +1 -1
  12. package/source/basic.d.ts +1 -33
  13. package/source/camel-case.d.ts +6 -6
  14. package/source/camel-cased-properties-deep.d.ts +7 -7
  15. package/source/camel-cased-properties.d.ts +4 -4
  16. package/source/characters.d.ts +60 -0
  17. package/source/conditional-except.d.ts +4 -4
  18. package/source/conditional-keys.d.ts +33 -19
  19. package/source/conditional-pick-deep.d.ts +9 -9
  20. package/source/conditional-pick.d.ts +3 -3
  21. package/source/conditional-simplify-deep.d.ts +71 -0
  22. package/source/conditional-simplify.d.ts +36 -20
  23. package/source/delimiter-case.d.ts +9 -12
  24. package/source/delimiter-cased-properties-deep.d.ts +4 -4
  25. package/source/delimiter-cased-properties.d.ts +3 -3
  26. package/source/distributed-omit.d.ts +1 -1
  27. package/source/distributed-pick.d.ts +1 -1
  28. package/source/enforce-optional.d.ts +1 -1
  29. package/source/entries.d.ts +1 -1
  30. package/source/exact.d.ts +6 -6
  31. package/source/except.d.ts +2 -2
  32. package/source/exclude-strict.d.ts +45 -0
  33. package/source/extends-strict.d.ts +42 -0
  34. package/source/extract-strict.d.ts +45 -0
  35. package/source/find-global-type.d.ts +1 -1
  36. package/source/fixed-length-array.d.ts +2 -2
  37. package/source/get.d.ts +15 -14
  38. package/source/globals/index.d.ts +1 -0
  39. package/source/{observable-like.d.ts → globals/observable-like.d.ts} +10 -0
  40. package/source/greater-than-or-equal.d.ts +1 -1
  41. package/source/greater-than.d.ts +9 -9
  42. package/source/has-optional-keys.d.ts +1 -1
  43. package/source/has-readonly-keys.d.ts +1 -1
  44. package/source/has-required-keys.d.ts +1 -1
  45. package/source/has-writable-keys.d.ts +1 -1
  46. package/source/if-any.d.ts +3 -1
  47. package/source/if-empty-object.d.ts +3 -1
  48. package/source/if-never.d.ts +3 -1
  49. package/source/if-null.d.ts +3 -1
  50. package/source/if-unknown.d.ts +3 -1
  51. package/source/if.d.ts +65 -0
  52. package/source/includes.d.ts +1 -1
  53. package/source/int-closed-range.d.ts +2 -2
  54. package/source/int-range.d.ts +6 -4
  55. package/source/internal/array.d.ts +54 -22
  56. package/source/internal/characters.d.ts +0 -4
  57. package/source/internal/index.d.ts +8 -8
  58. package/source/internal/keys.d.ts +24 -23
  59. package/source/internal/numeric.d.ts +45 -15
  60. package/source/internal/object.d.ts +58 -29
  61. package/source/internal/string.d.ts +4 -14
  62. package/source/internal/tuple.d.ts +4 -4
  63. package/source/internal/type.d.ts +34 -43
  64. package/source/is-any.d.ts +0 -4
  65. package/source/is-float.d.ts +1 -1
  66. package/source/is-integer.d.ts +4 -4
  67. package/source/is-literal.d.ts +44 -28
  68. package/source/is-lowercase.d.ts +36 -0
  69. package/source/is-nullable.d.ts +28 -0
  70. package/source/is-optional-key-of.d.ts +49 -0
  71. package/source/is-optional.d.ts +26 -0
  72. package/source/is-readonly-key-of.d.ts +53 -0
  73. package/source/is-required-key-of.d.ts +49 -0
  74. package/source/is-tuple.d.ts +15 -14
  75. package/source/is-undefined.d.ts +20 -0
  76. package/source/is-union.d.ts +37 -0
  77. package/source/is-unknown.d.ts +1 -1
  78. package/source/is-uppercase.d.ts +36 -0
  79. package/source/is-writable-key-of.d.ts +49 -0
  80. package/source/json-value.d.ts +31 -0
  81. package/source/jsonifiable.d.ts +1 -1
  82. package/source/jsonify.d.ts +18 -14
  83. package/source/kebab-case.d.ts +3 -3
  84. package/source/kebab-cased-properties-deep.d.ts +4 -4
  85. package/source/kebab-cased-properties.d.ts +4 -4
  86. package/source/{string-key-of.d.ts → key-as-string.d.ts} +5 -5
  87. package/source/keys-of-union.d.ts +1 -1
  88. package/source/less-than-or-equal.d.ts +1 -1
  89. package/source/less-than.d.ts +1 -1
  90. package/source/literal-to-primitive-deep.d.ts +2 -2
  91. package/source/literal-union.d.ts +1 -1
  92. package/source/merge-deep.d.ts +43 -34
  93. package/source/merge.d.ts +5 -5
  94. package/source/multidimensional-array.d.ts +2 -2
  95. package/source/multidimensional-readonly-array.d.ts +2 -2
  96. package/source/non-empty-object.d.ts +2 -2
  97. package/source/numeric.d.ts +4 -4
  98. package/source/omit-deep.d.ts +11 -11
  99. package/source/opaque.d.ts +1 -1
  100. package/source/optional-keys-of.d.ts +10 -5
  101. package/source/or.d.ts +67 -7
  102. package/source/override-properties.d.ts +1 -1
  103. package/source/package-json.d.ts +78 -78
  104. package/source/partial-deep.d.ts +22 -21
  105. package/source/partial-on-undefined-deep.d.ts +5 -4
  106. package/source/pascal-case.d.ts +13 -6
  107. package/source/pascal-cased-properties-deep.d.ts +11 -3
  108. package/source/pascal-cased-properties.d.ts +7 -3
  109. package/source/paths.d.ts +42 -39
  110. package/source/pick-deep.d.ts +6 -6
  111. package/source/readonly-deep.d.ts +2 -2
  112. package/source/readonly-keys-of.d.ts +10 -4
  113. package/source/remove-prefix.d.ts +128 -0
  114. package/source/replace.d.ts +1 -1
  115. package/source/require-all-or-none.d.ts +8 -7
  116. package/source/require-at-least-one.d.ts +10 -9
  117. package/source/require-exactly-one.d.ts +8 -7
  118. package/source/require-one-or-none.d.ts +9 -8
  119. package/source/required-deep.d.ts +26 -32
  120. package/source/required-keys-of.d.ts +4 -4
  121. package/source/schema.d.ts +74 -74
  122. package/source/screaming-snake-case.d.ts +4 -4
  123. package/source/set-field-type.d.ts +2 -2
  124. package/source/set-non-nullable-deep.d.ts +6 -6
  125. package/source/set-optional.d.ts +11 -5
  126. package/source/set-parameter-type.d.ts +7 -7
  127. package/source/set-readonly.d.ts +12 -9
  128. package/source/set-required-deep.d.ts +12 -12
  129. package/source/set-required.d.ts +15 -8
  130. package/source/set-return-type.d.ts +1 -1
  131. package/source/shared-union-fields-deep.d.ts +10 -10
  132. package/source/shared-union-fields.d.ts +5 -4
  133. package/source/simplify-deep.d.ts +3 -3
  134. package/source/single-key-object.d.ts +4 -3
  135. package/source/snake-case.d.ts +3 -3
  136. package/source/snake-cased-properties-deep.d.ts +4 -4
  137. package/source/snake-cased-properties.d.ts +4 -4
  138. package/source/split.d.ts +9 -9
  139. package/source/spread.d.ts +3 -3
  140. package/source/string-repeat.d.ts +2 -2
  141. package/source/string-slice.d.ts +3 -3
  142. package/source/structured-cloneable.d.ts +25 -25
  143. package/source/subtract.d.ts +3 -3
  144. package/source/sum.d.ts +3 -3
  145. package/source/tagged-union.d.ts +1 -1
  146. package/source/tagged.d.ts +3 -1
  147. package/source/trim.d.ts +1 -1
  148. package/source/tsconfig-json.d.ts +2 -1
  149. package/source/tuple-to-object.d.ts +5 -4
  150. package/source/undefined-on-partial-deep.d.ts +1 -1
  151. package/source/union-to-tuple.d.ts +2 -2
  152. package/source/value-of.d.ts +1 -1
  153. package/source/words.d.ts +5 -5
  154. package/source/writable-deep.d.ts +1 -1
  155. package/source/writable-keys-of.d.ts +5 -6
  156. package/source/writable.d.ts +4 -4
package/readme.md CHANGED
@@ -20,34 +20,6 @@
20
20
  <sup>Special thanks to:</sup>
21
21
  <br>
22
22
  <br>
23
- <a href="https://workos.com/?utm_campaign=github_repo&utm_medium=referral&utm_content=type-fest&utm_source=github">
24
- <div>
25
- <img src="https://sindresorhus.com/assets/thanks/workos-logo-white-bg.svg" width="220" alt="WorkOS">
26
- </div>
27
- <b>Your app, enterprise-ready.</b>
28
- <div>
29
- <sub>Start selling to enterprise customers with just a few lines of code.</sub>
30
- <br>
31
- <sup>Add Single Sign-On (and more) in minutes instead of months.</sup>
32
- </div>
33
- </a>
34
- <br>
35
- <br>
36
- <a href="https://logto.io/?ref=sindre">
37
- <div>
38
- <picture>
39
- <source width="200" media="(prefers-color-scheme: dark)" srcset="https://sindresorhus.com/assets/thanks/logto-logo-dark.svg?x">
40
- <source width="200" media="(prefers-color-scheme: light)" srcset="https://sindresorhus.com/assets/thanks/logto-logo-light.svg?x">
41
- <img width="200" src="https://sindresorhus.com/assets/thanks/logto-logo-light.svg?x" alt="Logto logo">
42
- </picture>
43
- </div>
44
- <b>The better identity infrastructure for developers</b>
45
- <div>
46
- <sup>Logto is an open-source Auth0 alternative designed for every app.</sup>
47
- </div>
48
- </a>
49
- <br>
50
- <br>
51
23
  <a href="https://nitric.io/?utm_campaign=github_repo&utm_medium=referral&utm_content=sindresorhus&utm_source=github">
52
24
  <div>
53
25
  <img width="230" src="https://sindresorhus.com/assets/thanks/nitric-logo.svg" alt="nitric logo">
@@ -83,7 +55,10 @@ PR welcome for additional commonly needed types and docs improvements. Read the
83
55
  npm install type-fest
84
56
  ```
85
57
 
86
- *Requires TypeScript >=5.1 and [`{strict: true}`](https://www.typescriptlang.org/tsconfig#strict) in your tsconfig.*
58
+ *Requires TypeScript >=5.9, [ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c), and [`{strict: true}`](https://www.typescriptlang.org/tsconfig#strict) in your tsconfig.*
59
+
60
+ > [!NOTE]
61
+ > This readme shows the current development version. For docs about the latest version, see the [npm page](https://www.npmjs.com/package/type-fest).
87
62
 
88
63
  ## Usage
89
64
 
@@ -108,10 +83,14 @@ Click the type names for complete docs.
108
83
  - [`Primitive`](source/primitive.d.ts) - Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
109
84
  - [`Class`](source/basic.d.ts) - Matches a [`class`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
110
85
  - [`Constructor`](source/basic.d.ts) - Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
111
- - [`AbstractClass`](source/basic.d.ts) - Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/classes.html#abstract-classes).
86
+ - [`AbstractClass`](source/basic.d.ts) - Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/2/classes.html#abstract-classes-and-members).
112
87
  - [`AbstractConstructor`](source/basic.d.ts) - Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-2.html#abstract-construct-signatures) constructor.
113
88
  - [`TypedArray`](source/typed-array.d.ts) - Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`.
114
- - [`ObservableLike`](source/observable-like.d.ts) - Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
89
+ - [`ObservableLike`](source/globals/observable-like.d.ts) - Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
90
+ - [`LowercaseLetter`](source/characters.d.ts) - Matches any lowercase letter in the basic Latin alphabet (a-z).
91
+ - [`UppercaseLetter`](source/characters.d.ts) - Matches any uppercase letter in the basic Latin alphabet (A-Z).
92
+ - [`DigitCharacter`](source/characters.d.ts) - Matches any digit as a string ('0'-'9').
93
+ - [`Alphanumeric`](source/characters.d.ts) - Matches any lowercase letter (a-z), uppercase letter (A-Z), or digit ('0'-'9') in the basic Latin alphabet.
115
94
 
116
95
  ### Utilities
117
96
 
@@ -169,11 +148,11 @@ Click the type names for complete docs.
169
148
  - [`Simplify`](source/simplify.d.ts) - Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
170
149
  - [`SimplifyDeep`](source/simplify-deep.d.ts) - Deeply simplifies an object type.
171
150
  - [`Get`](source/get.d.ts) - Get a deeply-nested property from an object using a key path, like [Lodash's `.get()`](https://lodash.com/docs/latest#get) function.
172
- - [`StringKeyOf`](source/string-key-of.d.ts) - Get keys of the given type as strings.
151
+ - [`KeyAsString`](source/key-as-string.d.ts) - Get keys of the given type as strings.
173
152
  - [`Schema`](source/schema.d.ts) - Create a deep version of another object type where property values are recursively replaced into a given value type.
174
153
  - [`Exact`](source/exact.d.ts) - Create a type that does not allow extra properties.
175
- - [`OptionalKeysOf`](source/optional-keys-of.d.ts) - Extract all optional keys from the given type.
176
154
  - [`KeysOfUnion`](source/keys-of-union.d.ts) - Create a union of all keys from a given type, even those exclusive to specific union members.
155
+ - [`OptionalKeysOf`](source/optional-keys-of.d.ts) - Extract all optional keys from the given type.
177
156
  - [`HasOptionalKeys`](source/has-optional-keys.d.ts) - Create a `true`/`false` type depending on whether the given type has any optional fields.
178
157
  - [`RequiredKeysOf`](source/required-keys-of.d.ts) - Extract all required keys from the given type.
179
158
  - [`HasRequiredKeys`](source/has-required-keys.d.ts) - Create a `true`/`false` type depending on whether the given type has any required fields.
@@ -199,58 +178,47 @@ Click the type names for complete docs.
199
178
  - [`DistributedPick`](source/distributed-pick.d.ts) - Picks keys from a type, distributing the operation over a union.
200
179
  - [`And`](source/and.d.ts) - Returns a boolean for whether two given types are both true.
201
180
  - [`Or`](source/or.d.ts) - Returns a boolean for whether either of two given types are true.
181
+ - [`AllExtend`](source/all-extend.d.ts) - Returns a boolean for whether every element in an array type extends another type.
202
182
  - [`NonEmptyTuple`](source/non-empty-tuple.d.ts) - Matches any non-empty tuple.
203
183
  - [`NonEmptyString`](source/non-empty-string.d.ts) - Matches any non-empty string.
204
184
  - [`FindGlobalType`](source/find-global-type.d.ts) - Tries to find the type of a global with the given name.
205
185
  - [`FindGlobalInstanceType`](source/find-global-type.d.ts) - Tries to find one or more types from their globally-defined constructors.
186
+ - [`ConditionalSimplify`](source/conditional-simplify.d.ts) - Simplifies a type while including and/or excluding certain types from being simplified.
187
+ - [`ConditionalSimplifyDeep`](source/conditional-simplify-deep.d.ts) - Recursively simplifies a type while including and/or excluding certain types from being simplified.
206
188
 
207
189
  ### Type Guard
208
190
 
209
- #### `IsType` vs. `IfType`
210
-
211
- For every `IsT` type (e.g. `IsAny`), there is an associated `IfT` type that can help simplify conditional types. While the `IsT` types return a `boolean`, the `IfT` types act like an `If`/`Else` - they resolve to the given `TypeIfT` or `TypeIfNotT` depending on whether `IsX` is `true` or not. By default, `IfT` returns a `boolean`:
212
-
213
- ```ts
214
- type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = (
215
- IsAny<T> extends true ? TypeIfAny : TypeIfNotAny
216
- );
217
- ```
218
-
219
- #### Usage
220
-
221
- ```ts
222
- import type {IsAny, IfAny} from 'type-fest';
223
-
224
- type ShouldBeTrue = IsAny<any> extends true ? true : false;
225
- //=> true
226
-
227
- type ShouldBeFalse = IfAny<'not any'>;
228
- //=> false
229
-
230
- type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
231
- //=> 'never'
232
- ```
233
-
191
+ - [`If`](source/if.d.ts) - An if-else-like type that resolves depending on whether the given `boolean` type is `true` or `false`.
234
192
  - [`IsLiteral`](source/is-literal.d.ts) - Returns a boolean for whether the given type is a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
235
193
  - [`IsStringLiteral`](source/is-literal.d.ts) - Returns a boolean for whether the given type is a `string` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
236
194
  - [`IsNumericLiteral`](source/is-literal.d.ts) - Returns a boolean for whether the given type is a `number` or `bigint` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
237
195
  - [`IsBooleanLiteral`](source/is-literal.d.ts) - 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).
238
196
  - [`IsSymbolLiteral`](source/is-literal.d.ts) - Returns a boolean for whether the given type is a `symbol` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
239
- - [`IsAny`](source/is-any.d.ts) - Returns a boolean for whether the given type is `any`. (Conditional version: [`IfAny`](source/if-any.d.ts))
240
- - [`IsNever`](source/is-never.d.ts) - Returns a boolean for whether the given type is `never`. (Conditional version: [`IfNever`](source/if-never.d.ts))
241
- - [`IsUnknown`](source/is-unknown.d.ts) - Returns a boolean for whether the given type is `unknown`. (Conditional version: [`IfUnknown`](source/if-unknown.d.ts))
242
- - [`IsEmptyObject`](source/empty-object.d.ts) - Returns a boolean for whether the type is strictly equal to an empty plain object, the `{}` value. (Conditional version: [`IfEmptyObject`](source/if-empty-object.d.ts))
243
- - [`IsNull`](source/is-null.d.ts) - Returns a boolean for whether the given type is `null`. (Conditional version: [`IfNull`](source/if-null.d.ts))
197
+ - [`IsAny`](source/is-any.d.ts) - Returns a boolean for whether the given type is `any`.
198
+ - [`IsNever`](source/is-never.d.ts) - Returns a boolean for whether the given type is `never`.
199
+ - [`IsUnknown`](source/is-unknown.d.ts) - Returns a boolean for whether the given type is `unknown`.
200
+ - [`IsEmptyObject`](source/empty-object.d.ts) - Returns a boolean for whether the type is strictly equal to an empty plain object, the `{}` value.
201
+ - [`IsNull`](source/is-null.d.ts) - Returns a boolean for whether the given type is `null`.
202
+ - [`IsUndefined`](source/is-undefined.d.ts) - Returns a boolean for whether the given type is `undefined`.
244
203
  - [`IsTuple`](source/is-tuple.d.ts) - Returns a boolean for whether the given array is a tuple.
204
+ - [`IsUnion`](source/is-union.d.ts) - Returns a boolean for whether the given type is a union.
205
+ - [`IsLowercase`](source/is-lowercase.d.ts) - Returns a boolean for whether the given string literal is lowercase.
206
+ - [`IsUppercase`](source/is-uppercase.d.ts) - Returns a boolean for whether the given string literal is uppercase.
207
+ - [`IsOptional`](source/is-optional.d.ts) - Returns a boolean for whether the given type includes `undefined`.
208
+ - [`IsNullable`](source/is-nullable.d.ts) - Returns a boolean for whether the given type includes `null`.
209
+ - [`IsOptionalKeyOf`](source/is-optional-key-of.d.ts) - Returns a boolean for whether the given key is an optional key of type.
210
+ - [`IsRequiredKeyOf`](source/is-required-key-of.d.ts) - Returns a boolean for whether the given key is a required key of type.
211
+ - [`IsReadonlyKeyOf`](source/is-readonly-key-of.d.ts) - Returns a boolean for whether the given key is a readonly key of type.
212
+ - [`IsWritableKeyOf`](source/is-writable-key-of.d.ts) - Returns a boolean for whether the given key is a writable key of type.
245
213
 
246
214
  ### JSON
247
215
 
248
216
  - [`Jsonify`](source/jsonify.d.ts) - Transform a type to one that is assignable to the `JsonValue` type.
249
217
  - [`Jsonifiable`](source/jsonifiable.d.ts) - Matches a value that can be losslessly converted to JSON.
250
- - [`JsonPrimitive`](source/basic.d.ts) - Matches a JSON primitive.
251
- - [`JsonObject`](source/basic.d.ts) - Matches a JSON object.
252
- - [`JsonArray`](source/basic.d.ts) - Matches a JSON array.
253
- - [`JsonValue`](source/basic.d.ts) - Matches any valid JSON value.
218
+ - [`JsonPrimitive`](source/json-value.d.ts) - Matches a JSON primitive.
219
+ - [`JsonObject`](source/json-value.d.ts) - Matches a JSON object.
220
+ - [`JsonArray`](source/json-value.d.ts) - Matches a JSON array.
221
+ - [`JsonValue`](source/json-value.d.ts) - Matches any valid JSON value.
254
222
 
255
223
  ### Structured clone
256
224
 
@@ -270,6 +238,7 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
270
238
  - [`Replace`](source/replace.d.ts) - Represents a string with some or all matches replaced by a replacement.
271
239
  - [`StringSlice`](source/string-slice.d.ts) - Returns a string slice of a given range, just like `String#slice()`.
272
240
  - [`StringRepeat`](source/string-repeat.d.ts) - Returns a new string which contains the specified number of copies of a given string, just like `String#repeat()`.
241
+ - [`RemovePrefix`](source/remove-prefix.d.ts) - Removes the specified prefix from the start of a string.
273
242
 
274
243
  ### Array
275
244
 
@@ -333,6 +302,12 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
333
302
  - [`PackageJson`](source/package-json.d.ts) - Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file). It also includes support for [TypeScript Declaration Files](https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html).
334
303
  - [`TsConfigJson`](source/tsconfig-json.d.ts) - Type for [TypeScript's `tsconfig.json` file](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html).
335
304
 
305
+ ### Improved built-in
306
+
307
+ - [`ExtendsStrict`](source/extends-strict.d.ts) - A stricter, non-distributive version of `extends` for checking whether one type is assignable to another.
308
+ - [`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`.
309
+ - [`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`.
310
+
336
311
  ## Declined types
337
312
 
338
313
  *If we decline a type addition, we will make sure to document the better solution here.*
@@ -365,6 +340,8 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
365
340
  - `SetValues` - See [`IterableElement`](source/iterable-element.d.ts)
366
341
  - `PickByTypes` - See [`ConditionalPick`](source/conditional-pick.d.ts)
367
342
  - `HomomorphicOmit` - See [`Except`](source/except.d.ts)
343
+ - `IfAny`, `IfNever`, `If*` - See [`If`](source/if.d.ts)
344
+ - `MaybePromise` - See [`Promisable`](source/promisable.d.ts)
368
345
 
369
346
  ## Tips
370
347
 
@@ -389,7 +366,7 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
389
366
  ### Related
390
367
 
391
368
  - [typed-query-selector](https://github.com/g-plane/typed-query-selector) - Enhances `document.querySelector` and `document.querySelectorAll` with a template literal type that matches element types returned from an HTML element query selector.
392
- - [`Linter.Config`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/eslint/index.d.ts) - Definitions for the [ESLint configuration schema](https://eslint.org/docs/user-guide/configuring/language-options).
369
+ - [`Linter.Config`](https://github.com/eslint/eslint/blob/main/lib/types/index.d.ts) - Definitions for the [ESLint configuration schema](https://eslint.org/docs/user-guide/configuring/language-options).
393
370
 
394
371
  ### Built-in types
395
372
 
@@ -0,0 +1,115 @@
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
+ 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
+ import type {AllExtend} from 'type-fest';
82
+
83
+ // `exactOptionalPropertyTypes` enabled
84
+ type A = AllExtend<[1?, 2?, 3?], number>;
85
+ //=> true
86
+
87
+ // `exactOptionalPropertyTypes` disabled
88
+ type B = AllExtend<[1?, 2?, 3?], number>;
89
+ //=> false
90
+
91
+ // `exactOptionalPropertyTypes` disabled
92
+ type C = AllExtend<[1?, 2?, 3?], number | undefined>;
93
+ //=> true
94
+ ```
95
+
96
+ @see {@link AllExtendOptions}
97
+
98
+ @category Utilities
99
+ @category Array
100
+ */
101
+ export type AllExtend<TArray extends UnknownArray, Type, Options extends AllExtendOptions = {}> =
102
+ _AllExtend<CollapseRestElement<TArray>, Type, ApplyDefaultOptions<AllExtendOptions, DefaultAllExtendOptions, Options>>;
103
+
104
+ type _AllExtend<TArray extends UnknownArray, Type, Options extends Required<AllExtendOptions>> = IfNotAnyOrNever<TArray, If<IsAny<Type>, true,
105
+ TArray extends readonly [infer First, ...infer Rest]
106
+ ? IsNever<First> extends true
107
+ ? Or<IsNever<Type>, Not<Options['strictNever']>> extends true
108
+ // If target `Type` is also `never` OR `strictNever` is disabled, recurse further.
109
+ ? _AllExtend<Rest, Type, Options>
110
+ : false
111
+ : First extends Type
112
+ ? _AllExtend<Rest, Type, Options>
113
+ : false
114
+ : true
115
+ >, false, false>;
@@ -1,8 +1,8 @@
1
- import type {NonRecursiveType, ReadonlyKeysOfUnion, ValueOfUnion} from './internal';
2
- import type {KeysOfUnion} from './keys-of-union';
3
- import type {SharedUnionFields} from './shared-union-fields';
4
- import type {Simplify} from './simplify';
5
- import type {UnknownArray} from './unknown-array';
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
6
 
7
7
  /**
8
8
  Create a type with all fields from a union of object types.
@@ -74,15 +74,15 @@ Extract<Union, NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<un
74
74
  | SkippedMembers
75
75
  | Simplify<
76
76
  // Include fields that are common in all union members
77
- SharedUnionFields<RelevantMembers> &
77
+ SharedUnionFields<RelevantMembers> &
78
78
  // Include readonly fields present in any union member
79
- {
80
- readonly [P in ReadonlyKeysOfUnion<RelevantMembers>]?: ValueOfUnion<RelevantMembers, P & KeysOfUnion<RelevantMembers>>
81
- } &
79
+ {
80
+ readonly [P in ReadonlyKeysOfUnion<RelevantMembers>]?: ValueOfUnion<RelevantMembers, P & KeysOfUnion<RelevantMembers>>
81
+ } &
82
82
  // Include remaining fields that are neither common nor readonly
83
- {
84
- [P in Exclude<KeysOfUnion<RelevantMembers>, ReadonlyKeysOfUnion<RelevantMembers> | keyof RelevantMembers>]?: ValueOfUnion<RelevantMembers, P>
85
- }
83
+ {
84
+ [P in Exclude<KeysOfUnion<RelevantMembers>, ReadonlyKeysOfUnion<RelevantMembers> | keyof RelevantMembers>]?: ValueOfUnion<RelevantMembers, P>
85
+ }
86
86
  >
87
87
  : never
88
88
  : never;
package/source/and.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type {IsEqual} from './is-equal';
1
+ import type {AllExtend} from './all-extend.d.ts';
2
2
 
3
3
  /**
4
4
  Returns a boolean for whether two given types are both true.
@@ -9,17 +9,69 @@ Use-case: Constructing complex conditional types where multiple conditions must
9
9
  ```
10
10
  import type {And} from 'type-fest';
11
11
 
12
- And<true, true>;
12
+ type TT = And<true, true>;
13
13
  //=> true
14
14
 
15
- And<true, false>;
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
+ @example
50
+ ```
51
+ import type {And} from 'type-fest';
52
+
53
+ type A = And<true, never>;
54
+ //=> false
55
+
56
+ type B = And<never, true>;
57
+ //=> false
58
+
59
+ type C = And<false, never>;
60
+ //=> false
61
+
62
+ type D = And<never, false>;
63
+ //=> false
64
+
65
+ type E = And<boolean, never>;
66
+ //=> false
67
+
68
+ type F = And<never, boolean>;
69
+ //=> false
70
+
71
+ type G = And<never, never>;
16
72
  //=> false
17
73
  ```
18
74
 
19
75
  @see {@link Or}
20
76
  */
21
- export type And<A extends boolean, B extends boolean> = [A, B][number] extends true
22
- ? true
23
- : true extends [IsEqual<A, false>, IsEqual<B, false>][number]
24
- ? false
25
- : never;
77
+ export type And<A extends boolean, B extends boolean> = AllExtend<[A, B], true>;
@@ -1,12 +1,12 @@
1
- import type {Sum} from './sum';
2
- import type {LessThanOrEqual} from './less-than-or-equal';
3
- import type {GreaterThanOrEqual} from './greater-than-or-equal';
4
- import type {GreaterThan} from './greater-than';
5
- import type {IsNegative} from './numeric';
6
- import type {Not, TupleMin} from './internal';
7
- import type {IsEqual} from './is-equal';
8
- import type {And} from './and';
9
- import type {ArraySplice} from './array-splice';
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
10
 
11
11
  /**
12
12
  Returns an array slice of a given range, just like `Array#slice()`.
@@ -74,8 +74,8 @@ type VariableLengthArraySliceHelper<
74
74
  > = And<Not<IsNegative<Start>>, IsEqual<End, never>> extends true
75
75
  ? ArraySplice<Array_, 0, Start>
76
76
  : And<
77
- And<Not<IsNegative<Start>>, Not<IsNegative<End>>>,
78
- IsEqual<GreaterThan<End, Start>, true>
77
+ And<Not<IsNegative<Start>>, Not<IsNegative<End>>>,
78
+ IsEqual<GreaterThan<End, Start>, true>
79
79
  > extends true
80
80
  ? ArraySliceByPositiveIndex<Array_, Start, End>
81
81
  : [];
@@ -1,7 +1,7 @@
1
- import type {BuildTuple, StaticPartOfArray, VariablePartOfArray} from './internal';
2
- import type {GreaterThanOrEqual} from './greater-than-or-equal';
3
- import type {Subtract} from './subtract';
4
- import type {UnknownArray} from './unknown-array';
1
+ import type {BuildTuple, 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
5
 
6
6
  /**
7
7
  The implementation of `SplitArrayByIndex` for fixed length arrays.
@@ -1,76 +1,68 @@
1
- import type {ApplyDefaultOptions, IfArrayReadonly} from './internal';
2
- import type {UnknownArray} from './unknown-array';
1
+ import type {If} from './if.d.ts';
2
+ import type {IfNotAnyOrNever, IsArrayReadonly} from './internal/index.d.ts';
3
+ import type {UnknownArray} from './unknown-array.d.ts';
3
4
 
4
5
  /**
5
- @see {@link ArrayTail}
6
- */
7
- type ArrayTailOptions = {
8
- /**
9
- Return a readonly array if the input array is readonly.
6
+ Extracts the type of an array or tuple minus the first element.
10
7
 
11
- @default false
8
+ @example
9
+ ```
10
+ import type {ArrayTail} from 'type-fest';
12
11
 
13
- @example
14
- ```
15
- import type {ArrayTail} from 'type-fest';
12
+ type A = ArrayTail<[1, 2, 3]>;
13
+ //=> [2, 3]
16
14
 
17
- type Example1 = ArrayTail<readonly [string, number, boolean], {preserveReadonly: true}>;
18
- //=> readonly [number, boolean]
15
+ type B = ArrayTail<readonly [1, 2, 3]>;
16
+ //=> readonly [2, 3]
19
17
 
20
- type Example2 = ArrayTail<[string, number, boolean], {preserveReadonly: true}>;
21
- //=> [number, boolean]
18
+ type C = ArrayTail<[1, 2, 3?, ...string[]]>;
19
+ //=> [2, 3?, ...string[]]
22
20
 
23
- type Example3 = ArrayTail<readonly [string, number, boolean], {preserveReadonly: false}>;
24
- //=> [number, boolean]
21
+ type D = ArrayTail<readonly [1]>;
22
+ //=> readonly []
25
23
 
26
- type Example4 = ArrayTail<[string, number, boolean], {preserveReadonly: false}>;
27
- //=> [number, boolean]
28
- ```
29
- */
30
- preserveReadonly?: boolean;
31
- };
24
+ type E = ArrayTail<[]>;
25
+ //=> []
32
26
 
33
- type DefaultArrayTailOptions = {
34
- preserveReadonly: false;
35
- };
27
+ type F = ArrayTail<string[]>;
28
+ //=> string[]
36
29
 
37
- /**
38
- Extracts the type of an array or tuple minus the first element.
30
+ type G = ArrayTail<readonly [...string[], 1, 2]>;
31
+ //=> readonly [...string[], 1, 2]
32
+ ```
39
33
 
40
34
  @example
41
35
  ```
42
36
  import type {ArrayTail} from 'type-fest';
43
37
 
44
- declare const curry: <Arguments extends unknown[], Return>(
45
- function_: (...arguments_: Arguments) => Return,
46
- ...arguments_: ArrayTail<Arguments>
47
- ) => (...arguments_: ArrayTail<Arguments>) => Return;
38
+ type Curry<Func> = Func extends (...agruments_: infer Arguments) => infer Return
39
+ ? Arguments extends readonly []
40
+ ? Return
41
+ : (agrument: Arguments[0]) => Curry<(...agruments_: ArrayTail<Arguments>) => Return>
42
+ : never;
48
43
 
49
- const add = (a: number, b: number) => a + b;
44
+ declare function curry<Func extends Function>(fn: Func): Curry<Func>;
50
45
 
51
- const add3 = curry(add, 3);
46
+ declare function searchBooks(genre: string, minRating: number, available: boolean): string[];
52
47
 
53
- add3(4);
54
- //=> 7
48
+ const availableTopSciFi = curry(searchBooks)('sci-fi')(4.5)(true);
49
+ //=> string[]
55
50
  ```
56
51
 
57
- @see {@link ArrayTailOptions}
58
-
59
52
  @category Array
60
53
  */
61
- export type ArrayTail<TArray extends UnknownArray, Options extends ArrayTailOptions = {}> =
62
- ApplyDefaultOptions<ArrayTailOptions, DefaultArrayTailOptions, Options> extends infer ResolvedOptions extends Required<ArrayTailOptions>
63
- ? TArray extends UnknownArray // For distributing `TArray`
64
- ? _ArrayTail<TArray> extends infer Result
65
- ? ResolvedOptions['preserveReadonly'] extends true
66
- ? IfArrayReadonly<TArray, Readonly<Result>, Result>
67
- : Result
68
- : never // Should never happen
54
+ export type ArrayTail<TArray extends UnknownArray> = IfNotAnyOrNever<TArray,
55
+ TArray extends UnknownArray // For distributing `TArray`
56
+ ? _ArrayTail<TArray> extends infer Result
57
+ ? If<IsArrayReadonly<TArray>, Readonly<Result>, Result>
69
58
  : never // Should never happen
70
- : never; // Should never happen
59
+ : never
60
+ >;
71
61
 
72
62
  type _ArrayTail<TArray extends UnknownArray> = TArray extends readonly [unknown?, ...infer Tail]
73
63
  ? keyof TArray & `${number}` extends never
74
- ? []
64
+ ? TArray extends readonly []
65
+ ? []
66
+ : TArray // Happens when `TArray` is a non-tuple array (e.g., `string[]`) or has a leading rest element (e.g., `[...string[], number]`)
75
67
  : Tail
76
68
  : [];
@@ -24,6 +24,6 @@ bundle('src/index.js', ['dist/index.cjs', 'dist/index.mjs']);
24
24
  @category Array
25
25
  */
26
26
  export type Arrayable<T> =
27
- T
27
+ T
28
28
  // TODO: Use `readonly T[]` when this issue is resolved: https://github.com/microsoft/TypeScript/issues/17002
29
- | T[];
29
+ | T[];
@@ -1,4 +1,4 @@
1
- import type {SetReturnType} from './set-return-type';
1
+ import type {SetReturnType} from './set-return-type.d.ts';
2
2
 
3
3
  /**
4
4
  Create an async version of the given function type, by boxing the return type in `Promise` while keeping the same parameter types.