type-fest 5.4.2 → 5.4.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "5.4.2",
3
+ "version": "5.4.3",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
@@ -27,7 +27,7 @@
27
27
  "scripts": {
28
28
  "test:tsc": "node --max-old-space-size=6144 ./node_modules/.bin/tsc",
29
29
  "test:tsd": "node --max-old-space-size=6144 ./node_modules/.bin/tsd",
30
- "test:xo": "node --max-old-space-size=6144 ./node_modules/.bin/xo",
30
+ "test:xo": "node --max-old-space-size=6144 ./node_modules/.bin/xo --ignores=lint-processors/fixtures/**/*.d.ts",
31
31
  "test:linter": "node --test",
32
32
  "test": "run-p test:*"
33
33
  },
@@ -2,7 +2,7 @@ import type {IsNever} from '../is-never.d.ts';
2
2
  import type {Finite, NegativeInfinity, PositiveInfinity} from '../numeric.d.ts';
3
3
  import type {UnknownArray} from '../unknown-array.d.ts';
4
4
  import type {StringToNumber} from './string.d.ts';
5
- import type {IsAnyOrNever} from './type.d.ts';
5
+ import type {IfNotAnyOrNever, IsAnyOrNever} from './type.d.ts';
6
6
 
7
7
  /**
8
8
  Returns the absolute value of a given value.
@@ -44,10 +44,11 @@ type E = IsNumberLike<'a'>;
44
44
  //=> false
45
45
  */
46
46
  export type IsNumberLike<N> =
47
- IsAnyOrNever<N> extends true ? N
48
- : N extends number | `${number}`
47
+ IfNotAnyOrNever<N,
48
+ N extends number | `${number}`
49
49
  ? true
50
- : false;
50
+ : false,
51
+ boolean, false>;
51
52
 
52
53
  /**
53
54
  Returns the minimum number in the given union of numbers.
package/source/merge.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import type {OmitIndexSignature} from './omit-index-signature.d.ts';
2
2
  import type {PickIndexSignature} from './pick-index-signature.d.ts';
3
3
  import type {Simplify} from './simplify.d.ts';
4
+ import type {If} from './if.d.ts';
5
+ import type {IsEqual} from './is-equal.d.ts';
4
6
 
5
7
  // Merges two objects without worrying about index signatures.
6
8
  type SimpleMerge<Destination, Source> = Simplify<{
@@ -47,11 +49,14 @@ Note: If you want a merge type that more accurately reflects the runtime behavio
47
49
  export type Merge<Destination, Source> =
48
50
  Destination extends unknown // For distributing `Destination`
49
51
  ? Source extends unknown // For distributing `Source`
50
- ? Simplify<
51
- SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
52
- & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
53
- >
52
+ ? If<IsEqual<Destination, Source>, Destination, _Merge<Destination, Source>>
54
53
  : never // Should never happen
55
54
  : never; // Should never happen
56
55
 
56
+ export type _Merge<Destination, Source> =
57
+ Simplify<
58
+ SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
59
+ & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
60
+ >;
61
+
57
62
  export {};
package/source/paths.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import type {StaticPartOfArray, VariablePartOfArray, NonRecursiveType, ToString, IsNumberLike, ApplyDefaultOptions} from './internal/index.d.ts';
2
2
  import type {IsAny} from './is-any.d.ts';
3
3
  import type {UnknownArray} from './unknown-array.d.ts';
4
- import type {Subtract} from './subtract.d.ts';
5
4
  import type {GreaterThan} from './greater-than.d.ts';
6
5
  import type {IsNever} from './is-never.d.ts';
6
+ import type {Sum} from './sum.d.ts';
7
+ import type {And} from './and.d.ts';
7
8
 
8
9
  /**
9
10
  Paths options.
@@ -190,7 +191,7 @@ open('listB.1'); // TypeError. Because listB only has one element.
190
191
  */
191
192
  export type Paths<T, Options extends PathsOptions = {}> = _Paths<T, ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, Options>>;
192
193
 
193
- type _Paths<T, Options extends Required<PathsOptions>> =
194
+ type _Paths<T, Options extends Required<PathsOptions>, CurrentDepth extends number = 0> =
194
195
  T extends NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
195
196
  ? never
196
197
  : IsAny<T> extends true
@@ -198,13 +199,13 @@ type _Paths<T, Options extends Required<PathsOptions>> =
198
199
  : T extends UnknownArray
199
200
  ? number extends T['length']
200
201
  // We need to handle the fixed and non-fixed index part of the array separately.
201
- ? InternalPaths<StaticPartOfArray<T>, Options> | InternalPaths<Array<VariablePartOfArray<T>[number]>, Options>
202
- : InternalPaths<T, Options>
202
+ ? InternalPaths<StaticPartOfArray<T>, Options, CurrentDepth> | InternalPaths<Array<VariablePartOfArray<T>[number]>, Options, CurrentDepth>
203
+ : InternalPaths<T, Options, CurrentDepth>
203
204
  : T extends object
204
- ? InternalPaths<T, Options>
205
+ ? InternalPaths<T, Options, CurrentDepth>
205
206
  : never;
206
207
 
207
- type InternalPaths<T, Options extends Required<PathsOptions>> =
208
+ type InternalPaths<T, Options extends Required<PathsOptions>, CurrentDepth extends number> =
208
209
  Options['maxRecursionDepth'] extends infer MaxDepth extends number
209
210
  ? Required<T> extends infer T
210
211
  ? T extends readonly []
@@ -215,19 +216,17 @@ type InternalPaths<T, Options extends Required<PathsOptions>> =
215
216
  [Key in keyof T]:
216
217
  Key extends string | number // Limit `Key` to string or number.
217
218
  ? (
218
- Options['bracketNotation'] extends true
219
- ? IsNumberLike<Key> extends true
220
- ? `[${Key}]`
221
- : (Key | ToString<Key>)
222
- : Options['bracketNotation'] extends false
219
+ And<Options['bracketNotation'], IsNumberLike<Key>> extends true
220
+ ? `[${Key}]`
223
221
  // If `Key` is a number, return `Key | `${Key}``, because both `array[0]` and `array['0']` work.
224
- ? (Key | ToString<Key>)
225
- : never
222
+ : CurrentDepth extends 0
223
+ ? Key | ToString<Key>
224
+ : `.${(Key | ToString<Key>)}`
226
225
  ) extends infer TranformedKey extends string | number ?
227
226
  // 1. If style is 'a[0].b' and 'Key' is a numberlike value like 3 or '3', transform 'Key' to `[${Key}]`, else to `${Key}` | Key
228
227
  // 2. If style is 'a.0.b', transform 'Key' to `${Key}` | Key
229
228
  | ((Options['leavesOnly'] extends true
230
- ? MaxDepth extends 0
229
+ ? MaxDepth extends CurrentDepth
231
230
  ? TranformedKey
232
231
  : T[Key] extends infer Value
233
232
  ? (Value extends readonly [] | NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
@@ -238,36 +237,16 @@ type InternalPaths<T, Options extends Required<PathsOptions>> =
238
237
  : never
239
238
  : TranformedKey
240
239
  ) extends infer _TransformedKey
241
- // If `depth` is provided, the condition becomes truthy only when it reaches `0`.
240
+ // If `depth` is provided, the condition becomes truthy only when it reaches `CurrentDepth`.
242
241
  // Otherwise, since `depth` defaults to `number`, the condition is always truthy, returning paths at all depths.
243
- ? 0 extends Options['depth']
242
+ ? CurrentDepth extends Options['depth']
244
243
  ? _TransformedKey
245
244
  : never
246
245
  : never)
247
246
  | (
248
247
  // Recursively generate paths for the current key
249
- GreaterThan<MaxDepth, 0> extends true // Limit the depth to prevent infinite recursion
250
- ? _Paths<T[Key],
251
- {
252
- bracketNotation: Options['bracketNotation'];
253
- maxRecursionDepth: Subtract<MaxDepth, 1>;
254
- leavesOnly: Options['leavesOnly'];
255
- depth: Subtract<Options['depth'], 1>;
256
- }> extends infer SubPath
257
- ? SubPath extends string | number
258
- ? (
259
- Options['bracketNotation'] extends true
260
- ? SubPath extends `[${any}]` | `[${any}]${string}`
261
- ? `${TranformedKey}${SubPath}` // If next node is number key like `[3]`, no need to add `.` before it.
262
- : `${TranformedKey}.${SubPath}`
263
- : never
264
- ) | (
265
- Options['bracketNotation'] extends false
266
- ? `${TranformedKey}.${SubPath}`
267
- : never
268
- )
269
- : never
270
- : never
248
+ GreaterThan<MaxDepth, CurrentDepth> extends true // Limit the depth to prevent infinite recursion
249
+ ? `${TranformedKey}${_Paths<T[Key], Options, Sum<CurrentDepth, 1>> & (string | number)}`
271
250
  : never
272
251
  )
273
252
  : never