@depup/type-fest 5.4.4-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 (130) hide show
  1. package/README.md +2 -2
  2. package/changes.json +5 -0
  3. package/index.d.ts +23 -8
  4. package/package.json +17 -11
  5. package/readme.md +98 -64
  6. package/source/absolute.d.ts +52 -0
  7. package/source/all-extend.d.ts +8 -7
  8. package/source/all-union-fields.d.ts +18 -18
  9. package/source/and-all.d.ts +76 -0
  10. package/source/and.d.ts +4 -3
  11. package/source/array-length.d.ts +36 -0
  12. package/source/array-reverse.d.ts +4 -3
  13. package/source/array-splice.d.ts +26 -26
  14. package/source/array-tail.d.ts +4 -4
  15. package/source/camel-case.d.ts +38 -5
  16. package/source/camel-cased-properties-deep.d.ts +11 -4
  17. package/source/camel-cased-properties.d.ts +5 -1
  18. package/source/conditional-keys.d.ts +1 -1
  19. package/source/conditional-pick-deep.d.ts +5 -3
  20. package/source/conditional-pick.d.ts +6 -4
  21. package/source/delimiter-case.d.ts +11 -9
  22. package/source/delimiter-cased-properties-deep.d.ts +8 -1
  23. package/source/delimiter-cased-properties.d.ts +5 -1
  24. package/source/empty-object.d.ts +1 -1
  25. package/source/entries.d.ts +1 -1
  26. package/source/entry.d.ts +1 -1
  27. package/source/exclude-exactly.d.ts +57 -0
  28. package/source/exclude-rest-element.d.ts +4 -4
  29. package/source/exclusify-union.d.ts +4 -4
  30. package/source/extends-strict.d.ts +129 -22
  31. package/source/extract-exactly.d.ts +56 -0
  32. package/source/get.d.ts +1 -1
  33. package/source/greater-than-or-equal.d.ts +34 -1
  34. package/source/greater-than.d.ts +37 -3
  35. package/source/has-optional-keys.d.ts +1 -1
  36. package/source/has-readonly-keys.d.ts +1 -1
  37. package/source/has-required-keys.d.ts +1 -1
  38. package/source/has-writable-keys.d.ts +1 -1
  39. package/source/int-closed-range.d.ts +1 -3
  40. package/source/int-range.d.ts +3 -5
  41. package/source/internal/array.d.ts +8 -15
  42. package/source/internal/keys.d.ts +9 -9
  43. package/source/internal/numeric.d.ts +19 -27
  44. package/source/internal/object.d.ts +44 -6
  45. package/source/internal/string.d.ts +1 -76
  46. package/source/internal/tuple.d.ts +3 -3
  47. package/source/internal/type.d.ts +16 -9
  48. package/source/is-boolean-literal.d.ts +39 -0
  49. package/source/is-equal.d.ts +0 -1
  50. package/source/is-integer.d.ts +8 -8
  51. package/source/is-literal.d.ts +43 -267
  52. package/source/is-numeric-literal.d.ts +50 -0
  53. package/source/is-string-literal.d.ts +72 -0
  54. package/source/is-symbol-literal.d.ts +39 -0
  55. package/source/is-union.d.ts +12 -12
  56. package/source/iterable-element.d.ts +5 -5
  57. package/source/jsonify.d.ts +5 -9
  58. package/source/kebab-case.d.ts +1 -0
  59. package/source/kebab-cased-properties-deep.d.ts +7 -0
  60. package/source/kebab-cased-properties.d.ts +5 -1
  61. package/source/keys-of-union.d.ts +2 -2
  62. package/source/last-array-element.d.ts +66 -13
  63. package/source/less-than-or-equal.d.ts +40 -4
  64. package/source/less-than.d.ts +35 -3
  65. package/source/literal-to-primitive.d.ts +1 -1
  66. package/source/literal-union.d.ts +1 -1
  67. package/source/merge-exclusive.d.ts +3 -3
  68. package/source/merge.d.ts +25 -0
  69. package/source/multidimensional-array.d.ts +1 -1
  70. package/source/multidimensional-readonly-array.d.ts +1 -1
  71. package/source/non-nullable-deep.d.ts +102 -0
  72. package/source/numeric.d.ts +3 -3
  73. package/source/object-merge.d.ts +21 -16
  74. package/source/omit-deep.d.ts +22 -23
  75. package/source/optional.d.ts +31 -0
  76. package/source/or-all.d.ts +73 -0
  77. package/source/or.d.ts +4 -11
  78. package/source/package-json.d.ts +7 -7
  79. package/source/partial-deep.d.ts +3 -1
  80. package/source/pascal-case.d.ts +1 -0
  81. package/source/pascal-cased-properties-deep.d.ts +7 -0
  82. package/source/pascal-cased-properties.d.ts +5 -1
  83. package/source/pick-deep.d.ts +8 -21
  84. package/source/readonly-deep.d.ts +5 -3
  85. package/source/remove-prefix.d.ts +16 -34
  86. package/source/remove-suffix.d.ts +114 -0
  87. package/source/rename-keys.d.ts +163 -0
  88. package/source/replace.d.ts +2 -2
  89. package/source/require-all-or-none.d.ts +5 -5
  90. package/source/require-at-least-one.d.ts +9 -11
  91. package/source/require-exactly-one.d.ts +7 -7
  92. package/source/require-one-or-none.d.ts +5 -5
  93. package/source/required-deep.d.ts +3 -1
  94. package/source/schema.d.ts +16 -8
  95. package/source/screaming-snake-case.d.ts +1 -0
  96. package/source/set-non-nullable-deep.d.ts +8 -4
  97. package/source/set-non-nullable.d.ts +4 -11
  98. package/source/set-optional.d.ts +6 -10
  99. package/source/set-parameter-type.d.ts +2 -2
  100. package/source/set-readonly.d.ts +4 -8
  101. package/source/set-required-deep.d.ts +3 -2
  102. package/source/set-required.d.ts +4 -8
  103. package/source/shared-union-fields-deep.d.ts +5 -4
  104. package/source/shared-union-fields.d.ts +9 -9
  105. package/source/snake-case.d.ts +1 -0
  106. package/source/snake-cased-properties-deep.d.ts +7 -0
  107. package/source/snake-cased-properties.d.ts +5 -1
  108. package/source/some-extend.d.ts +115 -0
  109. package/source/split-on-rest-element.d.ts +6 -4
  110. package/source/split.d.ts +1 -1
  111. package/source/spread.d.ts +1 -5
  112. package/source/string-length.d.ts +38 -0
  113. package/source/string-repeat.d.ts +48 -21
  114. package/source/string-slice.d.ts +1 -1
  115. package/source/string-to-array.d.ts +97 -0
  116. package/source/string-to-number.d.ts +67 -0
  117. package/source/subtract.d.ts +4 -3
  118. package/source/sum.d.ts +5 -4
  119. package/source/tagged.d.ts +5 -7
  120. package/source/tsconfig-json.d.ts +40 -8
  121. package/source/tuple-of.d.ts +42 -8
  122. package/source/typed-array.d.ts +1 -0
  123. package/source/union-length.d.ts +27 -0
  124. package/source/union-member.d.ts +65 -0
  125. package/source/union-to-intersection.d.ts +1 -1
  126. package/source/union-to-tuple.d.ts +10 -19
  127. package/source/unwrap-required.d.ts +37 -0
  128. package/source/words.d.ts +30 -4
  129. package/source/writable.d.ts +15 -19
  130. package/source/xor.d.ts +1 -1
@@ -8,12 +8,10 @@ export type TagContainer<Token> = {
8
8
  type Tag<Token extends PropertyKey, TagMetadata> = TagContainer<{[K in Token]: TagMetadata}>;
9
9
 
10
10
  /**
11
- Attach a "tag" to an arbitrary type. This allows you to create distinct types, that aren't assignable to one another, for distinct concepts in your program that should not be interchangeable, even if their runtime values have the same type. (See examples.)
11
+ Create a [tagged type](https://medium.com/@KevinBGreene/surviving-the-typescript-ecosystem-branding-and-type-tagging-6cf6e516523d) that can support [multiple tags](https://github.com/sindresorhus/type-fest/issues/665) and [per-tag metadata](https://medium.com/@ethanresnick/advanced-typescript-tagged-types-improved-with-type-level-metadata-5072fc125fcf).
12
12
 
13
13
  A type returned by `Tagged` can be passed to `Tagged` again, to create a type with multiple tags.
14
14
 
15
- [Read more about tagged types.](https://medium.com/@KevinBGreene/surviving-the-typescript-ecosystem-branding-and-type-tagging-6cf6e516523d)
16
-
17
15
  A tag's name is usually a string (and must be a string, number, or symbol), but each application of a tag can also contain an arbitrary type as its "metadata". See {@link GetTagMetadata} for examples and explanation.
18
16
 
19
17
  A type `A` returned by `Tagged` is assignable to another type `B` returned by `Tagged` if and only if:
@@ -100,7 +98,7 @@ const parsed = parse(x); // The type of `parsed` is { hello: string }
100
98
  export type GetTagMetadata<Type extends Tag<TagName, unknown>, TagName extends PropertyKey> = Type[typeof tag][TagName];
101
99
 
102
100
  /**
103
- Revert a tagged type back to its original type by removing all tags.
101
+ Get the untagged portion of a tagged type created with `Tagged`.
104
102
 
105
103
  Why is this necessary?
106
104
 
@@ -121,7 +119,7 @@ const moneyByAccountType: Record<UnwrapTagged<AccountType>, number> = {
121
119
  // Without UnwrapTagged, the following expression would throw a type error.
122
120
  const money = moneyByAccountType.SAVINGS; // TS error: Property 'SAVINGS' does not exist
123
121
 
124
- // Attempting to pass an non-Tagged type to UnwrapTagged will raise a type error.
122
+ // Attempting to pass a non-Tagged type to UnwrapTagged will raise a type error.
125
123
  // @ts-expect-error
126
124
  type WontWork = UnwrapTagged<string>;
127
125
  ```
@@ -129,7 +127,7 @@ type WontWork = UnwrapTagged<string>;
129
127
  @category Type
130
128
  */
131
129
  export type UnwrapTagged<TaggedType extends Tag<PropertyKey, any>> =
132
- RemoveAllTags<TaggedType>;
130
+ RemoveAllTags<TaggedType>;
133
131
 
134
132
  type RemoveAllTags<T> = T extends Tag<PropertyKey, any>
135
133
  ? {
@@ -239,7 +237,7 @@ const moneyByAccountType: Record<UnwrapOpaque<AccountType>, number> = {
239
237
  // Without UnwrapOpaque, the following expression would throw a type error.
240
238
  const money = moneyByAccountType.SAVINGS; // TS error: Property 'SAVINGS' does not exist
241
239
 
242
- // Attempting to pass an non-Opaque type to UnwrapOpaque will raise a type error.
240
+ // Attempting to pass a non-Opaque type to UnwrapOpaque will raise a type error.
243
241
  // @ts-expect-error
244
242
  type WontWork = UnwrapOpaque<string>;
245
243
 
@@ -61,6 +61,7 @@ export namespace TsConfigJson {
61
61
  | 'ES2022'
62
62
  | 'ES2023'
63
63
  | 'ES2024'
64
+ | 'ES2025'
64
65
  | 'ESNext'
65
66
  // Lowercase alternatives
66
67
  | 'es3'
@@ -76,6 +77,7 @@ export namespace TsConfigJson {
76
77
  | 'es2022'
77
78
  | 'es2023'
78
79
  | 'es2024'
80
+ | 'es2025'
79
81
  | 'esnext';
80
82
 
81
83
  type Lib =
@@ -148,6 +150,13 @@ export namespace TsConfigJson {
148
150
  | 'ES2024.Regexp'
149
151
  | 'ES2024.SharedMemory'
150
152
  | 'ES2024.String'
153
+ | 'ES2025'
154
+ | 'ES2025.Collection'
155
+ | 'ES2025.Float16'
156
+ | 'ES2025.Intl'
157
+ | 'ES2025.Iterator'
158
+ | 'ES2025.Promise'
159
+ | 'ES2025.RegExp'
151
160
  | 'ESNext'
152
161
  | 'ESNext.Array'
153
162
  | 'ESNext.AsyncIterable'
@@ -156,6 +165,7 @@ export namespace TsConfigJson {
156
165
  | 'ESNext.Decorators'
157
166
  | 'ESNext.Disposable'
158
167
  | 'ESNext.Error'
168
+ | 'ESNext.Float16'
159
169
  | 'ESNext.Intl'
160
170
  | 'ESNext.Iterator'
161
171
  | 'ESNext.Object'
@@ -163,6 +173,7 @@ export namespace TsConfigJson {
163
173
  | 'ESNext.Regexp'
164
174
  | 'ESNext.String'
165
175
  | 'ESNext.Symbol'
176
+ | 'ESNext.Temporal'
166
177
  | 'ESNext.WeakRef'
167
178
  | 'DOM'
168
179
  | 'DOM.AsyncIterable'
@@ -244,6 +255,13 @@ export namespace TsConfigJson {
244
255
  | 'es2024.regexp'
245
256
  | 'es2024.sharedmemory'
246
257
  | 'es2024.string'
258
+ | 'es2025'
259
+ | 'es2025.collection'
260
+ | 'es2025.float16'
261
+ | 'es2025.intl'
262
+ | 'es2025.iterator'
263
+ | 'es2025.promise'
264
+ | 'es2025.regexp'
247
265
  | 'esnext'
248
266
  | 'esnext.array'
249
267
  | 'esnext.asynciterable'
@@ -252,6 +270,7 @@ export namespace TsConfigJson {
252
270
  | 'esnext.decorators'
253
271
  | 'esnext.disposable'
254
272
  | 'esnext.error'
273
+ | 'esnext.float16'
255
274
  | 'esnext.intl'
256
275
  | 'esnext.iterator'
257
276
  | 'esnext.object'
@@ -259,6 +278,7 @@ export namespace TsConfigJson {
259
278
  | 'esnext.regexp'
260
279
  | 'esnext.string'
261
280
  | 'esnext.symbol'
281
+ | 'esnext.temporal'
262
282
  | 'esnext.weakref'
263
283
  | 'dom'
264
284
  | 'dom.asynciterable'
@@ -326,7 +346,7 @@ export namespace TsConfigJson {
326
346
  | 'legacy'
327
347
  | 'force';
328
348
 
329
- type IgnoreDeprecations = '5.0';
349
+ type IgnoreDeprecations = '5.0' | '6.0';
330
350
  }
331
351
 
332
352
  type CompilerOptions = {
@@ -479,7 +499,7 @@ export namespace TsConfigJson {
479
499
  /**
480
500
  Specify module code generation: 'None', 'CommonJS', 'AMD', 'System', 'UMD', 'ES6', 'ES2015' or 'ESNext'. Only 'AMD' and 'System' can be used in conjunction with `--outFile`. 'ES6' and 'ES2015' values may be used when targeting 'ES5' or lower.
481
501
 
482
- @default ['ES3', 'ES5'].includes(target) ? 'CommonJS' : 'ES6'
502
+ Default: `'ESNext'` since TypeScript 6.0, `['ES3', 'ES5'].includes(target) ? 'CommonJS' : 'ES6'` before.
483
503
  */
484
504
  module?: CompilerOptions.Module;
485
505
 
@@ -587,8 +607,17 @@ export namespace TsConfigJson {
587
607
  */
588
608
  skipLibCheck?: boolean;
589
609
 
610
+ /**
611
+ Enforce stable type ordering.
612
+
613
+ @default false
614
+ */
615
+ stableTypeOrdering?: boolean;
616
+
590
617
  /**
591
618
  Concatenate and emit output to single file.
619
+
620
+ @deprecated since TypeScript 6.0.
592
621
  */
593
622
  outFile?: string;
594
623
 
@@ -696,7 +725,7 @@ export namespace TsConfigJson {
696
725
  /**
697
726
  Specify ECMAScript target version.
698
727
 
699
- @default 'es3'
728
+ Default: Current-year ES version since TypeScript 6.0, `'es3'` before.
700
729
  */
701
730
  target?: CompilerOptions.Target;
702
731
 
@@ -776,7 +805,7 @@ export namespace TsConfigJson {
776
805
  /**
777
806
  Report error if failed to find a source file for a side effect import.
778
807
 
779
- @default false
808
+ Default: `true` since TypeScript 6.0, `false` before.
780
809
  */
781
810
  noUncheckedSideEffectImports?: boolean;
782
811
 
@@ -822,6 +851,8 @@ export namespace TsConfigJson {
822
851
 
823
852
  /**
824
853
  Base directory to resolve non-relative module names.
854
+
855
+ @deprecated since TypeScript 6.0.
825
856
  */
826
857
  baseUrl?: string;
827
858
 
@@ -944,7 +975,7 @@ export namespace TsConfigJson {
944
975
  /**
945
976
  Enable all strict type checking options.
946
977
 
947
- @default false
978
+ Default: `true` since TypeScript 6.0, `false` before.
948
979
  */
949
980
  strict?: boolean;
950
981
 
@@ -959,6 +990,7 @@ export namespace TsConfigJson {
959
990
  Provide full support for iterables in `for-of`, spread, and destructuring when targeting `ES5` or `ES3`.
960
991
 
961
992
  @default false
993
+ @deprecated since TypeScript 6.0.
962
994
  */
963
995
  downlevelIteration?: boolean;
964
996
 
@@ -1150,7 +1182,7 @@ export namespace TsConfigJson {
1150
1182
  /**
1151
1183
  Enable lib replacement.
1152
1184
 
1153
- @default true
1185
+ Default: `false` since TypeScript 6.0, `true` before.
1154
1186
  */
1155
1187
  libReplacement?: boolean;
1156
1188
  };
@@ -1234,7 +1266,7 @@ export namespace TsConfigJson {
1234
1266
  exclude?: string[];
1235
1267
 
1236
1268
  /**
1237
- Disable infering what types should be added based on filenames in a project.
1269
+ Disable inferring what types should be added based on filenames in a project.
1238
1270
  */
1239
1271
  disableFilenameBasedTypeAcquisition?: boolean;
1240
1272
  };
@@ -1266,7 +1298,7 @@ export namespace TsConfigJson {
1266
1298
  }
1267
1299
 
1268
1300
  /**
1269
- Type for [TypeScript's `tsconfig.json` file](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) (TypeScript 3.7).
1301
+ Type for [TypeScript's `tsconfig.json` file](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html).
1270
1302
 
1271
1303
  @category File
1272
1304
  */
@@ -1,6 +1,7 @@
1
1
  import type {If} from './if.d.ts';
2
2
  import type {IfNotAnyOrNever} from './internal/type.d.ts';
3
3
  import type {IsNegative} from './numeric.d.ts';
4
+ import type {DigitCharacter} from './characters.d.ts';
4
5
  import type {UnknownArray} from './unknown-array.d.ts';
5
6
 
6
7
  /**
@@ -30,7 +31,7 @@ type ZeroToFour = Range<0, 5>;
30
31
  //=> '0' | '1' | '2' | '3' | '4'
31
32
 
32
33
  type ThreeToEight = Range<3, 9>;
33
- //=> '5' | '3' | '4' | '6' | '7' | '8'
34
+ //=> '3' | '4' | '5' | '6' | '7' | '8'
34
35
  ```
35
36
 
36
37
  Note: If the specified length is the non-literal `number` type, the result will not be a tuple but a regular array.
@@ -63,18 +64,51 @@ type EmptyTuple = TupleOf<-3, string>;
63
64
  //=> []
64
65
  ```
65
66
 
67
+ Note: If the specified length has a decimal part, the decimal part will be ignored.
68
+
69
+ @example
70
+ ```
71
+ import type {TupleOf} from 'type-fest';
72
+
73
+ type DecimalLength = TupleOf<3.5, string>;
74
+ //=> [string, string, string]
75
+ ```
76
+
66
77
  Note: If you need a readonly tuple, simply wrap this type with `Readonly`, for example, to create `readonly [number, number, number]` use `Readonly<TupleOf<3, number>>`.
67
78
 
68
79
  @category Array
69
80
  */
70
- export type TupleOf<Length extends number, Fill = unknown> = IfNotAnyOrNever<Length,
71
- _TupleOf<If<IsNegative<Length>, 0, Length>, Fill, []>,
72
- Fill[], []>;
81
+ export type TupleOf<Length extends number, Fill = unknown> = IfNotAnyOrNever<Length, {
82
+ ifNot: _TupleOf<If<IsNegative<Length>, 0, Length>, Fill>;
83
+ ifAny: Fill[];
84
+ ifNever: [];
85
+ }>;
73
86
 
74
- type _TupleOf<L extends number, Fill, Accumulator extends UnknownArray> = number extends L
87
+ type _TupleOf<Length extends number, Fill> = number extends Length
75
88
  ? Fill[]
76
- : L extends Accumulator['length']
77
- ? Accumulator
78
- : _TupleOf<L, Fill, [...Accumulator, Fill]>;
89
+ : BuildTupleDigitByDigit<`${Length}`, Fill>;
90
+
91
+ type BuildTupleDigitByDigit<Length extends string, Fill, Accumulator extends UnknownArray = []> =
92
+ Length extends `${infer First extends DigitCharacter}${infer Rest}`
93
+ ? BuildTupleDigitByDigit<Rest, Fill, [...RepeatTupleTenTimes<Accumulator>, ...DigitTupleOf<First, Fill>]>
94
+ : Accumulator;
95
+
96
+ type RepeatTupleTenTimes<Tuple extends UnknownArray> = [
97
+ ...Tuple, ...Tuple, ...Tuple, ...Tuple, ...Tuple,
98
+ ...Tuple, ...Tuple, ...Tuple, ...Tuple, ...Tuple,
99
+ ];
100
+
101
+ type DigitTupleOf<Digit extends DigitCharacter, Fill> = [
102
+ [],
103
+ [Fill],
104
+ [Fill, Fill],
105
+ [Fill, Fill, Fill],
106
+ [Fill, Fill, Fill, Fill],
107
+ [Fill, Fill, Fill, Fill, Fill],
108
+ [Fill, Fill, Fill, Fill, Fill, Fill],
109
+ [Fill, Fill, Fill, Fill, Fill, Fill, Fill],
110
+ [Fill, Fill, Fill, Fill, Fill, Fill, Fill, Fill],
111
+ [Fill, Fill, Fill, Fill, Fill, Fill, Fill, Fill, Fill],
112
+ ][Digit];
79
113
 
80
114
  export {};
@@ -11,6 +11,7 @@ export type TypedArray =
11
11
  | Uint16Array
12
12
  | Int32Array
13
13
  | Uint32Array
14
+ | Float16Array
14
15
  | Float32Array
15
16
  | Float64Array
16
17
  | BigInt64Array
@@ -0,0 +1,27 @@
1
+ import type {UnionToTuple} from './union-to-tuple.d.ts';
2
+
3
+ /**
4
+ Returns the length of a union type.
5
+
6
+ @example
7
+ ```
8
+ import type {UnionLength} from 'type-fest';
9
+
10
+ type T1 = UnionLength<'foo' | 'bar' | 'baz'>;
11
+ //=> 3
12
+
13
+ type T2 = UnionLength<[string, string, string] | {x: string; y: string; z: string}>;
14
+ //=> 2
15
+
16
+ type T3 = UnionLength<any>;
17
+ //=> 1
18
+
19
+ type T4 = UnionLength<never>;
20
+ //=> 0
21
+ ```
22
+
23
+ @category Type
24
+ */
25
+ export type UnionLength<Union> = UnionToTuple<Union>['length'];
26
+
27
+ export {};
@@ -0,0 +1,65 @@
1
+ import type {UnionToIntersection} from './union-to-intersection.d.ts';
2
+ import type {IsNever} from './is-never.d.ts';
3
+
4
+ /**
5
+ Returns an arbitrary member of a union type.
6
+
7
+ Use-cases:
8
+ - Implementing recursive type functions that accept a union type.
9
+
10
+ @example
11
+ ```
12
+ import type {UnionMember, IsNever} from 'type-fest';
13
+
14
+ type UnionLength<T, Acc extends any[] = []> =
15
+ UnionMember<T> extends infer Member
16
+ ? IsNever<Member> extends false
17
+ ? UnionLength<Exclude<T, Member>, [...Acc, Member]>
18
+ : Acc['length']
19
+ : never;
20
+
21
+ type T1 = UnionLength<'foo' | 'bar' | 'baz'>;
22
+ //=> 3
23
+
24
+ type T2 = UnionLength<{a: string}>;
25
+ //=> 1
26
+ ```
27
+
28
+ - Picking an arbitrary member from a union
29
+
30
+ @example
31
+ ```
32
+ import type {UnionMember, Primitive, LiteralToPrimitive} from 'type-fest';
33
+
34
+ type IsHomogenous<T extends Primitive> = [T] extends [LiteralToPrimitive<UnionMember<T>>] ? true : false;
35
+
36
+ type T1 = IsHomogenous<1 | 2 | 3 | 4>;
37
+ //=> true
38
+
39
+ type T2 = IsHomogenous<'foo' | 'bar'>;
40
+ //=> true
41
+
42
+ type T3 = IsHomogenous<'foo' | 'bar' | 1>;
43
+ //=> false
44
+ ```
45
+
46
+ Returns `never` when the input is `never`.
47
+
48
+ @example
49
+ ```
50
+ import type {UnionMember} from 'type-fest';
51
+
52
+ type LastNever = UnionMember<never>;
53
+ //=> never
54
+ ```
55
+
56
+ @category Type
57
+ */
58
+ export type UnionMember<T> =
59
+ IsNever<T> extends true
60
+ ? never
61
+ : UnionToIntersection<T extends any ? () => T : never> extends () => (infer R)
62
+ ? R
63
+ : never;
64
+
65
+ export {};
@@ -1,5 +1,5 @@
1
1
  /**
2
- Convert a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
2
+ Convert a union type to an intersection type.
3
3
 
4
4
  Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
5
5
 
@@ -1,19 +1,7 @@
1
+ import type {ExcludeExactly} from './exclude-exactly.d.ts';
1
2
  import type {IsNever} from './is-never.d.ts';
2
- import type {UnionToIntersection} from './union-to-intersection.d.ts';
3
-
4
- /**
5
- Returns the last element of a union type.
6
-
7
- @example
8
- ```
9
- type Last = LastOfUnion<1 | 2 | 3>;
10
- //=> 3
11
- ```
12
- */
13
- type LastOfUnion<T> =
14
- UnionToIntersection<T extends any ? () => T : never> extends () => (infer R)
15
- ? R
16
- : never;
3
+ import type {UnionMember} from './union-member.d.ts';
4
+ import type {UnknownArray} from './unknown-array.d.ts';
17
5
 
18
6
  /**
19
7
  Convert a union type into an unordered tuple type of its elements.
@@ -50,9 +38,12 @@ const petList = Object.keys(pets) as UnionToTuple<Pet>;
50
38
 
51
39
  @category Array
52
40
  */
53
- export type UnionToTuple<T, L = LastOfUnion<T>> =
54
- IsNever<T> extends false
55
- ? [...UnionToTuple<Exclude<T, L>>, L]
56
- : [];
41
+ export type UnionToTuple<Union> =
42
+ _UnionToTuple<Union> extends infer Result extends UnknownArray ? Result : never; // Nudges the compiler that `UnionToTuple` always yields an array.
43
+
44
+ type _UnionToTuple<Union, Accumulator extends UnknownArray = [], Member = UnionMember<Union>> =
45
+ IsNever<Union> extends true
46
+ ? Accumulator
47
+ : _UnionToTuple<ExcludeExactly<Union, Member>, [Member, ...Accumulator]>;
57
48
 
58
49
  export {};
@@ -0,0 +1,37 @@
1
+ import type {MapsSetsOrArrays, NonRecursiveType} from './internal/type.d.ts';
2
+
3
+ /**
4
+ Revert the `Required` modifier on an object type.
5
+
6
+ Use-case: Infer the underlying type `T` when only `Required<T>` is available or the original type may not be directly accessible.
7
+
8
+ @example
9
+ ```
10
+ import type {UnwrapRequired} from 'type-fest';
11
+
12
+ type ContactFormData = Required<{
13
+ email: string;
14
+ message?: string;
15
+ }>;
16
+
17
+ type DraftContactFormData = UnwrapRequired<ContactFormData>;
18
+ //=> {email: string; message?: string}
19
+ ```
20
+
21
+ Note:
22
+ - If the provided type isn’t of the form `Required<T>`, `UnwrapRequired` simply returns the input type.
23
+ - `UnwrapRequired` doesn't work with arrays, if instantiated with arrays, it simply returns the input type.
24
+
25
+ @category Object
26
+ */
27
+ export type UnwrapRequired<RequiredObjectType> =
28
+ RequiredObjectType extends NonRecursiveType | MapsSetsOrArrays
29
+ ? RequiredObjectType
30
+ : _UnwrapRequired<RequiredObjectType>;
31
+
32
+ type _UnwrapRequired<RequiredObjectType> =
33
+ RequiredObjectType extends Required<infer ObjectType>
34
+ ? ObjectType
35
+ : RequiredObjectType;
36
+
37
+ export {};
package/source/words.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type {
2
2
  ApplyDefaultOptions,
3
+ AsciiPunctuation,
3
4
  IsNumeric,
4
5
  WordSeparators,
5
6
  } from './internal/index.d.ts';
@@ -38,17 +39,34 @@ export type WordsOptions = {
38
39
  ```
39
40
  */
40
41
  splitOnNumbers?: boolean;
42
+ /**
43
+ Split on {@link AsciiPunctuation | punctuation characters} (e.g., `#`, `&`, `*`, `:`, `?`, `@`, `~`).
44
+
45
+ @example
46
+ ```
47
+ import type {Words} from 'type-fest';
48
+
49
+ type Example1 = Words<'hello:world', {splitOnPunctuation: true}>;
50
+ //=> ['hello', 'world']
51
+
52
+ type Example2 = Words<'hello:world', {splitOnPunctuation: false}>;
53
+ //=> ['hello', ':world']
54
+ ```
55
+ */
56
+ splitOnPunctuation?: boolean;
41
57
  };
42
58
 
43
59
  export type _DefaultWordsOptions = {
44
60
  splitOnNumbers: true;
61
+ splitOnPunctuation: false;
45
62
  };
46
63
 
47
64
  /**
48
- Split a string (almost) like Lodash's `_.words()` function.
65
+ Split a string similar to Lodash's `_.words()` function.
49
66
 
50
67
  - Split on each word that begins with a capital letter.
51
68
  - Split on each {@link WordSeparators}.
69
+ - Split on each {@link AsciiPunctuation} (if {@link WordsOptions.splitOnPunctuation} is enabled).
52
70
  - Split on numeric sequence.
53
71
 
54
72
  @example
@@ -72,13 +90,21 @@ type Words4 = Words<'lifeIs42'>;
72
90
 
73
91
  type Words5 = Words<'p2pNetwork', {splitOnNumbers: false}>;
74
92
  //=> ['p2p', 'Network']
93
+
94
+ type Words6 = Words<'hello:world', {splitOnPunctuation: true}>;
95
+ //=> ['hello', 'world']
96
+
97
+ type Words7 = Words<'hello:world', {splitOnPunctuation: false}>;
98
+ //=> ['hello', ':world']
99
+
100
+ type Words8 = Words<'hello::world', {splitOnPunctuation: true}>;
101
+ //=> ['hello', 'world']
75
102
  ```
76
103
 
77
104
  @category Change case
78
105
  @category Template literal
79
106
  */
80
- export type Words<Sentence extends string, Options extends WordsOptions = {}> =
81
- WordsImplementation<Sentence, ApplyDefaultOptions<WordsOptions, _DefaultWordsOptions, Options>>;
107
+ export type Words<Sentence extends string, Options extends WordsOptions = {}> = WordsImplementation<Sentence, ApplyDefaultOptions<WordsOptions, _DefaultWordsOptions, Options>>;
82
108
 
83
109
  type WordsImplementation<
84
110
  Sentence extends string,
@@ -86,7 +112,7 @@ type WordsImplementation<
86
112
  LastCharacter extends string = '',
87
113
  CurrentWord extends string = '',
88
114
  > = Sentence extends `${infer FirstCharacter}${infer RemainingCharacters}`
89
- ? FirstCharacter extends WordSeparators
115
+ ? FirstCharacter extends WordSeparators | (Options['splitOnPunctuation'] extends true ? AsciiPunctuation : never)
90
116
  // Skip word separator
91
117
  ? [...SkipEmptyWord<CurrentWord>, ...WordsImplementation<RemainingCharacters, Options>]
92
118
  : LastCharacter extends ''
@@ -37,11 +37,7 @@ writableFoo.b[0] = 'new value'; // Will still fail as the value of property "b"
37
37
  writableFoo.b = ['something']; // Will work as the "b" property itself is no longer readonly.
38
38
 
39
39
  type SomeWritable = Writable<Foo, 'b' | 'c'>;
40
- // type SomeWritable = {
41
- // readonly a: number;
42
- // b: readonly string[]; // It's now writable. The type of the property remains unaffected.
43
- // c: boolean; // It's now writable.
44
- // }
40
+ //=> {readonly a: number; b: readonly string[]; c: boolean}
45
41
 
46
42
  // Also supports array
47
43
  const readonlyArray: readonly number[] = [1, 2, 3];
@@ -54,19 +50,19 @@ writableArray.push(4); // Will work as the array itself is now writable.
54
50
  @category Object
55
51
  */
56
52
  export type Writable<BaseType, Keys extends keyof BaseType = keyof BaseType> =
57
- BaseType extends ReadonlyMap<infer KeyType, infer ValueType>
58
- ? Map<KeyType, ValueType>
59
- : BaseType extends ReadonlySet<infer ItemType>
60
- ? Set<ItemType>
61
- : BaseType extends readonly unknown[]
62
- // Handle array
63
- ? WritableArray<BaseType>
64
- // Handle object
65
- : Simplify<
66
- // Pick just the keys that are not writable from the base type.
67
- Except<BaseType, Keys> &
68
- // Pick the keys that should be writable from the base type and make them writable by removing the `readonly` modifier from the key.
69
- {-readonly [KeyType in keyof Pick<BaseType, Keys>]: Pick<BaseType, Keys>[KeyType]}
70
- >;
53
+ BaseType extends ReadonlyMap<infer KeyType, infer ValueType>
54
+ ? Map<KeyType, ValueType>
55
+ : BaseType extends ReadonlySet<infer ItemType>
56
+ ? Set<ItemType>
57
+ : BaseType extends readonly unknown[]
58
+ // Handle array
59
+ ? WritableArray<BaseType>
60
+ // Handle object
61
+ : Simplify<
62
+ // Pick just the keys that are not writable from the base type.
63
+ Except<BaseType, Keys>
64
+ // Make the specified keys writable.
65
+ & {-readonly [KeyType in keyof BaseType as KeyType extends Keys ? KeyType : never]: BaseType[KeyType]}
66
+ >;
71
67
 
72
68
  export {};
package/source/xor.d.ts CHANGED
@@ -3,7 +3,7 @@ import type {And} from './and.d.ts';
3
3
  import type {Or} from './or.d.ts';
4
4
 
5
5
  /**
6
- Returns a boolean for whether only one of two given types is true.
6
+ Returns a boolean for whether only one of two given types is `true`.
7
7
 
8
8
  Use-case: Constructing complex conditional types where one single condition must be satisfied.
9
9