@depup/type-fest 5.5.0-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 (117) hide show
  1. package/README.md +2 -2
  2. package/changes.json +1 -1
  3. package/index.d.ts +16 -8
  4. package/package.json +8 -6
  5. package/readme.md +85 -61
  6. package/source/absolute.d.ts +52 -0
  7. package/source/all-extend.d.ts +6 -4
  8. package/source/all-union-fields.d.ts +18 -18
  9. package/source/and.d.ts +1 -1
  10. package/source/array-reverse.d.ts +4 -3
  11. package/source/array-splice.d.ts +24 -24
  12. package/source/array-tail.d.ts +4 -4
  13. package/source/camel-case.d.ts +38 -5
  14. package/source/camel-cased-properties-deep.d.ts +11 -4
  15. package/source/camel-cased-properties.d.ts +5 -1
  16. package/source/conditional-keys.d.ts +1 -1
  17. package/source/delimiter-case.d.ts +11 -9
  18. package/source/delimiter-cased-properties-deep.d.ts +8 -1
  19. package/source/delimiter-cased-properties.d.ts +5 -1
  20. package/source/empty-object.d.ts +1 -1
  21. package/source/entries.d.ts +1 -1
  22. package/source/entry.d.ts +1 -1
  23. package/source/exclude-exactly.d.ts +11 -11
  24. package/source/exclude-rest-element.d.ts +4 -4
  25. package/source/exclusify-union.d.ts +4 -4
  26. package/source/extends-strict.d.ts +129 -22
  27. package/source/extract-exactly.d.ts +56 -0
  28. package/source/get.d.ts +1 -1
  29. package/source/greater-than.d.ts +3 -2
  30. package/source/has-optional-keys.d.ts +1 -1
  31. package/source/has-readonly-keys.d.ts +1 -1
  32. package/source/has-required-keys.d.ts +1 -1
  33. package/source/has-writable-keys.d.ts +1 -1
  34. package/source/int-closed-range.d.ts +1 -3
  35. package/source/int-range.d.ts +3 -5
  36. package/source/internal/array.d.ts +8 -8
  37. package/source/internal/keys.d.ts +9 -9
  38. package/source/internal/numeric.d.ts +19 -27
  39. package/source/internal/object.d.ts +44 -6
  40. package/source/internal/string.d.ts +1 -76
  41. package/source/internal/tuple.d.ts +2 -2
  42. package/source/internal/type.d.ts +16 -10
  43. package/source/is-boolean-literal.d.ts +39 -0
  44. package/source/is-integer.d.ts +8 -8
  45. package/source/is-literal.d.ts +43 -267
  46. package/source/is-numeric-literal.d.ts +50 -0
  47. package/source/is-string-literal.d.ts +72 -0
  48. package/source/is-symbol-literal.d.ts +39 -0
  49. package/source/is-union.d.ts +12 -12
  50. package/source/iterable-element.d.ts +5 -5
  51. package/source/jsonify.d.ts +5 -9
  52. package/source/kebab-case.d.ts +1 -0
  53. package/source/kebab-cased-properties-deep.d.ts +7 -0
  54. package/source/kebab-cased-properties.d.ts +5 -1
  55. package/source/keys-of-union.d.ts +2 -2
  56. package/source/last-array-element.d.ts +66 -13
  57. package/source/less-than-or-equal.d.ts +1 -1
  58. package/source/literal-to-primitive.d.ts +1 -1
  59. package/source/literal-union.d.ts +1 -1
  60. package/source/merge-exclusive.d.ts +3 -3
  61. package/source/multidimensional-array.d.ts +1 -1
  62. package/source/multidimensional-readonly-array.d.ts +1 -1
  63. package/source/non-nullable-deep.d.ts +102 -0
  64. package/source/numeric.d.ts +3 -3
  65. package/source/object-merge.d.ts +21 -16
  66. package/source/omit-deep.d.ts +22 -22
  67. package/source/or.d.ts +1 -1
  68. package/source/package-json.d.ts +7 -7
  69. package/source/partial-deep.d.ts +3 -1
  70. package/source/pascal-case.d.ts +1 -0
  71. package/source/pascal-cased-properties-deep.d.ts +7 -0
  72. package/source/pascal-cased-properties.d.ts +5 -1
  73. package/source/pick-deep.d.ts +2 -0
  74. package/source/readonly-deep.d.ts +5 -3
  75. package/source/remove-prefix.d.ts +16 -34
  76. package/source/remove-suffix.d.ts +114 -0
  77. package/source/rename-keys.d.ts +163 -0
  78. package/source/replace.d.ts +2 -2
  79. package/source/require-all-or-none.d.ts +5 -5
  80. package/source/require-at-least-one.d.ts +9 -11
  81. package/source/require-exactly-one.d.ts +7 -7
  82. package/source/require-one-or-none.d.ts +5 -5
  83. package/source/required-deep.d.ts +3 -1
  84. package/source/schema.d.ts +16 -8
  85. package/source/screaming-snake-case.d.ts +1 -0
  86. package/source/set-non-nullable-deep.d.ts +8 -4
  87. package/source/set-non-nullable.d.ts +1 -1
  88. package/source/set-optional.d.ts +5 -5
  89. package/source/set-readonly.d.ts +3 -3
  90. package/source/set-required-deep.d.ts +3 -2
  91. package/source/set-required.d.ts +3 -3
  92. package/source/shared-union-fields-deep.d.ts +4 -3
  93. package/source/shared-union-fields.d.ts +9 -9
  94. package/source/snake-case.d.ts +1 -0
  95. package/source/snake-cased-properties-deep.d.ts +7 -0
  96. package/source/snake-cased-properties.d.ts +5 -1
  97. package/source/some-extend.d.ts +6 -4
  98. package/source/split-on-rest-element.d.ts +6 -4
  99. package/source/split.d.ts +1 -1
  100. package/source/string-length.d.ts +38 -0
  101. package/source/string-repeat.d.ts +48 -21
  102. package/source/string-slice.d.ts +1 -1
  103. package/source/string-to-array.d.ts +97 -0
  104. package/source/string-to-number.d.ts +67 -0
  105. package/source/subtract.d.ts +4 -3
  106. package/source/sum.d.ts +5 -4
  107. package/source/tagged.d.ts +3 -5
  108. package/source/tsconfig-json.d.ts +40 -8
  109. package/source/tuple-of.d.ts +42 -8
  110. package/source/typed-array.d.ts +1 -0
  111. package/source/union-length.d.ts +27 -0
  112. package/source/union-to-intersection.d.ts +1 -1
  113. package/source/union-to-tuple.d.ts +8 -4
  114. package/source/unwrap-required.d.ts +37 -0
  115. package/source/words.d.ts +30 -4
  116. package/source/writable.d.ts +14 -14
  117. package/source/xor.d.ts +1 -1
@@ -1,3 +1,8 @@
1
+ import type {If} from './if.d.ts';
2
+ import type {IfNotAnyOrNever, IsExactOptionalPropertyTypesEnabled} from './internal/type.d.ts';
3
+ import type {SplitOnRestElement} from './split-on-rest-element.d.ts';
4
+ import type {UnknownArray} from './unknown-array.d.ts';
5
+
1
6
  /**
2
7
  Extract the type of the last element of an array.
3
8
 
@@ -16,21 +21,69 @@ const last2 = lastOf([true, false, 'baz', 10]);
16
21
  //=> 10
17
22
  ```
18
23
 
24
+ Note: When the array ends with an optional or rest element, the last element's position becomes ambiguous. In such cases, the result is a union of the types of all elements that could potentially be the last element of the array.
25
+
26
+ @example
27
+ ```
28
+ import type {LastArrayElement} from 'type-fest';
29
+
30
+ type A = LastArrayElement<[string, number?, bigint?]>;
31
+ //=> bigint | number | string
32
+
33
+ type B = LastArrayElement<[string, number, bigint?, ...boolean[]]>;
34
+ //=> boolean | bigint | number
35
+ ```
36
+
37
+ Note: If empty array is a valid value for the array type, the result includes an `undefined`. This aligns with the runtime behavior of `[].at(-1)`.
38
+
39
+ @example
40
+ ```
41
+ import type {LastArrayElement} from 'type-fest';
42
+
43
+ type A = LastArrayElement<[]>;
44
+ //=> undefined
45
+
46
+ // `[]` is assignable to `string[]`
47
+ type B = LastArrayElement<string[]>;
48
+ //=> string | undefined
49
+
50
+ // `[]` is assignable to `[string?, number?]`
51
+ type C = LastArrayElement<[string?, number?]>;
52
+ //=> number | string | undefined
53
+
54
+ // `[]` is assignable to [string?, number?, ...bigint[]]`
55
+ type D = LastArrayElement<[string?, number?, ...bigint[]]>;
56
+ //=> bigint | number | string | undefined
57
+ ```
58
+
19
59
  @category Array
20
60
  @category Template literal
21
61
  */
22
- export type LastArrayElement<Elements extends readonly unknown[], ElementBeforeTailingSpreadElement = never> =
23
- // If the last element of an array is a spread element, the `LastArrayElement` result should be `'the type of the element before the spread element' | 'the type of the spread element'`.
24
- Elements extends readonly []
25
- ? ElementBeforeTailingSpreadElement
26
- : Elements extends readonly [...infer U, infer V]
27
- ? V
28
- : Elements extends readonly [infer U, ...infer V]
29
- // If we return `V[number] | U` directly, it would be wrong for `[[string, boolean, object, ...number[]]`.
30
- // So we need to recurse type `V` and carry over the type of the element before the spread element.
31
- ? LastArrayElement<V, U>
32
- : Elements extends ReadonlyArray<infer U>
33
- ? U | ElementBeforeTailingSpreadElement
34
- : never;
62
+ export type LastArrayElement<TArray extends UnknownArray> =
63
+ IfNotAnyOrNever<TArray, {
64
+ ifNot: TArray extends UnknownArray // For distributing `TArray`
65
+ ? SplitOnRestElement<TArray> extends readonly [infer BeforeRest extends UnknownArray, infer Rest extends UnknownArray, infer AfterRest extends UnknownArray]
66
+ ? _LastArrayElement<BeforeRest, Rest, AfterRest>
67
+ : never
68
+ : never;
69
+ }>;
70
+
71
+ type _LastArrayElement<BeforeRest extends UnknownArray, Rest extends UnknownArray, AfterRest extends UnknownArray> =
72
+ AfterRest extends readonly [...any, infer Last] // Note there are no optional elements in `AfterRest`.
73
+ ? Last // If there's a `Last` in `AfterRest`, then that's the result.
74
+ : Rest[number] | BeforeRestLastElement<BeforeRest>; // Otherwise, the result is union of the `Rest` element and the last element in `BeforeRest`.
75
+
76
+ type BeforeRestLastElement<BeforeRest extends UnknownArray, Accumulator = never> =
77
+ BeforeRest extends readonly []
78
+ ? Accumulator | undefined
79
+ : BeforeRest extends readonly [...any, infer Last]
80
+ ? Last | Accumulator
81
+ : BeforeRest extends readonly [...infer Rest, (infer Last)?]
82
+ ? BeforeRestLastElement<
83
+ Rest,
84
+ // Add `undefined` for optional elements, if `exactOptionalPropertyTypes` is disabled.
85
+ Last | Accumulator | If<IsExactOptionalPropertyTypesEnabled, never, undefined>
86
+ >
87
+ : never;
35
88
 
36
89
  export {};
@@ -1,7 +1,7 @@
1
1
  import type {GreaterThan} from './greater-than.d.ts';
2
2
 
3
3
  /**
4
- Returns a boolean for whether a given number is less than or equal to another number.
4
+ Returns a boolean for whether a given number is less than or equal to another number.
5
5
 
6
6
  @example
7
7
  ```
@@ -1,5 +1,5 @@
1
1
  /**
2
- Given a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types) return the {@link Primitive | primitive type} it belongs to, or `never` if it's not a primitive.
2
+ Given a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types) return the [primitive type](https://developer.mozilla.org/en-US/docs/Glossary/Primitive) it belongs to, or `never` if it's not a primitive.
3
3
 
4
4
  Use-case: Working with generic types that may be literal types.
5
5
 
@@ -3,7 +3,7 @@ import type {Primitive} from './primitive.d.ts';
3
3
  export type _LiteralStringUnion<T> = LiteralUnion<T, string>;
4
4
 
5
5
  /**
6
- Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union.
6
+ Create a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union.
7
7
 
8
8
  Currently, when a union type of a primitive type is combined with literal types, TypeScript loses all information about the combined literals. Thus, when such type is used in an IDE with autocompletion, no suggestions are made for the declared literals.
9
9
 
@@ -38,8 +38,8 @@ exclusiveOptions = {exclusive1: true, exclusive2: 'hi'};
38
38
  @category Object
39
39
  */
40
40
  export type MergeExclusive<FirstType, SecondType> =
41
- (FirstType | SecondType) extends object ?
42
- (Without<FirstType, SecondType> & SecondType) | (Without<SecondType, FirstType> & FirstType) :
43
- FirstType | SecondType;
41
+ (FirstType | SecondType) extends object
42
+ ? (Without<FirstType, SecondType> & SecondType) | (Without<SecondType, FirstType> & FirstType)
43
+ : FirstType | SecondType;
44
44
 
45
45
  export {};
@@ -4,7 +4,7 @@ import type {IsEqual} from './is-equal.d.ts';
4
4
  type Recursive<T> = Array<Recursive<T>>;
5
5
 
6
6
  /**
7
- Creates a type that represents a multidimensional array of the given type and dimension.
7
+ Create a type that represents a multidimensional array of the given type and dimension.
8
8
 
9
9
  Use-cases:
10
10
  - Return a n-dimensional array from functions.
@@ -4,7 +4,7 @@ import type {IsEqual} from './is-equal.d.ts';
4
4
  type Recursive<T> = ReadonlyArray<Recursive<T>>;
5
5
 
6
6
  /**
7
- Creates a type that represents a multidimensional readonly array that of the given type and dimension.
7
+ Create a type that represents a multidimensional readonly array of the given type and dimension.
8
8
 
9
9
  Use-cases:
10
10
  - Return a n-dimensional array from functions.
@@ -0,0 +1,102 @@
1
+ import type {BuiltIns, HasMultipleCallSignatures} from './internal/type.d.ts';
2
+ import type {IsNever} from './is-never.d.ts';
3
+ import type {Simplify} from './simplify.d.ts';
4
+
5
+ /**
6
+ Recursively removes `null` and `undefined` from the specified type.
7
+
8
+ Use-cases:
9
+ - Normalizing data received from external sources where `null`/`undefined` have been cleaned.
10
+ - Creating non-nullable variants of deeply nested types.
11
+
12
+ NOTE: Optional modifiers (`?`) are not removed from properties. For example, `NonNullableDeep<{foo?: string | null | undefined}>` will result in `{foo?: string}`. To remove both optional modifiers and nullables, use {@link RequiredDeep} in conjunction with this type.
13
+
14
+ @example
15
+ ```
16
+ import type {NonNullableDeep} from 'type-fest';
17
+
18
+ type UserDraft = {
19
+ name: string | null;
20
+ address: {
21
+ city: string | undefined;
22
+ postalCode: string | null;
23
+ landmark?: string | undefined;
24
+ };
25
+ tags: Array<string | null>;
26
+ visits: Map<string | null, {
27
+ date: Date | null;
28
+ notes: Set<string | undefined>;
29
+ }>;
30
+ };
31
+
32
+ type User = NonNullableDeep<UserDraft>;
33
+ //=> {
34
+ // name: string;
35
+ // address: {
36
+ // city: string;
37
+ // postalCode: string;
38
+ // landmark?: string;
39
+ // };
40
+ // tags: string[];
41
+ // visits: Map<string, {
42
+ // date: Date;
43
+ // notes: Set<string>;
44
+ // }>;
45
+ // }
46
+ ```
47
+
48
+ @example
49
+ ```
50
+ import type {NonNullableDeep} from 'type-fest';
51
+
52
+ type ArrayExample = NonNullableDeep<[{a: number | undefined}, ...Array<{b: string | null}>]>;
53
+ //=> [{a: number}, ...{b: string}[]]
54
+
55
+ type MapExample = NonNullableDeep<{a: Map<{a: string | null}, {c: number | undefined}>}>;
56
+ //=> {a: Map<{a: string}, {c: number}>}
57
+
58
+ type SetExample = NonNullableDeep<Set<{a: string | null}> | null | undefined>;
59
+ //=> Set<{a: string}>
60
+
61
+ type PromiseExample = NonNullableDeep<{a: Promise<{b: string | null}>}>;
62
+ //=> {a: Promise<{b: string}>}
63
+
64
+ type FunctionExample = NonNullableDeep<(a: string | null) => number | undefined>;
65
+ //=> (a: string) => number
66
+ ```
67
+
68
+ @category Utilities
69
+ @category Object
70
+ @category Array
71
+ @category Set
72
+ @category Map
73
+ */
74
+ export type NonNullableDeep<T> =
75
+ T extends BuiltIns | (new (...arguments_: any[]) => unknown)
76
+ ? Exclude<T, null | undefined> // `Exclude` is used instead of `NonNullable` because `NonNullable<void>` results in `void & {}`.
77
+ : T extends Map<infer KeyType, infer ValueType>
78
+ ? Map<NonNullableDeep<KeyType>, NonNullableDeep<ValueType>>
79
+ : T extends Set<infer ItemType>
80
+ ? Set<NonNullableDeep<ItemType>>
81
+ : T extends ReadonlyMap<infer KeyType, infer ValueType>
82
+ ? ReadonlyMap<NonNullableDeep<KeyType>, NonNullableDeep<ValueType>>
83
+ : T extends ReadonlySet<infer ItemType>
84
+ ? ReadonlySet<NonNullableDeep<ItemType>>
85
+ : T extends WeakMap<infer KeyType, infer ValueType>
86
+ ? WeakMap<NonNullableDeep<KeyType>, NonNullableDeep<ValueType>>
87
+ : T extends WeakSet<infer ItemType>
88
+ ? WeakSet<NonNullableDeep<ItemType>>
89
+ : T extends Promise<infer ValueType>
90
+ ? Promise<NonNullableDeep<ValueType>>
91
+ : T extends (...arguments_: any[]) => unknown
92
+ ? HasMultipleCallSignatures<T> extends true
93
+ ? T
94
+ : ((...arguments_: NonNullableDeep<Parameters<T>>) => NonNullableDeep<ReturnType<T>>)
95
+ & (IsNever<keyof T> extends true
96
+ ? unknown
97
+ : NonNullableDeep<Simplify<T>>) // `Simplify` removes the call signature
98
+ : T extends object
99
+ ? {[P in keyof T]: NonNullableDeep<T[P]>}
100
+ : unknown;
101
+
102
+ export {};
@@ -121,9 +121,9 @@ declare function setPercentage<T extends number>(length: Float<T>): void;
121
121
  @category Numeric
122
122
  */
123
123
  export type Float<T> =
124
- T extends unknown // To distributive type
125
- ? IsFloat<T> extends true ? T : never
126
- : never; // Never happens
124
+ T extends unknown // To distributive type
125
+ ? IsFloat<T> extends true ? T : never
126
+ : never; // Never happens
127
127
 
128
128
  /**
129
129
  A negative (`-∞ < x < 0`) `number` that is not an integer.
@@ -90,22 +90,27 @@ Note: If you want a simple merge where properties from the second object always
90
90
  @category Object
91
91
  */
92
92
  export type ObjectMerge<First extends object, Second extends object> =
93
- IfNotAnyOrNever<First, IfNotAnyOrNever<Second, First extends unknown // For distributing `First`
94
- ? Second extends unknown // For distributing `Second`
95
- ? First extends MapsSetsOrArrays
96
- ? unknown
97
- : Second extends MapsSetsOrArrays
98
- ? unknown
99
- : _ObjectMerge<
100
- First,
101
- Second,
102
- NormalizedLiteralKeys<First>,
103
- NormalizedLiteralKeys<Second>,
104
- IsExactOptionalPropertyTypesEnabled extends true ? Required<First> : First,
105
- IsExactOptionalPropertyTypesEnabled extends true ? Required<Second> : Second
106
- >
107
- : never // Should never happen
108
- : never>, First & Second>; // Should never happen
93
+ IfNotAnyOrNever<First, {
94
+ ifNot: IfNotAnyOrNever<Second, {
95
+ ifNot: First extends unknown // For distributing `First`
96
+ ? Second extends unknown // For distributing `Second`
97
+ ? First extends MapsSetsOrArrays
98
+ ? unknown
99
+ : Second extends MapsSetsOrArrays
100
+ ? unknown
101
+ : _ObjectMerge<
102
+ First,
103
+ Second,
104
+ NormalizedLiteralKeys<First>,
105
+ NormalizedLiteralKeys<Second>,
106
+ IsExactOptionalPropertyTypesEnabled extends true ? Required<First> : First,
107
+ IsExactOptionalPropertyTypesEnabled extends true ? Required<Second> : Second
108
+ >
109
+ : never // Should never happen
110
+ : never; // Should never happen
111
+ }>;
112
+ ifAny: First & Second;
113
+ }>;
109
114
 
110
115
  type _ObjectMerge<
111
116
  First extends object,
@@ -17,7 +17,7 @@ It supports removing specific items from an array, replacing each removed item w
17
17
 
18
18
  Use-case: Remove unneeded parts of complex objects.
19
19
 
20
- Use [`Omit`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys) if you only need one level deep.
20
+ Use [`Omit<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys) if you only need one level deep.
21
21
 
22
22
  @example
23
23
  ```
@@ -93,34 +93,34 @@ type OmitDeepHelper<T, PathTuple extends UnknownArray> =
93
93
  Omit one path from the given object/array.
94
94
  */
95
95
  type OmitDeepWithOnePath<T, Path extends string | number> =
96
- T extends NonRecursiveType
97
- ? T
98
- : T extends UnknownArray ? SetArrayAccess<OmitDeepArrayWithOnePath<T, Path>, IsArrayReadonly<T>>
99
- : T extends object ? OmitDeepObjectWithOnePath<T, Path>
100
- : T;
96
+ T extends NonRecursiveType
97
+ ? T
98
+ : T extends UnknownArray ? SetArrayAccess<OmitDeepArrayWithOnePath<T, Path>, IsArrayReadonly<T>>
99
+ : T extends object ? OmitDeepObjectWithOnePath<T, Path>
100
+ : T;
101
101
 
102
102
  /**
103
103
  Omit one path from the given object.
104
104
  */
105
105
  type OmitDeepObjectWithOnePath<ObjectT extends object, P extends string | number> =
106
- P extends `${infer RecordKeyInPath}.${infer SubPath}`
107
- ? {
108
- [Key in keyof ObjectT]:
109
- IsEqual<RecordKeyInPath, ToString<Key>> extends true
110
- ? ExactKey<ObjectT, Key> extends infer RealKey
111
- ? RealKey extends keyof ObjectT
112
- ? OmitDeepWithOnePath<ObjectT[RealKey], SubPath>
106
+ P extends `${infer RecordKeyInPath}.${infer SubPath}`
107
+ ? {
108
+ [Key in keyof ObjectT]:
109
+ IsEqual<RecordKeyInPath, ToString<Key>> extends true
110
+ ? ExactKey<ObjectT, Key> extends infer RealKey
111
+ ? RealKey extends keyof ObjectT
112
+ ? OmitDeepWithOnePath<ObjectT[RealKey], SubPath>
113
+ : ObjectT[Key]
113
114
  : ObjectT[Key]
114
115
  : ObjectT[Key]
115
- : ObjectT[Key]
116
- }
117
- : ExactKey<ObjectT, P> extends infer Key
118
- ? IsNever<Key> extends true
119
- ? ObjectT
120
- : Key extends PropertyKey
121
- ? Omit<ObjectT, Key>
122
- : ObjectT
123
- : ObjectT;
116
+ }
117
+ : ExactKey<ObjectT, P> extends infer Key
118
+ ? IsNever<Key> extends true
119
+ ? ObjectT
120
+ : Key extends PropertyKey
121
+ ? Omit<ObjectT, Key>
122
+ : ObjectT
123
+ : ObjectT;
124
124
 
125
125
  /**
126
126
  Omit one path from from the given array.
package/source/or.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type {OrAll} from './or-all.d.ts';
2
2
 
3
3
  /**
4
- Returns a boolean for whether either of two given types is true.
4
+ Returns a boolean for whether either of two given types is `true`.
5
5
 
6
6
  Use-case: Constructing complex conditional types where at least one condition must be satisfied.
7
7
 
@@ -699,12 +699,12 @@ Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-j
699
699
  @category File
700
700
  */
701
701
  export type PackageJson =
702
- JsonObject &
703
- PackageJson.NodeJsStandard &
704
- PackageJson.PackageJsonStandard &
705
- PackageJson.NonStandardEntryPoints &
706
- PackageJson.TypeScriptConfiguration &
707
- PackageJson.YarnConfiguration &
708
- PackageJson.JSPMConfiguration;
702
+ JsonObject
703
+ & PackageJson.NodeJsStandard
704
+ & PackageJson.PackageJsonStandard
705
+ & PackageJson.NonStandardEntryPoints
706
+ & PackageJson.TypeScriptConfiguration
707
+ & PackageJson.YarnConfiguration
708
+ & PackageJson.JSPMConfiguration;
709
709
 
710
710
  export {};
@@ -44,12 +44,14 @@ type DefaultPartialDeepOptions = {
44
44
  };
45
45
 
46
46
  /**
47
- Create a type from another type with all keys and nested keys set to optional.
47
+ Create a deeply optional version of another type.
48
48
 
49
49
  Use-cases:
50
50
  - Merging a default settings/config object with another object, the second object would be a deep partial of the default object.
51
51
  - Mocking and testing complex entities, where populating an entire object with its keys would be redundant in terms of the mock or test.
52
52
 
53
+ Use [`Partial<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype) if you only need one level deep.
54
+
53
55
  @example
54
56
  ```
55
57
  import type {PartialDeep} from 'type-fest';
@@ -12,6 +12,7 @@ import type {PascalCase} from 'type-fest';
12
12
 
13
13
  const someVariable: PascalCase<'foo-bar'> = 'FooBar';
14
14
  const preserveConsecutiveUppercase: PascalCase<'foo-BAR-baz', {preserveConsecutiveUppercase: true}> = 'FooBARBaz';
15
+ const splitOnPunctuation: PascalCase<'foo-bar>>baz', {splitOnPunctuation: true}> = 'FooBarBaz';
15
16
 
16
17
  // Advanced
17
18
 
@@ -48,6 +48,13 @@ const preserveConsecutiveUppercase: PascalCasedPropertiesDeep<{fooBAR: {fooBARBi
48
48
  }],
49
49
  },
50
50
  };
51
+
52
+ const splitOnPunctuation: PascalCasedPropertiesDeep<{'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
@@ -3,7 +3,7 @@ import type {ApplyDefaultOptions} from './internal/index.d.ts';
3
3
  import type {PascalCase} from './pascal-case.d.ts';
4
4
 
5
5
  /**
6
- Convert object properties to pascal case but not recursively.
6
+ Convert top-level object properties to pascal case.
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: PascalCasedProperties<User> = {
27
27
  const preserveConsecutiveUppercase: PascalCasedProperties<{fooBAR: string}, {preserveConsecutiveUppercase: true}> = {
28
28
  FooBAR: 'string',
29
29
  };
30
+
31
+ const splitOnPunctuation: PascalCasedProperties<{'foo::bar': string}, {splitOnPunctuation: true}> = {
32
+ FooBar: 'string',
33
+ };
30
34
  ```
31
35
 
32
36
  @category Change case
@@ -14,6 +14,8 @@ It supports recursing into arrays.
14
14
 
15
15
  Use-case: Distill complex objects down to the components you need to target.
16
16
 
17
+ Use [`Pick<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys) if you only need one level deep.
18
+
17
19
  @example
18
20
  ```
19
21
  import type {PickDeep, PartialDeep} from 'type-fest';
@@ -1,12 +1,14 @@
1
1
  import type {BuiltIns, HasMultipleCallSignatures} from './internal/index.d.ts';
2
2
 
3
3
  /**
4
- Convert `object`s, `Map`s, `Set`s, and `Array`s and all of their keys/elements into immutable structures recursively.
4
+ Create a deeply immutable version of another type.
5
5
 
6
6
  This is useful when a deeply nested structure needs to be exposed as completely immutable, for example, an imported JSON module or when receiving an API response that is passed around.
7
7
 
8
8
  Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/13923) if you want to have this type as a built-in in TypeScript.
9
9
 
10
+ Use [`Readonly<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype) if you only need one level deep.
11
+
10
12
  @example
11
13
  ```
12
14
  import type {ReadonlyDeep} from 'type-fest';
@@ -83,8 +85,8 @@ export type ReadonlyDeep<T> = T extends BuiltIns
83
85
  ? ReadonlyMapDeep<KeyType, ValueType>
84
86
  : T extends Readonly<ReadonlySet<infer ItemType>>
85
87
  ? ReadonlySetDeep<ItemType>
86
- : // Identify tuples to avoid converting them to arrays inadvertently; special case `readonly [...never[]]`, as it emerges undesirably from recursive invocations of ReadonlyDeep below.
87
- T extends readonly [] | readonly [...never[]]
88
+ // Identify tuples to avoid converting them to arrays inadvertently; special case `readonly [...never[]]`, as it emerges undesirably from recursive invocations of ReadonlyDeep below.
89
+ : T extends readonly [] | readonly [...never[]]
88
90
  ? readonly []
89
91
  : T extends readonly [infer U, ...infer V]
90
92
  ? readonly [ReadonlyDeep<U>, ...ReadonlyDeep<V>]
@@ -1,7 +1,9 @@
1
1
  import type {ApplyDefaultOptions} from './internal/object.d.ts';
2
2
  import type {IfNotAnyOrNever, Not} from './internal/type.d.ts';
3
- import type {IsStringLiteral} from './is-literal.d.ts';
3
+ import type {IsStringLiteral} from './is-string-literal.d.ts';
4
+ import type {IsNever} from './is-never.d.ts';
4
5
  import type {Or} from './or.d.ts';
6
+ import type {If} from './if.d.ts';
5
7
 
6
8
  /**
7
9
  @see {@link RemovePrefix}
@@ -58,24 +60,6 @@ export type RemovePrefixOptions = {
58
60
  type D = RemovePrefix<`id-${number}`, 'id-', {strict: false}>;
59
61
  //=> `${number}`
60
62
  ```
61
-
62
- Note: If it can be statically determined that the input string can never start with the specified non-literal prefix, then the input string is returned as-is, regardless of the value of this option.
63
- For example, ``RemovePrefix<`${string}/${number}`, `${string}:`>`` returns `` `${string}/${number}` ``, since a string of type `` `${string}/${number}` `` can never start with a prefix of type `` `${string}:` ``.
64
- ```
65
- import type {RemovePrefix} from 'type-fest';
66
-
67
- type A = RemovePrefix<`${string}/${number}`, `${string}:`, {strict: true}>;
68
- //=> `${string}/${number}`
69
-
70
- type B = RemovePrefix<`${string}/${number}`, `${string}:`, {strict: false}>;
71
- //=> `${string}/${number}`
72
-
73
- type C = RemovePrefix<'on-change', `${number}-`, {strict: true}>;
74
- //=> 'on-change'
75
-
76
- type D = RemovePrefix<'on-change', `${number}-`, {strict: false}>;
77
- //=> 'on-change'
78
- ```
79
63
  */
80
64
  strict?: boolean;
81
65
  };
@@ -110,23 +94,21 @@ type D = RemovePrefix<`handle${Capitalize<string>}`, 'handle'>;
110
94
  @category Template literal
111
95
  */
112
96
  export type RemovePrefix<S extends string, Prefix extends string, Options extends RemovePrefixOptions = {}> =
113
- IfNotAnyOrNever<
114
- S,
115
- IfNotAnyOrNever<
116
- Prefix,
117
- _RemovePrefix<S, Prefix, ApplyDefaultOptions<RemovePrefixOptions, DefaultRemovePrefixOptions, Options>>,
118
- string,
119
- S
120
- >
121
- >;
97
+ IfNotAnyOrNever<S, {
98
+ ifNot: If<
99
+ IsNever<Prefix>,
100
+ S,
101
+ _RemovePrefix<S, Prefix, ApplyDefaultOptions<RemovePrefixOptions, DefaultRemovePrefixOptions, Options>>
102
+ >;
103
+ }>;
122
104
 
123
105
  type _RemovePrefix<S extends string, Prefix extends string, Options extends Required<RemovePrefixOptions>> =
124
- Prefix extends string // For distributing `Prefix`
125
- ? S extends `${Prefix}${infer Rest}`
106
+ Prefix extends string // For distributing `Prefix`
126
107
  ? Or<IsStringLiteral<Prefix>, Not<Options['strict']>> extends true
127
- ? Rest
128
- : string // Fallback to `string` when `Prefix` is non-literal and `strict` is disabled
129
- : S // Return back `S` when `Prefix` is not present at the start of `S`
130
- : never;
108
+ ? S extends `${Prefix}${infer Rest}`
109
+ ? Rest
110
+ : S // Return back `S` when `Prefix` is not present at the start of `S`
111
+ : string // Fallback to `string` when `Prefix` is non-literal and `strict` is enabled
112
+ : never;
131
113
 
132
114
  export {};