type-fest 5.4.4 → 5.6.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 (100) hide show
  1. package/index.d.ts +10 -0
  2. package/package.json +6 -4
  3. package/readme.md +83 -58
  4. package/source/absolute.d.ts +52 -0
  5. package/source/all-extend.d.ts +5 -6
  6. package/source/all-union-fields.d.ts +18 -18
  7. package/source/and-all.d.ts +76 -0
  8. package/source/and.d.ts +4 -3
  9. package/source/array-length.d.ts +36 -0
  10. package/source/array-splice.d.ts +26 -26
  11. package/source/camel-case.d.ts +38 -5
  12. package/source/camel-cased-properties-deep.d.ts +11 -4
  13. package/source/camel-cased-properties.d.ts +5 -1
  14. package/source/conditional-pick-deep.d.ts +5 -3
  15. package/source/conditional-pick.d.ts +6 -4
  16. package/source/delimiter-case.d.ts +1 -0
  17. package/source/delimiter-cased-properties-deep.d.ts +8 -1
  18. package/source/delimiter-cased-properties.d.ts +5 -1
  19. package/source/empty-object.d.ts +1 -1
  20. package/source/entries.d.ts +1 -1
  21. package/source/entry.d.ts +1 -1
  22. package/source/exclude-exactly.d.ts +57 -0
  23. package/source/get.d.ts +1 -1
  24. package/source/greater-than-or-equal.d.ts +34 -1
  25. package/source/greater-than.d.ts +37 -3
  26. package/source/has-optional-keys.d.ts +1 -1
  27. package/source/has-readonly-keys.d.ts +1 -1
  28. package/source/has-required-keys.d.ts +1 -1
  29. package/source/has-writable-keys.d.ts +1 -1
  30. package/source/int-closed-range.d.ts +1 -3
  31. package/source/int-range.d.ts +3 -5
  32. package/source/internal/array.d.ts +7 -14
  33. package/source/internal/keys.d.ts +9 -9
  34. package/source/internal/numeric.d.ts +13 -23
  35. package/source/internal/tuple.d.ts +3 -3
  36. package/source/internal/type.d.ts +1 -0
  37. package/source/is-equal.d.ts +0 -1
  38. package/source/is-integer.d.ts +8 -8
  39. package/source/is-literal.d.ts +5 -5
  40. package/source/is-union.d.ts +12 -12
  41. package/source/iterable-element.d.ts +5 -5
  42. package/source/jsonify.d.ts +4 -4
  43. package/source/kebab-case.d.ts +1 -0
  44. package/source/kebab-cased-properties-deep.d.ts +7 -0
  45. package/source/kebab-cased-properties.d.ts +5 -1
  46. package/source/keys-of-union.d.ts +2 -2
  47. package/source/less-than-or-equal.d.ts +40 -4
  48. package/source/less-than.d.ts +35 -3
  49. package/source/literal-to-primitive.d.ts +1 -1
  50. package/source/literal-union.d.ts +1 -1
  51. package/source/merge-exclusive.d.ts +3 -3
  52. package/source/merge.d.ts +25 -0
  53. package/source/multidimensional-array.d.ts +1 -1
  54. package/source/multidimensional-readonly-array.d.ts +1 -1
  55. package/source/non-nullable-deep.d.ts +102 -0
  56. package/source/numeric.d.ts +3 -3
  57. package/source/omit-deep.d.ts +22 -23
  58. package/source/optional.d.ts +31 -0
  59. package/source/or-all.d.ts +73 -0
  60. package/source/or.d.ts +4 -11
  61. package/source/package-json.d.ts +7 -7
  62. package/source/partial-deep.d.ts +3 -1
  63. package/source/pascal-case.d.ts +1 -0
  64. package/source/pascal-cased-properties-deep.d.ts +7 -0
  65. package/source/pascal-cased-properties.d.ts +5 -1
  66. package/source/pick-deep.d.ts +8 -21
  67. package/source/readonly-deep.d.ts +5 -3
  68. package/source/remove-prefix.d.ts +15 -15
  69. package/source/replace.d.ts +2 -2
  70. package/source/require-all-or-none.d.ts +1 -1
  71. package/source/require-at-least-one.d.ts +5 -7
  72. package/source/require-exactly-one.d.ts +3 -3
  73. package/source/require-one-or-none.d.ts +1 -1
  74. package/source/required-deep.d.ts +3 -1
  75. package/source/screaming-snake-case.d.ts +1 -0
  76. package/source/set-non-nullable-deep.d.ts +6 -3
  77. package/source/set-non-nullable.d.ts +4 -11
  78. package/source/set-optional.d.ts +6 -10
  79. package/source/set-parameter-type.d.ts +2 -2
  80. package/source/set-readonly.d.ts +4 -8
  81. package/source/set-required-deep.d.ts +1 -1
  82. package/source/set-required.d.ts +4 -8
  83. package/source/shared-union-fields-deep.d.ts +1 -1
  84. package/source/shared-union-fields.d.ts +9 -9
  85. package/source/snake-case.d.ts +1 -0
  86. package/source/snake-cased-properties-deep.d.ts +7 -0
  87. package/source/snake-cased-properties.d.ts +5 -1
  88. package/source/some-extend.d.ts +113 -0
  89. package/source/spread.d.ts +1 -5
  90. package/source/subtract.d.ts +4 -3
  91. package/source/sum.d.ts +5 -4
  92. package/source/tagged.d.ts +5 -7
  93. package/source/tsconfig-json.d.ts +39 -7
  94. package/source/union-length.d.ts +27 -0
  95. package/source/union-member.d.ts +65 -0
  96. package/source/union-to-intersection.d.ts +1 -1
  97. package/source/union-to-tuple.d.ts +10 -19
  98. package/source/words.d.ts +30 -4
  99. package/source/writable.d.ts +15 -19
  100. package/source/xor.d.ts +1 -1
@@ -0,0 +1,36 @@
1
+ /**
2
+ Return the length of an array. Equivalent to `T['length']` where `T` extends any array.
3
+
4
+ Tuples resolve to numeric literals, while non-tuples resolve to the `number` type.
5
+
6
+ @example
7
+ ```
8
+ import type {ArrayLength} from 'type-fest';
9
+
10
+ type TupleLength = ArrayLength<[1, 2, 3]>;
11
+ //=> 3
12
+
13
+ type TupleWithOptionalMembersLength = ArrayLength<[1, 2, number?]>;
14
+ //=> 2 | 3
15
+
16
+ type NonTupleArrayLength = ArrayLength<string[]>;
17
+ //=> number
18
+
19
+ type TupleWithRestElementLength = ArrayLength<[1, 2, ...string[]]>;
20
+ //=> number
21
+
22
+ // Distinguish between arrays with fixed and non-fixed lengths
23
+ type IsFixedLengthArray<T extends readonly unknown[]> = number extends ArrayLength<T> ? false : true;
24
+
25
+ type A = IsFixedLengthArray<number[]>;
26
+ //=> false
27
+
28
+ type B = IsFixedLengthArray<[1, 2, 3]>;
29
+ //=> true
30
+ ```
31
+
32
+ @category Array
33
+ */
34
+ export type ArrayLength<T extends readonly unknown[]> = T['length'];
35
+
36
+ export {};
@@ -8,13 +8,13 @@ import type {TupleOf} from './tuple-of.d.ts';
8
8
  The implementation of `SplitArrayByIndex` for fixed length arrays.
9
9
  */
10
10
  type SplitFixedArrayByIndex<T extends UnknownArray, SplitIndex extends number> =
11
- SplitIndex extends 0
12
- ? [[], T]
13
- : T extends readonly [...TupleOf<SplitIndex>, ...infer V]
14
- ? T extends readonly [...infer U, ...V]
15
- ? [U, V]
16
- : [never, never]
17
- : [never, never];
11
+ SplitIndex extends 0
12
+ ? [[], T]
13
+ : T extends readonly [...TupleOf<SplitIndex>, ...infer V]
14
+ ? T extends readonly [...infer U, ...V]
15
+ ? [U, V]
16
+ : [never, never]
17
+ : [never, never];
18
18
 
19
19
  /**
20
20
  The implementation of `SplitArrayByIndex` for variable length arrays.
@@ -26,23 +26,23 @@ type SplitVariableArrayByIndex<T extends UnknownArray,
26
26
  ? TupleOf<GreaterThanOrEqual<T1, 0> extends true ? T1 : number, VariablePartOfArray<T>[number]>
27
27
  : [],
28
28
  > =
29
- SplitIndex extends 0
30
- ? [[], T]
31
- : GreaterThanOrEqual<StaticPartOfArray<T>['length'], SplitIndex> extends true
32
- ? [
33
- SplitFixedArrayByIndex<StaticPartOfArray<T>, SplitIndex>[0],
34
- [
35
- ...SplitFixedArrayByIndex<StaticPartOfArray<T>, SplitIndex>[1],
36
- ...VariablePartOfArray<T>,
37
- ],
38
- ]
39
- : [
40
- [
41
- ...StaticPartOfArray<T>,
42
- ...(T2 extends UnknownArray ? T2 : []),
43
- ],
44
- VariablePartOfArray<T>,
45
- ];
29
+ SplitIndex extends 0
30
+ ? [[], T]
31
+ : GreaterThanOrEqual<StaticPartOfArray<T>['length'], SplitIndex> extends true
32
+ ? [
33
+ SplitFixedArrayByIndex<StaticPartOfArray<T>, SplitIndex>[0],
34
+ [
35
+ ...SplitFixedArrayByIndex<StaticPartOfArray<T>, SplitIndex>[1],
36
+ ...VariablePartOfArray<T>,
37
+ ],
38
+ ]
39
+ : [
40
+ [
41
+ ...StaticPartOfArray<T>,
42
+ ...(T2 extends UnknownArray ? T2 : []),
43
+ ],
44
+ VariablePartOfArray<T>,
45
+ ];
46
46
 
47
47
  /**
48
48
  Split the given array `T` by the given `SplitIndex`.
@@ -50,10 +50,10 @@ Split the given array `T` by the given `SplitIndex`.
50
50
  @example
51
51
  ```
52
52
  type A = SplitArrayByIndex<[1, 2, 3, 4], 2>;
53
- // type A = [[1, 2], [3, 4]];
53
+ //=> [[1, 2], [3, 4]];
54
54
 
55
55
  type B = SplitArrayByIndex<[1, 2, 3, 4], 0>;
56
- // type B = [[], [1, 2, 3, 4]];
56
+ //=> [[], [1, 2, 3, 4]];
57
57
  ```
58
58
  */
59
59
  type SplitArrayByIndex<T extends UnknownArray, SplitIndex extends number> =
@@ -1,5 +1,5 @@
1
1
  import type {ApplyDefaultOptions} from './internal/index.d.ts';
2
- import type {Words, WordsOptions} from './words.d.ts';
2
+ import type {_DefaultWordsOptions, Words, WordsOptions} from './words.d.ts';
3
3
 
4
4
  /**
5
5
  CamelCase options.
@@ -13,13 +13,39 @@ export type CamelCaseOptions = WordsOptions & {
13
13
  @default false
14
14
  */
15
15
  preserveConsecutiveUppercase?: boolean;
16
+
17
+ /**
18
+ Whether to preserve leading underscores.
19
+
20
+ This matches the behavior of the [`camelcase`](https://github.com/sindresorhus/camelcase) package v9+.
21
+
22
+ @default false
23
+ */
24
+ preserveLeadingUnderscores?: boolean;
16
25
  };
17
26
 
18
- export type _DefaultCamelCaseOptions = {
19
- splitOnNumbers: true;
27
+ export type _DefaultCamelCaseOptions = _DefaultWordsOptions & {
20
28
  preserveConsecutiveUppercase: false;
29
+ preserveLeadingUnderscores: false;
21
30
  };
22
31
 
32
+ /**
33
+ Extract leading underscores from a string.
34
+
35
+ @example
36
+ ```
37
+ type A = LeadingUnderscores<'__foo_bar'>;
38
+ //=> '__'
39
+
40
+ type B = LeadingUnderscores<'foo_bar'>;
41
+ //=> ''
42
+ ```
43
+ */
44
+ type LeadingUnderscores<Type extends string, Underscores extends string = ''> =
45
+ Type extends `_${infer Rest}`
46
+ ? LeadingUnderscores<Rest, `_${Underscores}`>
47
+ : Underscores;
48
+
23
49
  /**
24
50
  Convert an array of words to camel-case.
25
51
  */
@@ -43,6 +69,8 @@ This can be useful when, for example, converting some kebab-cased command-line f
43
69
 
44
70
  By default, consecutive uppercase letter are preserved. See {@link CamelCaseOptions.preserveConsecutiveUppercase preserveConsecutiveUppercase} option to change this behaviour.
45
71
 
72
+ Use the `preserveLeadingUnderscores` option to retain leading underscores, matching the runtime behavior of [`camelcase`](https://github.com/sindresorhus/camelcase) v9+.
73
+
46
74
  @example
47
75
  ```
48
76
  import type {CamelCase} from 'type-fest';
@@ -51,6 +79,8 @@ import type {CamelCase} from 'type-fest';
51
79
 
52
80
  const someVariable: CamelCase<'foo-bar'> = 'fooBar';
53
81
  const preserveConsecutiveUppercase: CamelCase<'foo-BAR-baz', {preserveConsecutiveUppercase: true}> = 'fooBARBaz';
82
+ const splitOnPunctuation: CamelCase<'foo-bar:BAZ', {splitOnPunctuation: true}> = 'fooBarBaz';
83
+ const preserveLeadingUnderscores: CamelCase<'_foo_bar', {preserveLeadingUnderscores: true}> = '_fooBar';
54
84
 
55
85
  // Advanced
56
86
 
@@ -83,10 +113,13 @@ const dbResult: CamelCasedProperties<RawOptions> = {
83
113
  export type CamelCase<Type, Options extends CamelCaseOptions = {}> = Type extends string
84
114
  ? string extends Type
85
115
  ? Type
86
- : Uncapitalize<CamelCaseFromArray<
116
+ : `${Options['preserveLeadingUnderscores'] extends true
117
+ ? LeadingUnderscores<Type>
118
+ : ''
119
+ }${Uncapitalize<CamelCaseFromArray<
87
120
  Words<Type extends Uppercase<Type> ? Lowercase<Type> : Type, Options>,
88
121
  ApplyDefaultOptions<CamelCaseOptions, _DefaultCamelCaseOptions, Options>
89
- >>
122
+ >>}`
90
123
  : Type;
91
124
 
92
125
  export {};
@@ -48,6 +48,13 @@ const preserveConsecutiveUppercase: CamelCasedPropertiesDeep<{fooBAR: {fooBARBiz
48
48
  }],
49
49
  },
50
50
  };
51
+
52
+ const splitOnPunctuation: CamelCasedPropertiesDeep<{'user@info': {'user::id': number; 'user::name': string}}, {splitOnPunctuation: true}> = {
53
+ userInfo: {
54
+ userId: 1,
55
+ userName: 'Tom',
56
+ },
57
+ };
51
58
  ```
52
59
 
53
60
  @category Change case
@@ -86,11 +93,11 @@ type CamelCasedPropertiesArrayDeep<
86
93
  ? [_CamelCasedPropertiesDeep<U, Options>, ..._CamelCasedPropertiesDeep<V, Options>]
87
94
  : Value extends readonly [infer U, ...infer V]
88
95
  ? readonly [_CamelCasedPropertiesDeep<U, Options>, ..._CamelCasedPropertiesDeep<V, Options>]
89
- : // Leading spread array
90
- Value extends readonly [...infer U, infer V]
96
+ // Leading spread array
97
+ : Value extends readonly [...infer U, infer V]
91
98
  ? [..._CamelCasedPropertiesDeep<U, Options>, _CamelCasedPropertiesDeep<V, Options>]
92
- : // Array
93
- Value extends Array<infer U>
99
+ // Array
100
+ : Value extends Array<infer U>
94
101
  ? Array<_CamelCasedPropertiesDeep<U, Options>>
95
102
  : Value extends ReadonlyArray<infer U>
96
103
  ? ReadonlyArray<_CamelCasedPropertiesDeep<U, Options>>
@@ -2,7 +2,7 @@ import type {CamelCase, CamelCaseOptions, _DefaultCamelCaseOptions} from './came
2
2
  import type {ApplyDefaultOptions} from './internal/index.d.ts';
3
3
 
4
4
  /**
5
- Convert object properties to camel case but not recursively.
5
+ Convert top-level object properties to camel case.
6
6
 
7
7
  This can be useful when, for example, converting some API types from a different style.
8
8
 
@@ -26,6 +26,10 @@ const result: CamelCasedProperties<User> = {
26
26
  const preserveConsecutiveUppercase: CamelCasedProperties<{fooBAR: string}, {preserveConsecutiveUppercase: true}> = {
27
27
  fooBAR: 'string',
28
28
  };
29
+
30
+ const splitOnPunctuation: CamelCasedProperties<{'foo::bar': string}, {splitOnPunctuation: true}> = {
31
+ fooBar: 'string',
32
+ };
29
33
  ```
30
34
 
31
35
  @category Change case
@@ -74,7 +74,7 @@ type BooleanPick = ConditionalPickDeep<Example, boolean | undefined>;
74
74
  //=> {c: {e: {g?: boolean}; j: boolean}}
75
75
 
76
76
  type NumberPick = ConditionalPickDeep<Example, number>;
77
- //=> {}
77
+ //=> never
78
78
 
79
79
  type StringOrBooleanPick = ConditionalPickDeep<Example, string | boolean>;
80
80
  //=> {
@@ -99,11 +99,13 @@ export type ConditionalPickDeep<
99
99
  Type,
100
100
  Condition,
101
101
  Options extends ConditionalPickDeepOptions = {},
102
- > = _ConditionalPickDeep<
102
+ > = _NeverIfEmpty<_ConditionalPickDeep<
103
103
  Type,
104
104
  Condition,
105
105
  ApplyDefaultOptions<ConditionalPickDeepOptions, DefaultConditionalPickDeepOptions, Options>
106
- >;
106
+ >>;
107
+
108
+ type _NeverIfEmpty<Type> = Type extends EmptyObject ? never : Type;
107
109
 
108
110
  type _ConditionalPickDeep<
109
111
  Type,
@@ -1,4 +1,5 @@
1
1
  import type {ConditionalKeys} from './conditional-keys.d.ts';
2
+ import type {IsNever} from './is-never.d.ts';
2
3
 
3
4
  /**
4
5
  Pick keys from the shape that matches the given `Condition`.
@@ -38,9 +39,10 @@ type StringKeysOnly = ConditionalPick<Example, string>;
38
39
 
39
40
  @category Object
40
41
  */
41
- export type ConditionalPick<Base, Condition> = Pick<
42
- Base,
43
- ConditionalKeys<Base, Condition>
44
- >;
42
+ export type ConditionalPick<Base, Condition> = ConditionalKeys<Base, Condition> extends infer Keys
43
+ ? IsNever<Keys> extends true
44
+ ? never
45
+ : Pick<Base, Keys & keyof Base>
46
+ : never;
45
47
 
46
48
  export {};
@@ -38,6 +38,7 @@ import type {DelimiterCase} from 'type-fest';
38
38
 
39
39
  const someVariable: DelimiterCase<'fooBar', '#'> = 'foo#bar';
40
40
  const someVariableNoSplitOnNumbers: DelimiterCase<'p2pNetwork', '#', {splitOnNumbers: false}> = 'p2p#network';
41
+ const someVariableWithPunctuation: DelimiterCase<'div.card::after', '#', {splitOnPunctuation: true}> = 'div#card#after';
41
42
 
42
43
  // Advanced
43
44
 
@@ -4,7 +4,7 @@ import type {UnknownArray} from './unknown-array.d.ts';
4
4
  import type {WordsOptions} from './words.d.ts';
5
5
 
6
6
  /**
7
- Convert object properties to delimiter case recursively.
7
+ Convert object properties to a custom string delimiter casing recursively.
8
8
 
9
9
  This can be useful when, for example, converting some API types from a different style.
10
10
 
@@ -51,6 +51,13 @@ const splitOnNumbers: DelimiterCasedPropertiesDeep<{line1: {line2: [{line3: stri
51
51
  ],
52
52
  },
53
53
  };
54
+
55
+ const splitOnPunctuation: DelimiterCasedPropertiesDeep<{'user@info': {'user::id': number; 'user::name': string}}, '-', {splitOnPunctuation: true}> = {
56
+ 'user-info': {
57
+ 'user-id': 1,
58
+ 'user-name': 'Tom',
59
+ },
60
+ };
54
61
  ```
55
62
 
56
63
  @category Change case
@@ -3,7 +3,7 @@ import type {ApplyDefaultOptions} from './internal/index.d.ts';
3
3
  import type {WordsOptions} from './words.d.ts';
4
4
 
5
5
  /**
6
- Convert object properties to delimiter case but not recursively.
6
+ Convert object properties to a custom string delimiter casing.
7
7
 
8
8
  This can be useful when, for example, converting some API types from a different style.
9
9
 
@@ -27,6 +27,10 @@ const result: DelimiterCasedProperties<User, '-'> = {
27
27
  const splitOnNumbers: DelimiterCasedProperties<{line1: string}, '-', {splitOnNumbers: true}> = {
28
28
  'line-1': 'string',
29
29
  };
30
+
31
+ const splitOnPunctuation: DelimiterCasedProperties<{'foo::bar': string}, '-', {splitOnPunctuation: true}> = {
32
+ 'foo-bar': 'string',
33
+ };
30
34
  ```
31
35
 
32
36
  @category Change case
@@ -32,7 +32,7 @@ Unfortunately, `Record<string, never>`, `Record<keyof any, never>` and `Record<n
32
32
  export type EmptyObject = {[emptyObjectSymbol]?: never};
33
33
 
34
34
  /**
35
- Returns a `boolean` for whether the type is strictly equal to an empty plain object, the `{}` value.
35
+ Returns a boolean for whether the type is strictly equal to an empty plain object, the `{}` value.
36
36
 
37
37
  @example
38
38
  ```
@@ -6,7 +6,7 @@ type ObjectEntries<BaseType> = Array<_ObjectEntry<BaseType>>;
6
6
  type SetEntries<BaseType extends Set<unknown>> = Array<_SetEntry<BaseType>>;
7
7
 
8
8
  /**
9
- Many collections have an `entries` method which returns an array of a given object's own enumerable string-keyed property [key, value] pairs. The `Entries` type will return the type of that collection's entries.
9
+ Create a type that describes the key-value pairs produced when calling a collections `entries` method.
10
10
 
11
11
  For example the {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries|`Object`}, {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries|`Map`}, {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries|`Array`}, and {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/entries|`Set`} collections all have this method. Note that `WeakMap` and `WeakSet` do not have this method since their entries are not enumerable.
12
12
 
package/source/entry.d.ts CHANGED
@@ -7,7 +7,7 @@ export type _ObjectEntry<BaseType> = [keyof BaseType, BaseType[keyof BaseType]];
7
7
  export type _SetEntry<BaseType> = BaseType extends Set<infer ItemType> ? [ItemType, ItemType] : never;
8
8
 
9
9
  /**
10
- Many collections have an `entries` method which returns an array of a given object's own enumerable string-keyed property [key, value] pairs. The `Entry` type will return the type of that collection's entry.
10
+ Create a type that describes a single key-value pair produced when calling a collections `entries` method.
11
11
 
12
12
  For example the {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries|`Object`}, {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries|`Map`}, {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries|`Array`}, and {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/entries|`Set`} collections all have this method. Note that `WeakMap` and `WeakSet` do not have this method since their entries are not enumerable.
13
13
 
@@ -0,0 +1,57 @@
1
+ import type {IsNever} from './is-never.d.ts';
2
+ import type {IsAny} from './is-any.d.ts';
3
+ import type {If} from './if.d.ts';
4
+ import type {IsEqual} from './is-equal.d.ts';
5
+ import type {IfNotAnyOrNever} from './internal/type.d.ts';
6
+
7
+ /**
8
+ A stricter version of `Exclude<T, U>` that excludes types only when they are exactly identical.
9
+
10
+ @example
11
+ ```
12
+ import type {ExcludeExactly} from 'type-fest';
13
+
14
+ type TestExclude1 = Exclude<'a' | 'b' | 'c' | 1 | 2 | 3, string>;
15
+ //=> 1 | 2 | 3
16
+
17
+ type TestExcludeExactly1 = ExcludeExactly<'a' | 'b' | 'c' | 1 | 2 | 3, string>;
18
+ //=> 'a' | 'b' | 'c' | 1 | 2 | 3
19
+
20
+ type TestExclude2 = Exclude<'a' | 'b' | 'c' | 1 | 2 | 3, any>;
21
+ //=> never
22
+
23
+ type TestExcludeExactly2 = ExcludeExactly<'a' | 'b' | 'c' | 1 | 2 | 3, any>;
24
+ //=> 'a' | 'b' | 'c' | 1 | 2 | 3
25
+
26
+ type TestExclude3 = Exclude<{a: string} | {a: string; b: string}, {a: string}>;
27
+ //=> never
28
+
29
+ type TestExcludeExactly3 = ExcludeExactly<{a: string} | {a: string; b: string}, {a: string}>;
30
+ //=> {a: string; b: string}
31
+ ```
32
+
33
+ @category Improved Built-in
34
+ */
35
+ export type ExcludeExactly<Union, Delete> =
36
+ IfNotAnyOrNever<
37
+ Union,
38
+ _ExcludeExactly<Union, Delete>,
39
+ // If `Union` is `any`, then if `Delete` is `any`, return `never`, else return `Union`.
40
+ If<IsAny<Delete>, never, Union>,
41
+ // If `Union` is `never`, then if `Delete` is `never`, return `never`, else return `Union`.
42
+ If<IsNever<Delete>, never, Union>
43
+ >;
44
+
45
+ type _ExcludeExactly<Union, Delete> =
46
+ IfNotAnyOrNever<Delete,
47
+ Union extends unknown // For distributing `Union`
48
+ ? [Delete extends unknown // For distributing `Delete`
49
+ ? If<IsEqual<Union, Delete>, true, never>
50
+ : never] extends [never] ? Union : never
51
+ : never,
52
+ // If `Delete` is `any` or `never`, then return `Union`,
53
+ // because `Union` cannot be `any` or `never` here.
54
+ Union, Union
55
+ >;
56
+
57
+ export {};
package/source/get.d.ts CHANGED
@@ -161,7 +161,7 @@ type PropertyOf<BaseType, Key extends string, Options extends Required<GetOption
161
161
 
162
162
  // This works by first splitting the path based on `.` and `[...]` characters into a tuple of string keys. Then it recursively uses the head key to get the next property of the current object, until there are no keys left. Number keys extract the item type from arrays, or are converted to strings to extract types from tuples and dictionaries with number keys.
163
163
  /**
164
- Get a deeply-nested property from an object using a key path, like Lodash's `.get()` function.
164
+ Get a deeply-nested property from an object using a key path, like [Lodash's `.get()`](https://lodash.com/docs/latest#get) function.
165
165
 
166
166
  Use-case: Retrieve a property from deep inside an API response or some other complex object.
167
167
 
@@ -16,9 +16,42 @@ type B = GreaterThanOrEqual<1, 1>;
16
16
  type C = GreaterThanOrEqual<1, 5>;
17
17
  //=> false
18
18
  ```
19
+
20
+ Note: If either argument is the non-literal `number` type, the result is `boolean`.
21
+
22
+ @example
23
+ ```
24
+ import type {GreaterThanOrEqual} from 'type-fest';
25
+
26
+ type A = GreaterThanOrEqual<number, 1>;
27
+ //=> boolean
28
+
29
+ type B = GreaterThanOrEqual<1, number>;
30
+ //=> boolean
31
+
32
+ type C = GreaterThanOrEqual<number, number>;
33
+ //=> boolean
34
+ ```
35
+
36
+ @example
37
+ ```
38
+ import type {GreaterThanOrEqual} from 'type-fest';
39
+
40
+ // Use `GreaterThanOrEqual` to constrain a function parameter to non-negative numbers.
41
+ declare function setNonNegative<N extends number>(value: GreaterThanOrEqual<N, 0> extends true ? N : never): void;
42
+
43
+ setNonNegative(0); // ✅ Allowed
44
+ setNonNegative(1); // ✅ Allowed
45
+
46
+ // @ts-expect-error
47
+ setNonNegative(-1);
48
+
49
+ // @ts-expect-error
50
+ setNonNegative(-2);
51
+ ```
19
52
  */
20
53
  export type GreaterThanOrEqual<A extends number, B extends number> = number extends A | B
21
- ? never
54
+ ? boolean
22
55
  : A extends number // For distributing `A`
23
56
  ? B extends number // For distributing `B`
24
57
  ? A extends B
@@ -1,8 +1,9 @@
1
- import type {NumberAbsolute, PositiveNumericStringGt} from './internal/index.d.ts';
1
+ import type {PositiveNumericStringGt} from './internal/index.d.ts';
2
2
  import type {IsEqual} from './is-equal.d.ts';
3
3
  import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric.d.ts';
4
4
  import type {And} from './and.d.ts';
5
5
  import type {Or} from './or.d.ts';
6
+ import type {Absolute} from './absolute.d.ts';
6
7
 
7
8
  /**
8
9
  Returns a boolean for whether a given number is greater than another number.
@@ -20,12 +21,45 @@ type B = GreaterThan<1, 1>;
20
21
  type C = GreaterThan<1, 5>;
21
22
  //=> false
22
23
  ```
24
+
25
+ Note: If either argument is the non-literal `number` type, the result is `boolean`.
26
+
27
+ @example
28
+ ```
29
+ import type {GreaterThan} from 'type-fest';
30
+
31
+ type A = GreaterThan<number, 1>;
32
+ //=> boolean
33
+
34
+ type B = GreaterThan<1, number>;
35
+ //=> boolean
36
+
37
+ type C = GreaterThan<number, number>;
38
+ //=> boolean
39
+ ```
40
+
41
+ @example
42
+ ```
43
+ import type {GreaterThan} from 'type-fest';
44
+
45
+ // Use `GreaterThan` to constrain a function parameter to positive numbers.
46
+ declare function setPositive<N extends number>(value: GreaterThan<N, 0> extends true ? N : never): void;
47
+
48
+ setPositive(1); // ✅ Allowed
49
+ setPositive(2); // ✅ Allowed
50
+
51
+ // @ts-expect-error
52
+ setPositive(0);
53
+
54
+ // @ts-expect-error
55
+ setPositive(-1);
56
+ ```
23
57
  */
24
58
  export type GreaterThan<A extends number, B extends number> =
25
59
  A extends number // For distributing `A`
26
60
  ? B extends number // For distributing `B`
27
61
  ? number extends A | B
28
- ? never
62
+ ? boolean
29
63
  : [
30
64
  IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
31
65
  IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
@@ -49,7 +83,7 @@ export type GreaterThan<A extends number, B extends number> =
49
83
  ? true
50
84
  : [false, false] extends R
51
85
  ? PositiveNumericStringGt<`${A}`, `${B}`>
52
- : PositiveNumericStringGt<`${NumberAbsolute<B>}`, `${NumberAbsolute<A>}`>
86
+ : PositiveNumericStringGt<`${Absolute<B>}`, `${Absolute<A>}`>
53
87
  : never
54
88
  : never
55
89
  : never // Should never happen
@@ -1,7 +1,7 @@
1
1
  import type {OptionalKeysOf} from './optional-keys-of.d.ts';
2
2
 
3
3
  /**
4
- Creates a type that represents `true` or `false` depending on whether the given type has any optional fields.
4
+ Returns a boolean for whether the given type has any optional fields.
5
5
 
6
6
  This is useful when you want to create an API whose behavior depends on the presence or absence of optional fields.
7
7
 
@@ -1,7 +1,7 @@
1
1
  import type {ReadonlyKeysOf} from './readonly-keys-of.d.ts';
2
2
 
3
3
  /**
4
- Creates a type that represents `true` or `false` depending on whether the given type has any readonly fields.
4
+ Returns a boolean for whether the given type has any readonly fields.
5
5
 
6
6
  This is useful when you want to create an API whose behavior depends on the presence or absence of readonly fields.
7
7
 
@@ -1,7 +1,7 @@
1
1
  import type {RequiredKeysOf} from './required-keys-of.d.ts';
2
2
 
3
3
  /**
4
- Creates a type that represents `true` or `false` depending on whether the given type has any required fields.
4
+ Returns a boolean for whether the given type has any required fields.
5
5
 
6
6
  This is useful when you want to create an API whose behavior depends on the presence or absence of required fields.
7
7
 
@@ -1,7 +1,7 @@
1
1
  import type {WritableKeysOf} from './writable-keys-of.d.ts';
2
2
 
3
3
  /**
4
- Creates a type that represents `true` or `false` depending on whether the given type has any writable fields.
4
+ Returns a boolean for whether the given type has any writable fields.
5
5
 
6
6
  This is useful when you want to create an API whose behavior depends on the presence or absence of writable fields.
7
7
 
@@ -2,9 +2,7 @@ import type {IntRange} from './int-range.d.ts';
2
2
  import type {Sum} from './sum.d.ts';
3
3
 
4
4
  /**
5
- Generate a union of numbers.
6
-
7
- The numbers are created from the given `Start` (inclusive) parameter to the given `End` (inclusive) parameter.
5
+ Generate a union of numbers between a specified start and end (both inclusive), with an optional step.
8
6
 
9
7
  You skip over numbers using the `Step` parameter (defaults to `1`). For example, `IntClosedRange<0, 10, 2>` will create a union of `0 | 2 | 4 | 6 | 8 | 10`.
10
8
 
@@ -2,9 +2,7 @@ import type {TupleOf} from './tuple-of.d.ts';
2
2
  import type {Subtract} from './subtract.d.ts';
3
3
 
4
4
  /**
5
- Generate a union of numbers.
6
-
7
- The numbers are created from the given `Start` (inclusive) parameter to the given `End` (exclusive) parameter.
5
+ Generate a union of numbers between a specified start (inclusive) and end (exclusive), with an optional step.
8
6
 
9
7
  You skip over numbers using the `Step` parameter (defaults to `1`). For example, `IntRange<0, 10, 2>` will create a union of `0 | 2 | 4 | 6 | 8`.
10
8
 
@@ -56,9 +54,9 @@ type PrivateIntRange<
56
54
  // The final `List` is `[...StartLengthTuple, ...[number, ...GapLengthTuple], ...[number, ...GapLengthTuple], ... ...]`, so can initialize the `List` with `[...StartLengthTuple]`
57
55
  List extends unknown[] = TupleOf<Start, never>,
58
56
  EndLengthTuple extends unknown[] = TupleOf<End>,
59
- > = Gap extends 0 ?
57
+ > = Gap extends 0
60
58
  // Handle the case that without `Step`
61
- List['length'] extends End // The result of "List[length] === End"
59
+ ? List['length'] extends End // The result of "List[length] === End"
62
60
  ? Exclude<List[number], never> // All unused elements are `never`, so exclude them
63
61
  : PrivateIntRange<Start, End, Step, Gap, [...List, List['length'] ]>
64
62
  // Handle the case that with `Step`