@depup/type-fest 5.4.4-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 (201) hide show
  1. package/README.md +25 -0
  2. package/index.d.ts +211 -0
  3. package/license-cc0 +121 -0
  4. package/license-mit +9 -0
  5. package/package.json +85 -0
  6. package/readme.md +1052 -0
  7. package/source/all-extend.d.ts +120 -0
  8. package/source/all-union-fields.d.ts +91 -0
  9. package/source/and.d.ts +81 -0
  10. package/source/array-element.d.ts +46 -0
  11. package/source/array-indices.d.ts +25 -0
  12. package/source/array-reverse.d.ts +84 -0
  13. package/source/array-slice.d.ts +132 -0
  14. package/source/array-splice.d.ts +104 -0
  15. package/source/array-tail.d.ts +70 -0
  16. package/source/array-values.d.ts +24 -0
  17. package/source/arrayable.d.ts +31 -0
  18. package/source/async-return-type.d.ts +28 -0
  19. package/source/asyncify.d.ts +25 -0
  20. package/source/basic.d.ts +38 -0
  21. package/source/camel-case.d.ts +92 -0
  22. package/source/camel-cased-properties-deep.d.ts +99 -0
  23. package/source/camel-cased-properties.d.ts +45 -0
  24. package/source/characters.d.ts +67 -0
  25. package/source/conditional-except.d.ts +47 -0
  26. package/source/conditional-keys.d.ts +63 -0
  27. package/source/conditional-pick-deep.d.ts +120 -0
  28. package/source/conditional-pick.d.ts +46 -0
  29. package/source/conditional-simplify-deep.d.ts +73 -0
  30. package/source/conditional-simplify.d.ts +50 -0
  31. package/source/delimiter-case.d.ts +77 -0
  32. package/source/delimiter-cased-properties-deep.d.ts +108 -0
  33. package/source/delimiter-cased-properties.d.ts +48 -0
  34. package/source/distributed-omit.d.ts +96 -0
  35. package/source/distributed-pick.d.ts +92 -0
  36. package/source/empty-object.d.ts +51 -0
  37. package/source/entries.d.ts +64 -0
  38. package/source/entry.d.ts +67 -0
  39. package/source/exact.d.ts +73 -0
  40. package/source/except.d.ts +112 -0
  41. package/source/exclude-rest-element.d.ts +40 -0
  42. package/source/exclude-strict.d.ts +51 -0
  43. package/source/exclusify-union.d.ts +147 -0
  44. package/source/extends-strict.d.ts +44 -0
  45. package/source/extract-rest-element.d.ts +30 -0
  46. package/source/extract-strict.d.ts +51 -0
  47. package/source/find-global-type.d.ts +68 -0
  48. package/source/fixed-length-array.d.ts +97 -0
  49. package/source/get.d.ts +227 -0
  50. package/source/global-this.d.ts +24 -0
  51. package/source/globals/index.d.ts +3 -0
  52. package/source/globals/observable-like.d.ts +78 -0
  53. package/source/greater-than-or-equal.d.ts +30 -0
  54. package/source/greater-than.d.ts +58 -0
  55. package/source/has-optional-keys.d.ts +23 -0
  56. package/source/has-readonly-keys.d.ts +23 -0
  57. package/source/has-required-keys.d.ts +61 -0
  58. package/source/has-writable-keys.d.ts +23 -0
  59. package/source/if-any.d.ts +28 -0
  60. package/source/if-empty-object.d.ts +30 -0
  61. package/source/if-never.d.ts +28 -0
  62. package/source/if-null.d.ts +28 -0
  63. package/source/if-unknown.d.ts +28 -0
  64. package/source/if.d.ts +102 -0
  65. package/source/includes.d.ts +24 -0
  66. package/source/int-closed-range.d.ts +47 -0
  67. package/source/int-range.d.ts +69 -0
  68. package/source/internal/array.d.ts +151 -0
  69. package/source/internal/characters.d.ts +65 -0
  70. package/source/internal/enforce-optional.d.ts +49 -0
  71. package/source/internal/index.d.ts +11 -0
  72. package/source/internal/keys.d.ts +100 -0
  73. package/source/internal/numeric.d.ts +151 -0
  74. package/source/internal/object.d.ts +292 -0
  75. package/source/internal/string.d.ts +202 -0
  76. package/source/internal/tuple.d.ts +79 -0
  77. package/source/internal/type.d.ts +164 -0
  78. package/source/invariant-of.d.ts +85 -0
  79. package/source/is-any.d.ts +31 -0
  80. package/source/is-equal.d.ts +42 -0
  81. package/source/is-float.d.ts +43 -0
  82. package/source/is-integer.d.ts +60 -0
  83. package/source/is-literal.d.ts +315 -0
  84. package/source/is-lowercase.d.ts +38 -0
  85. package/source/is-never.d.ts +56 -0
  86. package/source/is-null.d.ts +22 -0
  87. package/source/is-nullable.d.ts +30 -0
  88. package/source/is-optional-key-of.d.ts +51 -0
  89. package/source/is-optional.d.ts +28 -0
  90. package/source/is-readonly-key-of.d.ts +55 -0
  91. package/source/is-required-key-of.d.ts +51 -0
  92. package/source/is-tuple.d.ts +92 -0
  93. package/source/is-undefined.d.ts +22 -0
  94. package/source/is-union.d.ts +40 -0
  95. package/source/is-unknown.d.ts +43 -0
  96. package/source/is-uppercase.d.ts +38 -0
  97. package/source/is-writable-key-of.d.ts +51 -0
  98. package/source/iterable-element.d.ts +66 -0
  99. package/source/join.d.ts +79 -0
  100. package/source/json-value.d.ts +33 -0
  101. package/source/jsonifiable.d.ts +37 -0
  102. package/source/jsonify.d.ts +127 -0
  103. package/source/kebab-case.d.ts +46 -0
  104. package/source/kebab-cased-properties-deep.d.ts +65 -0
  105. package/source/kebab-cased-properties.d.ts +42 -0
  106. package/source/key-as-string.d.ts +27 -0
  107. package/source/keys-of-union.d.ts +44 -0
  108. package/source/last-array-element.d.ts +36 -0
  109. package/source/less-than-or-equal.d.ts +24 -0
  110. package/source/less-than.d.ts +28 -0
  111. package/source/literal-to-primitive-deep.d.ts +71 -0
  112. package/source/literal-to-primitive.d.ts +38 -0
  113. package/source/literal-union.d.ts +39 -0
  114. package/source/merge-deep.d.ts +495 -0
  115. package/source/merge-exclusive.d.ts +45 -0
  116. package/source/merge.d.ts +62 -0
  117. package/source/multidimensional-array.d.ts +38 -0
  118. package/source/multidimensional-readonly-array.d.ts +38 -0
  119. package/source/non-empty-object.d.ts +38 -0
  120. package/source/non-empty-string.d.ts +32 -0
  121. package/source/non-empty-tuple.d.ts +24 -0
  122. package/source/numeric.d.ts +226 -0
  123. package/source/object-merge.d.ts +194 -0
  124. package/source/omit-deep.d.ts +154 -0
  125. package/source/omit-index-signature.d.ts +98 -0
  126. package/source/opaque.d.ts +3 -0
  127. package/source/optional-keys-of.d.ts +46 -0
  128. package/source/or.d.ts +89 -0
  129. package/source/override-properties.d.ts +43 -0
  130. package/source/package-json.d.ts +710 -0
  131. package/source/partial-deep.d.ts +155 -0
  132. package/source/partial-on-undefined-deep.d.ts +81 -0
  133. package/source/pascal-case.d.ts +51 -0
  134. package/source/pascal-cased-properties-deep.d.ts +72 -0
  135. package/source/pascal-cased-properties.d.ts +42 -0
  136. package/source/paths.d.ts +241 -0
  137. package/source/pick-deep.d.ts +152 -0
  138. package/source/pick-index-signature.d.ts +52 -0
  139. package/source/primitive.d.ts +15 -0
  140. package/source/promisable.d.ts +27 -0
  141. package/source/readonly-deep.d.ts +116 -0
  142. package/source/readonly-keys-of.d.ts +38 -0
  143. package/source/readonly-tuple.d.ts +34 -0
  144. package/source/remove-prefix.d.ts +132 -0
  145. package/source/replace.d.ts +87 -0
  146. package/source/require-all-or-none.d.ts +54 -0
  147. package/source/require-at-least-one.d.ts +50 -0
  148. package/source/require-exactly-one.d.ts +48 -0
  149. package/source/require-one-or-none.d.ts +49 -0
  150. package/source/required-deep.d.ts +76 -0
  151. package/source/required-keys-of.d.ts +38 -0
  152. package/source/schema.d.ts +119 -0
  153. package/source/screaming-snake-case.d.ts +30 -0
  154. package/source/set-field-type.d.ts +67 -0
  155. package/source/set-non-nullable-deep.d.ts +85 -0
  156. package/source/set-non-nullable.d.ts +41 -0
  157. package/source/set-optional.d.ts +46 -0
  158. package/source/set-parameter-type.d.ts +125 -0
  159. package/source/set-readonly.d.ts +44 -0
  160. package/source/set-required-deep.d.ts +64 -0
  161. package/source/set-required.d.ts +79 -0
  162. package/source/set-return-type.d.ts +31 -0
  163. package/source/shared-union-fields-deep.d.ts +180 -0
  164. package/source/shared-union-fields.d.ts +79 -0
  165. package/source/simplify-deep.d.ts +117 -0
  166. package/source/simplify.d.ts +61 -0
  167. package/source/single-key-object.d.ts +28 -0
  168. package/source/snake-case.d.ts +47 -0
  169. package/source/snake-cased-properties-deep.d.ts +65 -0
  170. package/source/snake-cased-properties.d.ts +42 -0
  171. package/source/split-on-rest-element.d.ts +106 -0
  172. package/source/split.d.ts +90 -0
  173. package/source/spread.d.ts +82 -0
  174. package/source/string-repeat.d.ts +49 -0
  175. package/source/string-slice.d.ts +39 -0
  176. package/source/stringified.d.ts +25 -0
  177. package/source/structured-cloneable.d.ts +89 -0
  178. package/source/subtract.d.ts +86 -0
  179. package/source/sum.d.ts +81 -0
  180. package/source/tagged-union.d.ts +53 -0
  181. package/source/tagged.d.ts +263 -0
  182. package/source/trim.d.ts +29 -0
  183. package/source/tsconfig-json.d.ts +1324 -0
  184. package/source/tuple-of.d.ts +80 -0
  185. package/source/tuple-to-object.d.ts +47 -0
  186. package/source/tuple-to-union.d.ts +54 -0
  187. package/source/typed-array.d.ts +19 -0
  188. package/source/undefined-on-partial-deep.d.ts +83 -0
  189. package/source/union-to-intersection.d.ts +35 -0
  190. package/source/union-to-tuple.d.ts +58 -0
  191. package/source/unknown-array.d.ts +27 -0
  192. package/source/unknown-map.d.ts +26 -0
  193. package/source/unknown-record.d.ts +33 -0
  194. package/source/unknown-set.d.ts +26 -0
  195. package/source/unwrap-partial.d.ts +33 -0
  196. package/source/value-of.d.ts +24 -0
  197. package/source/words.d.ts +122 -0
  198. package/source/writable-deep.d.ts +84 -0
  199. package/source/writable-keys-of.d.ts +34 -0
  200. package/source/writable.d.ts +72 -0
  201. package/source/xor.d.ts +83 -0
@@ -0,0 +1,54 @@
1
+ import type {If} from './if.d.ts';
2
+ import type {IfNotAnyOrNever, RequireNone} from './internal/index.d.ts';
3
+ import type {IsAny} from './is-any.d.ts';
4
+ import type {IsNever} from './is-never.d.ts';
5
+
6
+ /**
7
+ Requires all of the keys in the given object.
8
+ */
9
+ type RequireAll<ObjectType, KeysType extends keyof ObjectType> = Required<Pick<ObjectType, KeysType>>;
10
+
11
+ /**
12
+ Create a type that requires all of the given keys or none of the given keys. The remaining keys are kept as is.
13
+
14
+ Use-cases:
15
+ - Creating interfaces for components with mutually-inclusive keys.
16
+
17
+ The caveat with `RequireAllOrNone` is that TypeScript doesn't always know at compile time every key that will exist at runtime. Therefore `RequireAllOrNone` can't do anything to prevent extra keys it doesn't know about.
18
+
19
+ @example
20
+ ```
21
+ import type {RequireAllOrNone} from 'type-fest';
22
+
23
+ type Responder = {
24
+ text?: () => string;
25
+ json?: () => string;
26
+ secure: boolean;
27
+ };
28
+
29
+ const responder1: RequireAllOrNone<Responder, 'text' | 'json'> = {
30
+ secure: true,
31
+ };
32
+
33
+ const responder2: RequireAllOrNone<Responder, 'text' | 'json'> = {
34
+ text: () => '{"message": "hi"}',
35
+ json: () => '{"message": "ok"}',
36
+ secure: true,
37
+ };
38
+ ```
39
+
40
+ @category Object
41
+ */
42
+ export type RequireAllOrNone<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> =
43
+ IfNotAnyOrNever<ObjectType,
44
+ If<IsNever<KeysType>,
45
+ ObjectType,
46
+ _RequireAllOrNone<ObjectType, If<IsAny<KeysType>, keyof ObjectType, KeysType>>
47
+ >>;
48
+
49
+ type _RequireAllOrNone<ObjectType, KeysType extends keyof ObjectType> = (
50
+ | RequireAll<ObjectType, KeysType>
51
+ | RequireNone<KeysType>
52
+ ) & Omit<ObjectType, KeysType>; // The rest of the keys.
53
+
54
+ export {};
@@ -0,0 +1,50 @@
1
+ import type {Except} from './except.d.ts';
2
+ import type {If} from './if.d.ts';
3
+ import type {IfNotAnyOrNever} from './internal/index.d.ts';
4
+ import type {IsAny} from './is-any.d.ts';
5
+ import type {IsNever} from './is-never.d.ts';
6
+
7
+ /**
8
+ Create a type that requires at least one of the given keys. The remaining keys are kept as is.
9
+
10
+ @example
11
+ ```
12
+ import type {RequireAtLeastOne} from 'type-fest';
13
+
14
+ type Responder = {
15
+ text?: () => string;
16
+ json?: () => string;
17
+ secure?: boolean;
18
+ };
19
+
20
+ const responder: RequireAtLeastOne<Responder, 'text' | 'json'> = {
21
+ json: () => '{"message": "ok"}',
22
+ secure: true,
23
+ };
24
+ ```
25
+
26
+ @category Object
27
+ */
28
+ export type RequireAtLeastOne<
29
+ ObjectType,
30
+ KeysType extends keyof ObjectType = keyof ObjectType,
31
+ > =
32
+ IfNotAnyOrNever<ObjectType,
33
+ If<IsNever<KeysType>,
34
+ never,
35
+ _RequireAtLeastOne<ObjectType, If<IsAny<KeysType>, keyof ObjectType, KeysType>>
36
+ >>;
37
+
38
+ type _RequireAtLeastOne<
39
+ ObjectType,
40
+ KeysType extends keyof ObjectType,
41
+ > = {
42
+ // For each `Key` in `KeysType` make a mapped type:
43
+ [Key in KeysType]-?: Required<Pick<ObjectType, Key>> & // 1. Make `Key`'s type required
44
+ // 2. Make all other keys in `KeysType` optional
45
+ Partial<Pick<ObjectType, Exclude<KeysType, Key>>>;
46
+ }[KeysType] &
47
+ // 3. Add the remaining keys not in `KeysType`
48
+ Except<ObjectType, KeysType>;
49
+
50
+ export {};
@@ -0,0 +1,48 @@
1
+ import type {If} from './if.d.ts';
2
+ import type {IfNotAnyOrNever} from './internal/index.d.ts';
3
+ import type {IsAny} from './is-any.d.ts';
4
+ import type {IsNever} from './is-never.d.ts';
5
+
6
+ /**
7
+ Create a type that requires exactly one of the given keys and disallows more. The remaining keys are kept as is.
8
+
9
+ Use-cases:
10
+ - Creating interfaces for components that only need one of the keys to display properly.
11
+ - Declaring generic keys in a single place for a single use-case that gets narrowed down via `RequireExactlyOne`.
12
+
13
+ The caveat with `RequireExactlyOne` is that TypeScript doesn't always know at compile time every key that will exist at runtime. Therefore `RequireExactlyOne` can't do anything to prevent extra keys it doesn't know about.
14
+
15
+ @example
16
+ ```
17
+ import type {RequireExactlyOne} from 'type-fest';
18
+
19
+ type Responder = {
20
+ text: () => string;
21
+ json: () => string;
22
+ secure: boolean;
23
+ };
24
+
25
+ const responder: RequireExactlyOne<Responder, 'text' | 'json'> = {
26
+ // Adding a `text` key here would cause a compile error.
27
+
28
+ json: () => '{"message": "ok"}',
29
+ secure: true,
30
+ };
31
+ ```
32
+
33
+ @category Object
34
+ */
35
+ export type RequireExactlyOne<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> =
36
+ IfNotAnyOrNever<ObjectType,
37
+ If<IsNever<KeysType>,
38
+ never,
39
+ _RequireExactlyOne<ObjectType, If<IsAny<KeysType>, keyof ObjectType, KeysType>>
40
+ >>;
41
+
42
+ type _RequireExactlyOne<ObjectType, KeysType extends keyof ObjectType> =
43
+ {[Key in KeysType]: (
44
+ Required<Pick<ObjectType, Key>> &
45
+ Partial<Record<Exclude<KeysType, Key>, never>>
46
+ )}[KeysType] & Omit<ObjectType, KeysType>;
47
+
48
+ export {};
@@ -0,0 +1,49 @@
1
+ import type {RequireExactlyOne} from './require-exactly-one.d.ts';
2
+ import type {IfNotAnyOrNever, RequireNone} from './internal/index.d.ts';
3
+ import type {If} from './if.d.ts';
4
+ import type {IsAny} from './is-any.d.ts';
5
+ import type {IsNever} from './is-never.d.ts';
6
+
7
+ /**
8
+ Create a type that requires exactly one of the given keys and disallows more, or none of the given keys. The remaining keys are kept as is.
9
+
10
+ @example
11
+ ```
12
+ import type {RequireOneOrNone} from 'type-fest';
13
+
14
+ type Responder = RequireOneOrNone<{
15
+ text: () => string;
16
+ json: () => string;
17
+ secure: boolean;
18
+ }, 'text' | 'json'>;
19
+
20
+ const responder1: Responder = {
21
+ secure: true,
22
+ };
23
+
24
+ const responder2: Responder = {
25
+ text: () => '{"message": "hi"}',
26
+ secure: true,
27
+ };
28
+
29
+ const responder3: Responder = {
30
+ json: () => '{"message": "ok"}',
31
+ secure: true,
32
+ };
33
+ ```
34
+
35
+ @category Object
36
+ */
37
+ export type RequireOneOrNone<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> =
38
+ IfNotAnyOrNever<ObjectType,
39
+ If<IsNever<KeysType>,
40
+ ObjectType,
41
+ _RequireOneOrNone<ObjectType, If<IsAny<KeysType>, keyof ObjectType, KeysType>>
42
+ >>;
43
+
44
+ type _RequireOneOrNone<ObjectType, KeysType extends keyof ObjectType> = (
45
+ | RequireExactlyOne<ObjectType, KeysType>
46
+ | RequireNone<KeysType>
47
+ ) & Omit<ObjectType, KeysType>; // Ignore unspecified keys.
48
+
49
+ export {};
@@ -0,0 +1,76 @@
1
+ import type {BuiltIns, HasMultipleCallSignatures} from './internal/index.d.ts';
2
+ import type {IsNever} from './is-never.d.ts';
3
+ import type {Simplify} from './simplify.d.ts';
4
+
5
+ /**
6
+ Create a type from another type with all keys and nested keys set to required.
7
+
8
+ Use-cases:
9
+ - Creating optional configuration interfaces where the underlying implementation still requires all options to be fully specified.
10
+ - Modeling the resulting type after a deep merge with a set of defaults.
11
+
12
+ @example
13
+ ```
14
+ import type {RequiredDeep} from 'type-fest';
15
+
16
+ type Settings = {
17
+ textEditor?: {
18
+ fontSize?: number;
19
+ fontColor?: string;
20
+ fontWeight?: number | undefined;
21
+ };
22
+ autocomplete?: boolean;
23
+ autosave?: boolean | undefined;
24
+ };
25
+
26
+ type RequiredSettings = RequiredDeep<Settings>;
27
+ //=> {
28
+ // textEditor: {
29
+ // fontSize: number;
30
+ // fontColor: string;
31
+ // fontWeight: number | undefined;
32
+ // };
33
+ // autocomplete: boolean;
34
+ // autosave: boolean | undefined;
35
+ // }
36
+ ```
37
+
38
+ Note that types containing overloaded functions are not made deeply required due to a [TypeScript limitation](https://github.com/microsoft/TypeScript/issues/29732).
39
+
40
+ @category Utilities
41
+ @category Object
42
+ @category Array
43
+ @category Set
44
+ @category Map
45
+ */
46
+ export type RequiredDeep<T> = T extends BuiltIns
47
+ ? T
48
+ : T extends Map<infer KeyType, infer ValueType>
49
+ ? Map<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
50
+ : T extends Set<infer ItemType>
51
+ ? Set<RequiredDeep<ItemType>>
52
+ : T extends ReadonlyMap<infer KeyType, infer ValueType>
53
+ ? ReadonlyMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
54
+ : T extends ReadonlySet<infer ItemType>
55
+ ? ReadonlySet<RequiredDeep<ItemType>>
56
+ : T extends WeakMap<infer KeyType, infer ValueType>
57
+ ? WeakMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
58
+ : T extends WeakSet<infer ItemType>
59
+ ? WeakSet<RequiredDeep<ItemType>>
60
+ : T extends Promise<infer ValueType>
61
+ ? Promise<RequiredDeep<ValueType>>
62
+ : T extends (...arguments_: any[]) => unknown
63
+ ? IsNever<keyof T> extends true
64
+ ? T
65
+ : HasMultipleCallSignatures<T> extends true
66
+ ? T
67
+ : ((...arguments_: Parameters<T>) => ReturnType<T>) & RequiredObjectDeep<T>
68
+ : T extends object
69
+ ? Simplify<RequiredObjectDeep<T>> // `Simplify` to prevent `RequiredObjectDeep` from appearing in the resulting type
70
+ : unknown;
71
+
72
+ type RequiredObjectDeep<ObjectType extends object> = {
73
+ [KeyType in keyof ObjectType]-?: RequiredDeep<ObjectType[KeyType]>
74
+ };
75
+
76
+ export {};
@@ -0,0 +1,38 @@
1
+ import type {OptionalKeysOf} from './optional-keys-of.d.ts';
2
+
3
+ /**
4
+ Extract all required keys from the given type.
5
+
6
+ This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
7
+
8
+ @example
9
+ ```
10
+ import type {RequiredKeysOf} from 'type-fest';
11
+
12
+ declare function createValidation<
13
+ Entity extends object,
14
+ Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>,
15
+ >(field: Key, validator: (value: Entity[Key]) => boolean): (entity: Entity) => boolean;
16
+
17
+ type User = {
18
+ name: string;
19
+ surname: string;
20
+ luckyNumber?: number;
21
+ };
22
+
23
+ const validator1 = createValidation<User>('name', value => value.length < 25);
24
+ const validator2 = createValidation<User>('surname', value => value.length < 25);
25
+
26
+ // @ts-expect-error
27
+ const validator3 = createValidation<User>('luckyNumber', value => value > 0);
28
+ // Error: Argument of type '"luckyNumber"' is not assignable to parameter of type '"name" | "surname"'.
29
+ ```
30
+
31
+ @category Utilities
32
+ */
33
+ export type RequiredKeysOf<Type extends object> =
34
+ Type extends unknown // For distributing `Type`
35
+ ? Exclude<keyof Type, OptionalKeysOf<Type>>
36
+ : never; // Should never happen
37
+
38
+ export {};
@@ -0,0 +1,119 @@
1
+ import type {ApplyDefaultOptions} from './internal/object.d.ts';
2
+ import type {IfNotAnyOrNever, NonRecursiveType} from './internal/type.d.ts';
3
+ import type {OptionalKeysOf} from './optional-keys-of.d.ts';
4
+ import type {Simplify} from './simplify.d.ts';
5
+ import type {UnknownArray} from './unknown-array.d.ts';
6
+
7
+ /**
8
+ @see {@link Schema}
9
+ */
10
+ export type SchemaOptions = {
11
+ /**
12
+ By default, this affects elements in array and tuple types. You can change this by passing `{recurseIntoArrays: false}` as the third type argument:
13
+ - If `recurseIntoArrays` is set to `true` (default), array elements will be recursively processed as well.
14
+ - If `recurseIntoArrays` is set to `false`, arrays will not be recursively processed, and the entire array will be replaced with the given value type.
15
+
16
+ @example
17
+ ```
18
+ import type {Schema} from 'type-fest';
19
+
20
+ type Participants = {
21
+ attendees: string[];
22
+ speakers: string[];
23
+ };
24
+
25
+ type ParticipantsWithMetadata = Schema<Participants, {id: number; name: string}, {recurseIntoArrays: true}>;
26
+ //=> {
27
+ // attendees: {
28
+ // id: number;
29
+ // name: string;
30
+ // }[];
31
+ // speakers: {
32
+ // id: number;
33
+ // name: string;
34
+ // }[];
35
+ // }
36
+
37
+ type ParticipantsCount = Schema<Participants, number, {recurseIntoArrays: false}>;
38
+ //=> {attendees: number; speakers: number}
39
+ ```
40
+
41
+ @default true
42
+ */
43
+ recurseIntoArrays?: boolean;
44
+ };
45
+
46
+ type DefaultSchemaOptions = {
47
+ recurseIntoArrays: true;
48
+ };
49
+
50
+ /**
51
+ Create a deep version of another object type where property values are recursively replaced into a given value type.
52
+
53
+ Use-cases:
54
+ - Form validation: Define how each field should be validated.
55
+ - Form settings: Define configuration for input fields.
56
+ - Parsing: Define types that specify special behavior for specific fields.
57
+
58
+ @example
59
+ ```
60
+ import type {Schema} from 'type-fest';
61
+
62
+ type User = {
63
+ id: string;
64
+ name: {
65
+ firstname: string;
66
+ lastname: string;
67
+ };
68
+ created: Date;
69
+ active: boolean;
70
+ passwordHash: string;
71
+ location: [latitude: number, longitude: number];
72
+ };
73
+
74
+ type UserMask = Schema<User, 'mask' | 'hide' | 'show'>;
75
+
76
+ const userMaskSettings: UserMask = {
77
+ id: 'show',
78
+ name: {
79
+ firstname: 'show',
80
+ lastname: 'mask',
81
+ },
82
+ created: 'show',
83
+ active: 'show',
84
+ passwordHash: 'hide',
85
+ location: ['hide', 'hide'],
86
+ };
87
+ ```
88
+
89
+ @see {@link SchemaOptions}
90
+
91
+ @category Object
92
+ */
93
+ export type Schema<Type, Value, Options extends SchemaOptions = {}> =
94
+ IfNotAnyOrNever<Type,
95
+ _Schema<Type, Value, ApplyDefaultOptions<SchemaOptions, DefaultSchemaOptions, Options>>,
96
+ Value, Value>;
97
+
98
+ type _Schema<Type, Value, Options extends Required<SchemaOptions>> =
99
+ Type extends NonRecursiveType | Map<unknown, unknown> | Set<unknown> | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
100
+ ? Value
101
+ : Type extends UnknownArray
102
+ ? Options['recurseIntoArrays'] extends false
103
+ ? Value
104
+ : SchemaHelper<Type, Value, Options>
105
+ : SchemaHelper<Type, Value, Options>;
106
+
107
+ /**
108
+ Internal helper for {@link _Schema}.
109
+
110
+ Recursively transforms the value of each property in objects and arrays.
111
+ */
112
+ type SchemaHelper<Type, Value, Options extends Required<SchemaOptions>> = Simplify<{
113
+ [Key in keyof Type]: _Schema<
114
+ Key extends OptionalKeysOf<Type & object> ? Exclude<Type[Key], undefined> : Type[Key], // Remove `| undefined` when accessing optional properties
115
+ Value,
116
+ Options>
117
+ }>;
118
+
119
+ export {};
@@ -0,0 +1,30 @@
1
+ import type {_DefaultDelimiterCaseOptions} from './delimiter-case.d.ts';
2
+ import type {ApplyDefaultOptions} from './internal/index.d.ts';
3
+ import type {SnakeCase} from './snake-case.d.ts';
4
+ import type {WordsOptions} from './words.d.ts';
5
+
6
+ /**
7
+ Convert a string literal to screaming-snake-case.
8
+
9
+ This can be useful when, for example, converting a camel-cased object property to a screaming-snake-cased SQL column name.
10
+
11
+ @example
12
+ ```
13
+ import type {ScreamingSnakeCase} from 'type-fest';
14
+
15
+ const someVariable: ScreamingSnakeCase<'fooBar'> = 'FOO_BAR';
16
+ const someVariableNoSplitOnNumbers: ScreamingSnakeCase<'p2pNetwork', {splitOnNumbers: false}> = 'P2P_NETWORK';
17
+
18
+ ```
19
+
20
+ @category Change case
21
+ @category Template literal
22
+ */
23
+ export type ScreamingSnakeCase<
24
+ Value,
25
+ Options extends WordsOptions = {},
26
+ > = Value extends string
27
+ ? Uppercase<SnakeCase<Value, ApplyDefaultOptions<WordsOptions, _DefaultDelimiterCaseOptions, Options>>>
28
+ : Value;
29
+
30
+ export {};
@@ -0,0 +1,67 @@
1
+ import type {ApplyDefaultOptions} from './internal/index.d.ts';
2
+ import type {Simplify} from './simplify.d.ts';
3
+
4
+ export type SetFieldTypeOptions = {
5
+ /**
6
+ Preserve optional and readonly modifiers for properties being updated.
7
+
8
+ NOTE: Property modifiers will always be preserved for properties that are not being updated.
9
+
10
+ @default true
11
+ */
12
+ preservePropertyModifiers?: boolean;
13
+ };
14
+
15
+ type DefaultSetFieldTypeOptions = {
16
+ preservePropertyModifiers: true;
17
+ };
18
+
19
+ /**
20
+ Create a type that changes the type of the given keys.
21
+
22
+ Use-cases:
23
+ - Creating variations of a base model.
24
+ - Fixing incorrect external types.
25
+
26
+ @see `Merge` if you need to change multiple properties to different types.
27
+
28
+ @example
29
+ ```
30
+ import type {SetFieldType} from 'type-fest';
31
+
32
+ type MyModel = {
33
+ readonly id: number;
34
+ readonly createdAt: Date;
35
+ updatedAt?: Date;
36
+ };
37
+
38
+ type MyModelApi1 = SetFieldType<MyModel, 'createdAt' | 'updatedAt', string>;
39
+ // {
40
+ // readonly id: number;
41
+ // readonly createdAt: string;
42
+ // updatedAt?: string;
43
+ // }
44
+
45
+ // `preservePropertyModifiers` option can be set to `false` if you want to remove property modifiers for properties being updated
46
+ type MyModelApi2 = SetFieldType<MyModel, 'createdAt' | 'updatedAt', string, {preservePropertyModifiers: false}>;
47
+ // {
48
+ // readonly id: number;
49
+ // createdAt: string; // no longer readonly
50
+ // updatedAt: string; // no longer optional
51
+ // }
52
+ ```
53
+
54
+ @category Object
55
+ */
56
+ export type SetFieldType<BaseType, Keys extends keyof BaseType, NewType, Options extends SetFieldTypeOptions = {}> =
57
+ _SetFieldType<BaseType, Keys, NewType, ApplyDefaultOptions<SetFieldTypeOptions, DefaultSetFieldTypeOptions, Options>>;
58
+
59
+ type _SetFieldType<BaseType, Keys extends keyof BaseType, NewType, Options extends Required<SetFieldTypeOptions>> =
60
+ Simplify<{
61
+ [P in keyof BaseType]: P extends Keys ? NewType : BaseType[P];
62
+ } & (
63
+ // `Record` is used to remove property modifiers
64
+ Options['preservePropertyModifiers'] extends false ? Record<Keys, NewType> : unknown
65
+ )>;
66
+
67
+ export {};
@@ -0,0 +1,85 @@
1
+ import type {NonRecursiveType, StringToNumber} from './internal/index.d.ts';
2
+ import type {Paths} from './paths.d.ts';
3
+ import type {SetNonNullable} from './set-non-nullable.d.ts';
4
+ import type {Simplify} from './simplify.d.ts';
5
+ import type {UnionToTuple} from './union-to-tuple.d.ts';
6
+ import type {UnknownArray} from './unknown-array.d.ts';
7
+
8
+ /**
9
+ Create a type that makes the specified keys non-nullable (removes `null` and `undefined`), supports deeply nested key paths, and leaves all other keys unchanged.
10
+
11
+ NOTE: Optional modifiers (`?`) are not removed from properties. For example, `SetNonNullableDeep<{foo?: string | null | undefined}, 'foo'>` will result in `{foo?: string}`.
12
+
13
+ @example
14
+ ```
15
+ import type {SetNonNullableDeep} from 'type-fest';
16
+
17
+ type User = {
18
+ name: string;
19
+ address: {
20
+ city: string | undefined;
21
+ street?: string | null;
22
+ };
23
+ contact: {
24
+ email?: string | null | undefined;
25
+ phone: string | undefined;
26
+ };
27
+ };
28
+
29
+ type UpdatedUser = SetNonNullableDeep<User, 'address.street' | 'contact.email' | 'contact.phone'>;
30
+ //=> {
31
+ // name: string;
32
+ // address: {
33
+ // city: string | undefined;
34
+ // street?: string;
35
+ // };
36
+ // contact: {
37
+ // email?: string;
38
+ // phone: string;
39
+ // };
40
+ // }
41
+ ```
42
+
43
+ @example
44
+ ```
45
+ import type {SetNonNullableDeep} from 'type-fest';
46
+
47
+ // Set specific indices in an array to be non-nullable.
48
+ type ArrayExample1 = SetNonNullableDeep<{a: [number | null, number | null, number | undefined]}, 'a.1' | 'a.2'>;
49
+ //=> {a: [number | null, number, number]}
50
+
51
+ // Optional modifier (`?`) is not removed.
52
+ type ArrayExample2 = SetNonNullableDeep<{a: [(number | null)?, (number | null)?]}, 'a.1'>;
53
+ //=> {a: [(number | null)?, number?]}
54
+ ```
55
+
56
+ @category Object
57
+ */
58
+ export type SetNonNullableDeep<BaseType, KeyPaths extends Paths<BaseType>> =
59
+ SetNonNullableDeepHelper<BaseType, UnionToTuple<KeyPaths>>;
60
+
61
+ /**
62
+ Internal helper for {@link SetNonNullableDeep}.
63
+
64
+ Recursively transforms the `BaseType` by applying {@link SetNonNullableDeepSinglePath} for each path in `KeyPathsTuple`.
65
+ */
66
+ type SetNonNullableDeepHelper<BaseType, KeyPathsTuple extends UnknownArray> =
67
+ KeyPathsTuple extends [infer KeyPath, ...infer RestPaths]
68
+ ? SetNonNullableDeepHelper<SetNonNullableDeepSinglePath<BaseType, KeyPath>, RestPaths>
69
+ : BaseType;
70
+
71
+ /**
72
+ Makes a single path non-nullable in `BaseType`.
73
+ */
74
+ type SetNonNullableDeepSinglePath<BaseType, KeyPath> =
75
+ BaseType extends NonRecursiveType | ReadonlySet<unknown> | ReadonlyMap<unknown, unknown> // Also distributes `BaseType`
76
+ ? BaseType
77
+ : KeyPath extends `${infer Property}.${infer RestPath}`
78
+ ? {
79
+ [Key in keyof BaseType]: Property extends `${Key & (string | number)}`
80
+ ? SetNonNullableDeepSinglePath<BaseType[Key], RestPath>
81
+ : BaseType[Key];
82
+ }
83
+ : Simplify<SetNonNullable<BaseType, (KeyPath | StringToNumber<KeyPath & string>) & keyof BaseType>>;
84
+
85
+ export {};
@@ -0,0 +1,41 @@
1
+ /**
2
+ Create a type that makes the given keys non-nullable, where the remaining keys are kept as is.
3
+
4
+ If no keys are given, all keys will be made non-nullable.
5
+
6
+ Use-case: You want to define a single model where the only thing that changes is whether or not some or all of the keys are non-nullable.
7
+
8
+ @example
9
+ ```
10
+ import type {SetNonNullable} from 'type-fest';
11
+
12
+ type Foo = {
13
+ a: number | null;
14
+ b: string | undefined;
15
+ c?: boolean | null;
16
+ };
17
+
18
+ type SomeNonNullable = SetNonNullable<Foo, 'b' | 'c'>;
19
+ // type SomeNonNullable = {
20
+ // a: number | null;
21
+ // b: string; // Can no longer be undefined.
22
+ // c?: boolean; // Can no longer be null, but is still optional.
23
+ // }
24
+
25
+ type AllNonNullable = SetNonNullable<Foo>;
26
+ // type AllNonNullable = {
27
+ // a: number; // Can no longer be null.
28
+ // b: string; // Can no longer be undefined.
29
+ // c?: boolean; // Can no longer be null, but is still optional.
30
+ // }
31
+ ```
32
+
33
+ @category Object
34
+ */
35
+ export type SetNonNullable<BaseType, Keys extends keyof BaseType = keyof BaseType> = {
36
+ [Key in keyof BaseType]: Key extends Keys
37
+ ? NonNullable<BaseType[Key]>
38
+ : BaseType[Key];
39
+ };
40
+
41
+ export {};