ts-fortress 1.0.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 (318) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +324 -0
  3. package/dist/array/array-of-length.d.mts +6 -0
  4. package/dist/array/array-of-length.d.mts.map +1 -0
  5. package/dist/array/array-of-length.mjs +50 -0
  6. package/dist/array/array-of-length.mjs.map +1 -0
  7. package/dist/array/array.d.mts +6 -0
  8. package/dist/array/array.d.mts.map +1 -0
  9. package/dist/array/array.mjs +39 -0
  10. package/dist/array/array.mjs.map +1 -0
  11. package/dist/array/index.d.mts +5 -0
  12. package/dist/array/index.d.mts.map +1 -0
  13. package/dist/array/index.mjs +5 -0
  14. package/dist/array/index.mjs.map +1 -0
  15. package/dist/array/non-empty-array.d.mts +6 -0
  16. package/dist/array/non-empty-array.d.mts.map +1 -0
  17. package/dist/array/non-empty-array.mjs +46 -0
  18. package/dist/array/non-empty-array.mjs.map +1 -0
  19. package/dist/array/tuple.d.mts +9 -0
  20. package/dist/array/tuple.d.mts.map +1 -0
  21. package/dist/array/tuple.mjs +47 -0
  22. package/dist/array/tuple.mjs.map +1 -0
  23. package/dist/branded/brand.d.mts +11 -0
  24. package/dist/branded/brand.d.mts.map +1 -0
  25. package/dist/branded/brand.mjs +29 -0
  26. package/dist/branded/brand.mjs.map +1 -0
  27. package/dist/branded/index.d.mts +5 -0
  28. package/dist/branded/index.d.mts.map +1 -0
  29. package/dist/branded/index.mjs +20 -0
  30. package/dist/branded/index.mjs.map +1 -0
  31. package/dist/branded/number/finite-number.d.mts +3 -0
  32. package/dist/branded/number/finite-number.d.mts.map +1 -0
  33. package/dist/branded/number/finite-number.mjs +14 -0
  34. package/dist/branded/number/finite-number.mjs.map +1 -0
  35. package/dist/branded/number/index.d.mts +17 -0
  36. package/dist/branded/number/index.d.mts.map +1 -0
  37. package/dist/branded/number/index.mjs +17 -0
  38. package/dist/branded/number/index.mjs.map +1 -0
  39. package/dist/branded/number/int.d.mts +3 -0
  40. package/dist/branded/number/int.d.mts.map +1 -0
  41. package/dist/branded/number/int.mjs +14 -0
  42. package/dist/branded/number/int.mjs.map +1 -0
  43. package/dist/branded/number/int16.d.mts +3 -0
  44. package/dist/branded/number/int16.d.mts.map +1 -0
  45. package/dist/branded/number/int16.mjs +26 -0
  46. package/dist/branded/number/int16.mjs.map +1 -0
  47. package/dist/branded/number/int32.d.mts +3 -0
  48. package/dist/branded/number/int32.d.mts.map +1 -0
  49. package/dist/branded/number/int32.mjs +22 -0
  50. package/dist/branded/number/int32.mjs.map +1 -0
  51. package/dist/branded/number/non-negative-finite-number.d.mts +3 -0
  52. package/dist/branded/number/non-negative-finite-number.d.mts.map +1 -0
  53. package/dist/branded/number/non-negative-finite-number.mjs +14 -0
  54. package/dist/branded/number/non-negative-finite-number.mjs.map +1 -0
  55. package/dist/branded/number/non-zero-finite-number.d.mts +3 -0
  56. package/dist/branded/number/non-zero-finite-number.d.mts.map +1 -0
  57. package/dist/branded/number/non-zero-finite-number.mjs +14 -0
  58. package/dist/branded/number/non-zero-finite-number.mjs.map +1 -0
  59. package/dist/branded/number/non-zero-int.d.mts +3 -0
  60. package/dist/branded/number/non-zero-int.d.mts.map +1 -0
  61. package/dist/branded/number/non-zero-int.mjs +14 -0
  62. package/dist/branded/number/non-zero-int.mjs.map +1 -0
  63. package/dist/branded/number/non-zero-safe-int.d.mts +3 -0
  64. package/dist/branded/number/non-zero-safe-int.d.mts.map +1 -0
  65. package/dist/branded/number/non-zero-safe-int.mjs +14 -0
  66. package/dist/branded/number/non-zero-safe-int.mjs.map +1 -0
  67. package/dist/branded/number/positive-finite-number.d.mts +3 -0
  68. package/dist/branded/number/positive-finite-number.d.mts.map +1 -0
  69. package/dist/branded/number/positive-finite-number.mjs +22 -0
  70. package/dist/branded/number/positive-finite-number.mjs.map +1 -0
  71. package/dist/branded/number/positive-int.d.mts +3 -0
  72. package/dist/branded/number/positive-int.d.mts.map +1 -0
  73. package/dist/branded/number/positive-int.mjs +23 -0
  74. package/dist/branded/number/positive-int.mjs.map +1 -0
  75. package/dist/branded/number/positive-safe-int.d.mts +3 -0
  76. package/dist/branded/number/positive-safe-int.d.mts.map +1 -0
  77. package/dist/branded/number/positive-safe-int.mjs +24 -0
  78. package/dist/branded/number/positive-safe-int.mjs.map +1 -0
  79. package/dist/branded/number/safe-int.d.mts +3 -0
  80. package/dist/branded/number/safe-int.d.mts.map +1 -0
  81. package/dist/branded/number/safe-int.mjs +14 -0
  82. package/dist/branded/number/safe-int.mjs.map +1 -0
  83. package/dist/branded/number/safe-uint.d.mts +3 -0
  84. package/dist/branded/number/safe-uint.d.mts.map +1 -0
  85. package/dist/branded/number/safe-uint.mjs +23 -0
  86. package/dist/branded/number/safe-uint.mjs.map +1 -0
  87. package/dist/branded/number/uint.d.mts +3 -0
  88. package/dist/branded/number/uint.d.mts.map +1 -0
  89. package/dist/branded/number/uint.mjs +22 -0
  90. package/dist/branded/number/uint.mjs.map +1 -0
  91. package/dist/branded/number/uint16.d.mts +3 -0
  92. package/dist/branded/number/uint16.d.mts.map +1 -0
  93. package/dist/branded/number/uint16.mjs +26 -0
  94. package/dist/branded/number/uint16.mjs.map +1 -0
  95. package/dist/branded/number/uint32.d.mts +3 -0
  96. package/dist/branded/number/uint32.d.mts.map +1 -0
  97. package/dist/branded/number/uint32.mjs +24 -0
  98. package/dist/branded/number/uint32.mjs.map +1 -0
  99. package/dist/branded/simple-branded-number.d.mts +3 -0
  100. package/dist/branded/simple-branded-number.d.mts.map +1 -0
  101. package/dist/branded/simple-branded-number.mjs +15 -0
  102. package/dist/branded/simple-branded-number.mjs.map +1 -0
  103. package/dist/branded/simple-branded-string.d.mts +3 -0
  104. package/dist/branded/simple-branded-string.d.mts.map +1 -0
  105. package/dist/branded/simple-branded-string.mjs +15 -0
  106. package/dist/branded/simple-branded-string.mjs.map +1 -0
  107. package/dist/compose/index.d.mts +4 -0
  108. package/dist/compose/index.d.mts.map +1 -0
  109. package/dist/compose/index.mjs +4 -0
  110. package/dist/compose/index.mjs.map +1 -0
  111. package/dist/compose/intersection.d.mts +19 -0
  112. package/dist/compose/intersection.d.mts.map +1 -0
  113. package/dist/compose/intersection.mjs +38 -0
  114. package/dist/compose/intersection.mjs.map +1 -0
  115. package/dist/compose/merge-records.d.mts +15 -0
  116. package/dist/compose/merge-records.d.mts.map +1 -0
  117. package/dist/compose/merge-records.mjs +48 -0
  118. package/dist/compose/merge-records.mjs.map +1 -0
  119. package/dist/compose/union.d.mts +14 -0
  120. package/dist/compose/union.d.mts.map +1 -0
  121. package/dist/compose/union.mjs +33 -0
  122. package/dist/compose/union.mjs.map +1 -0
  123. package/dist/enum/enum.d.mts +6 -0
  124. package/dist/enum/enum.d.mts.map +1 -0
  125. package/dist/enum/enum.mjs +34 -0
  126. package/dist/enum/enum.mjs.map +1 -0
  127. package/dist/enum/index.d.mts +3 -0
  128. package/dist/enum/index.d.mts.map +1 -0
  129. package/dist/enum/index.mjs +3 -0
  130. package/dist/enum/index.mjs.map +1 -0
  131. package/dist/enum/uint-range.d.mts +8 -0
  132. package/dist/enum/uint-range.d.mts.map +1 -0
  133. package/dist/enum/uint-range.mjs +40 -0
  134. package/dist/enum/uint-range.mjs.map +1 -0
  135. package/dist/globals.d.mts +3 -0
  136. package/dist/index.d.mts +10 -0
  137. package/dist/index.d.mts.map +1 -0
  138. package/dist/index.mjs +50 -0
  139. package/dist/index.mjs.map +1 -0
  140. package/dist/primitives/bigint.d.mts +4 -0
  141. package/dist/primitives/bigint.d.mts.map +1 -0
  142. package/dist/primitives/bigint.mjs +12 -0
  143. package/dist/primitives/bigint.mjs.map +1 -0
  144. package/dist/primitives/boolean.d.mts +4 -0
  145. package/dist/primitives/boolean.d.mts.map +1 -0
  146. package/dist/primitives/boolean.mjs +12 -0
  147. package/dist/primitives/boolean.mjs.map +1 -0
  148. package/dist/primitives/index.d.mts +8 -0
  149. package/dist/primitives/index.d.mts.map +1 -0
  150. package/dist/primitives/index.mjs +8 -0
  151. package/dist/primitives/index.mjs.map +1 -0
  152. package/dist/primitives/null.d.mts +3 -0
  153. package/dist/primitives/null.d.mts.map +1 -0
  154. package/dist/primitives/null.mjs +11 -0
  155. package/dist/primitives/null.mjs.map +1 -0
  156. package/dist/primitives/number.d.mts +4 -0
  157. package/dist/primitives/number.d.mts.map +1 -0
  158. package/dist/primitives/number.mjs +12 -0
  159. package/dist/primitives/number.mjs.map +1 -0
  160. package/dist/primitives/string.d.mts +4 -0
  161. package/dist/primitives/string.d.mts.map +1 -0
  162. package/dist/primitives/string.mjs +12 -0
  163. package/dist/primitives/string.mjs.map +1 -0
  164. package/dist/primitives/symbol.d.mts +3 -0
  165. package/dist/primitives/symbol.d.mts.map +1 -0
  166. package/dist/primitives/symbol.mjs +11 -0
  167. package/dist/primitives/symbol.mjs.map +1 -0
  168. package/dist/primitives/undefined.d.mts +3 -0
  169. package/dist/primitives/undefined.d.mts.map +1 -0
  170. package/dist/primitives/undefined.mjs +11 -0
  171. package/dist/primitives/undefined.mjs.map +1 -0
  172. package/dist/record/index.d.mts +8 -0
  173. package/dist/record/index.d.mts.map +1 -0
  174. package/dist/record/index.mjs +8 -0
  175. package/dist/record/index.mjs.map +1 -0
  176. package/dist/record/key-value-record.d.mts +7 -0
  177. package/dist/record/key-value-record.d.mts.map +1 -0
  178. package/dist/record/key-value-record.mjs +52 -0
  179. package/dist/record/key-value-record.mjs.map +1 -0
  180. package/dist/record/keyof.d.mts +7 -0
  181. package/dist/record/keyof.d.mts.map +1 -0
  182. package/dist/record/keyof.mjs +15 -0
  183. package/dist/record/keyof.mjs.map +1 -0
  184. package/dist/record/omit.d.mts +8 -0
  185. package/dist/record/omit.d.mts.map +1 -0
  186. package/dist/record/omit.mjs +39 -0
  187. package/dist/record/omit.mjs.map +1 -0
  188. package/dist/record/optional.d.mts +5 -0
  189. package/dist/record/optional.d.mts.map +1 -0
  190. package/dist/record/optional.mjs +8 -0
  191. package/dist/record/optional.mjs.map +1 -0
  192. package/dist/record/partial.d.mts +13 -0
  193. package/dist/record/partial.d.mts.map +1 -0
  194. package/dist/record/partial.mjs +128 -0
  195. package/dist/record/partial.mjs.map +1 -0
  196. package/dist/record/pick.d.mts +8 -0
  197. package/dist/record/pick.d.mts.map +1 -0
  198. package/dist/record/pick.mjs +39 -0
  199. package/dist/record/pick.mjs.map +1 -0
  200. package/dist/record/record.d.mts +21 -0
  201. package/dist/record/record.d.mts.map +1 -0
  202. package/dist/record/record.mjs +53 -0
  203. package/dist/record/record.mjs.map +1 -0
  204. package/dist/tsconfig.json +1 -0
  205. package/dist/type.d.mts +27 -0
  206. package/dist/type.d.mts.map +1 -0
  207. package/dist/type.mjs +2 -0
  208. package/dist/type.mjs.map +1 -0
  209. package/dist/types.d.mts +2 -0
  210. package/dist/unknown.d.mts +3 -0
  211. package/dist/unknown.d.mts.map +1 -0
  212. package/dist/unknown.mjs +18 -0
  213. package/dist/unknown.mjs.map +1 -0
  214. package/dist/utils/create-assert-fn.d.mts +3 -0
  215. package/dist/utils/create-assert-fn.d.mts.map +1 -0
  216. package/dist/utils/create-assert-fn.mjs +12 -0
  217. package/dist/utils/create-assert-fn.mjs.map +1 -0
  218. package/dist/utils/create-cast-fn.d.mts +3 -0
  219. package/dist/utils/create-cast-fn.d.mts.map +1 -0
  220. package/dist/utils/create-cast-fn.mjs +13 -0
  221. package/dist/utils/create-cast-fn.mjs.map +1 -0
  222. package/dist/utils/create-is-fn.d.mts +3 -0
  223. package/dist/utils/create-is-fn.d.mts.map +1 -0
  224. package/dist/utils/create-is-fn.mjs +6 -0
  225. package/dist/utils/create-is-fn.mjs.map +1 -0
  226. package/dist/utils/create-primitive-type.d.mts +7 -0
  227. package/dist/utils/create-primitive-type.d.mts.map +1 -0
  228. package/dist/utils/create-primitive-type.mjs +24 -0
  229. package/dist/utils/create-primitive-type.mjs.map +1 -0
  230. package/dist/utils/create-type.d.mts +8 -0
  231. package/dist/utils/create-type.d.mts.map +1 -0
  232. package/dist/utils/create-type.mjs +19 -0
  233. package/dist/utils/create-type.mjs.map +1 -0
  234. package/dist/utils/index.d.mts +7 -0
  235. package/dist/utils/index.d.mts.map +1 -0
  236. package/dist/utils/index.mjs +7 -0
  237. package/dist/utils/index.mjs.map +1 -0
  238. package/dist/utils/validation-error-message.d.mts +2 -0
  239. package/dist/utils/validation-error-message.d.mts.map +1 -0
  240. package/dist/utils/validation-error-message.mjs +13 -0
  241. package/dist/utils/validation-error-message.mjs.map +1 -0
  242. package/package.json +102 -0
  243. package/src/array/array-of-length.mts +76 -0
  244. package/src/array/array-of-length.test.mts +113 -0
  245. package/src/array/array.mts +62 -0
  246. package/src/array/array.test.mts +84 -0
  247. package/src/array/index.mts +4 -0
  248. package/src/array/non-empty-array.mts +70 -0
  249. package/src/array/non-empty-array.test.mts +113 -0
  250. package/src/array/tuple.mts +72 -0
  251. package/src/array/tuple.test.mts +122 -0
  252. package/src/branded/brand.mts +70 -0
  253. package/src/branded/index.mts +4 -0
  254. package/src/branded/number/finite-number.mts +15 -0
  255. package/src/branded/number/index.mts +16 -0
  256. package/src/branded/number/int.mts +13 -0
  257. package/src/branded/number/int16.mts +25 -0
  258. package/src/branded/number/int32.mts +21 -0
  259. package/src/branded/number/non-negative-finite-number.mts +18 -0
  260. package/src/branded/number/non-zero-finite-number.mts +15 -0
  261. package/src/branded/number/non-zero-int.mts +15 -0
  262. package/src/branded/number/non-zero-safe-int.mts +15 -0
  263. package/src/branded/number/positive-finite-number.mts +23 -0
  264. package/src/branded/number/positive-int.mts +24 -0
  265. package/src/branded/number/positive-safe-int.mts +25 -0
  266. package/src/branded/number/safe-int.mts +13 -0
  267. package/src/branded/number/safe-uint.mts +24 -0
  268. package/src/branded/number/uint.mts +21 -0
  269. package/src/branded/number/uint16.mts +25 -0
  270. package/src/branded/number/uint32.mts +23 -0
  271. package/src/branded/simple-branded-number.mts +18 -0
  272. package/src/branded/simple-branded-string.mts +18 -0
  273. package/src/combination.test.mts +287 -0
  274. package/src/compose/index.mts +3 -0
  275. package/src/compose/intersection.mts +206 -0
  276. package/src/compose/intersection.test.mts +231 -0
  277. package/src/compose/merge-records.mts +218 -0
  278. package/src/compose/merge-records.test.mts +90 -0
  279. package/src/compose/union.mts +77 -0
  280. package/src/compose/union.test.mts +92 -0
  281. package/src/enum/enum.mts +53 -0
  282. package/src/enum/enum.test.mts +63 -0
  283. package/src/enum/index.mts +2 -0
  284. package/src/enum/uint-range.mts +63 -0
  285. package/src/enum/uint-range.test.mts +66 -0
  286. package/src/globals.d.mts +3 -0
  287. package/src/index.mts +9 -0
  288. package/src/primitives/bigint.mts +13 -0
  289. package/src/primitives/boolean.mts +13 -0
  290. package/src/primitives/index.mts +7 -0
  291. package/src/primitives/null.mts +9 -0
  292. package/src/primitives/number.mts +13 -0
  293. package/src/primitives/string.mts +13 -0
  294. package/src/primitives/symbol.mts +10 -0
  295. package/src/primitives/undefined.mts +9 -0
  296. package/src/record/index.mts +7 -0
  297. package/src/record/key-value-record.mts +84 -0
  298. package/src/record/key-value-record.test.mts +96 -0
  299. package/src/record/keyof.mts +31 -0
  300. package/src/record/keyof.test.mts +73 -0
  301. package/src/record/omit.mts +94 -0
  302. package/src/record/omit.test.mts +116 -0
  303. package/src/record/optional.mts +16 -0
  304. package/src/record/partial.mts +186 -0
  305. package/src/record/partial.test.mts +261 -0
  306. package/src/record/pick.mts +98 -0
  307. package/src/record/pick.test.mts +116 -0
  308. package/src/record/record.mts +105 -0
  309. package/src/record/record.test.mts +253 -0
  310. package/src/type.mts +30 -0
  311. package/src/unknown.mts +17 -0
  312. package/src/utils/create-assert-fn.mts +14 -0
  313. package/src/utils/create-cast-fn.mts +15 -0
  314. package/src/utils/create-is-fn.mts +7 -0
  315. package/src/utils/create-primitive-type.mts +35 -0
  316. package/src/utils/create-type.mts +28 -0
  317. package/src/utils/index.mts +6 -0
  318. package/src/utils/validation-error-message.mts +17 -0
@@ -0,0 +1,16 @@
1
+ import { type Type } from '../type.mjs';
2
+
3
+ export type OptionalPropertyType<T extends Type<unknown>> = T &
4
+ PartiallyRequired<T, 'optional'>;
5
+
6
+ export const optional = <T extends Type<unknown>>(
7
+ t: T,
8
+ ): OptionalPropertyType<T> => ({
9
+ ...t,
10
+ optional: true,
11
+ });
12
+
13
+ export const isOptionalProperty = <T extends Type<unknown>>(
14
+ t: T,
15
+ ): t is OptionalPropertyType<T> =>
16
+ Object.hasOwn(t, 'optional') && t.optional === true;
@@ -0,0 +1,186 @@
1
+ import { expectType, ISet, isRecord, Obj, Result } from 'ts-data-forge';
2
+ import { type Type, type TypeOf } from '../type.mjs';
3
+ import {
4
+ createAssertFn,
5
+ createCastFn,
6
+ createIsFn,
7
+ validationErrorMessage,
8
+ } from '../utils/index.mjs';
9
+
10
+ /**
11
+ * Creates a Partial type. If keysToBeOptional is set, only those keys are
12
+ * optional, otherwise, all properties are optional.
13
+ */
14
+ export const partial = <
15
+ const R extends Type<UnknownRecord>,
16
+ const KeysToBeOptional extends NonEmptyArray<keyof TypeOf<R> & string>,
17
+ >(
18
+ recordType: R,
19
+ options?: Partial<
20
+ Readonly<{
21
+ keysToBeOptional: KeysToBeOptional;
22
+ typeName: string;
23
+ }>
24
+ >,
25
+ ): PartialType<R, KeysToBeOptional> => {
26
+ type V = PartialTypeValue<R, KeysToBeOptional>;
27
+
28
+ const typeNameFilled: string =
29
+ options?.typeName ??
30
+ (options?.keysToBeOptional === undefined
31
+ ? `Partial<${recordType.typeName}>`
32
+ : `PartiallyPartial<${recordType.typeName}, ${options.keysToBeOptional.join('|')}>`);
33
+
34
+ const keysToBeOptional =
35
+ options?.keysToBeOptional ?? Object.keys(recordType.defaultValue);
36
+
37
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
38
+ const defaultValue: V = Obj.omit(
39
+ recordType.defaultValue,
40
+ keysToBeOptional,
41
+ ) as V;
42
+
43
+ const validate: Type<V>['validate'] = (a) => {
44
+ if (!isRecord(a)) {
45
+ return Result.err([
46
+ validationErrorMessage(a, 'The value is expected to be a record'),
47
+ ]);
48
+ }
49
+
50
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
51
+ const objectFilled = {
52
+ // fill optional properties to skip check by recordType.validate
53
+ ...Obj.pick(recordType.defaultValue, keysToBeOptional),
54
+ // overwrite with `a`
55
+ ...a,
56
+ } as TypeOf<R>;
57
+
58
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
59
+ return recordType.validate(objectFilled) as ReturnType<Type<V>['validate']>;
60
+ };
61
+
62
+ const fill: Type<V>['fill'] = (a) => {
63
+ if (!isRecord(a)) {
64
+ return defaultValue;
65
+ }
66
+
67
+ const inputKeys = ISet.create(Object.keys(a));
68
+
69
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
70
+ return Obj.omit(
71
+ // First, create a recursively filled object with all properties required.
72
+ recordType.fill(a),
73
+ // Then, Omit optional properties not present in `a`.
74
+ keysToBeOptional.filter(
75
+ (k) =>
76
+ // avoid removing properties from `a`
77
+ !inputKeys.has(k),
78
+ ),
79
+ ) as unknown as V;
80
+ };
81
+
82
+ return {
83
+ typeName: typeNameFilled,
84
+ defaultValue,
85
+ fill,
86
+ validate,
87
+ is: createIsFn(validate),
88
+ assertIs: createAssertFn(validate),
89
+ cast: createCastFn(validate),
90
+ };
91
+ };
92
+
93
+ type PartialTypeValue<
94
+ R extends Type<UnknownRecord>,
95
+ KeysToBeOptional extends NonEmptyArray<keyof TypeOf<R> & string> | undefined,
96
+ > =
97
+ TypeEq<KeysToBeOptional, undefined> extends true
98
+ ? Partial<TypeOf<R>>
99
+ : PartiallyPartial<TypeOf<R>, ArrayElement<KeysToBeOptional>>;
100
+
101
+ type PartialType<
102
+ R extends Type<UnknownRecord>,
103
+ KeysToBeOptional extends NonEmptyArray<keyof TypeOf<R> & string> | undefined,
104
+ > = Type<PartialTypeValue<R, KeysToBeOptional>>;
105
+
106
+ // expectType<TsFortressInternal.SetOptional<Type<0>>, OptionalType<0>>('=');
107
+
108
+ // expectType<
109
+ // TsFortressInternal.SetOptionalForAllKey<{
110
+ // a: Type<0>;
111
+ // }>,
112
+ // Readonly<{
113
+ // a: OptionalType<0>;
114
+ // }>
115
+ // >('=');
116
+
117
+ // expectType<
118
+ // TsFortressInternal.SetOptionalForAllKey<{
119
+ // a: Type<0>;
120
+ // b: OptionalType<0>;
121
+ // }>,
122
+ // Readonly<{
123
+ // a: OptionalType<0>;
124
+ // b: OptionalType<0>;
125
+ // }>
126
+ // >('=');
127
+
128
+ // expectType<
129
+ // TsFortressInternal.SetOptionalForAllKey<
130
+ // RecordType<{
131
+ // a: OptionalType<0>;
132
+ // b: OptionalType<0>;
133
+ // c: Type<0>;
134
+ // }>['source']
135
+ // >,
136
+ // Readonly<{
137
+ // a: OptionalType<0>;
138
+ // b: OptionalType<0>;
139
+ // c: OptionalType<0>;
140
+ // }>
141
+ // >('=');
142
+
143
+ // expectType<
144
+ // PartialType<
145
+ // RecordType<{
146
+ // a: OptionalType<0>;
147
+ // b: OptionalType<0>;
148
+ // c: Type<0>;
149
+ // }>,
150
+ // undefined
151
+ // >,
152
+ // RecordType<
153
+ // Readonly<{
154
+ // a: OptionalType<0>;
155
+ // b: OptionalType<0>;
156
+ // c: OptionalType<0>;
157
+ // }>
158
+ // >
159
+ // >('=');
160
+
161
+ // expectType<
162
+ // PartialType<
163
+ // Type<{
164
+ // a: OptionalType<0>;
165
+ // b: Type<0>;
166
+ // c: Type<0>;
167
+ // }>,
168
+ // ['b']
169
+ // >,
170
+ // RecordType<
171
+ // Readonly<{
172
+ // a: OptionalType<0>;
173
+ // b: OptionalType<0>;
174
+ // c: Type<0>;
175
+ // }>
176
+ // >
177
+ // >('=');
178
+
179
+ expectType<
180
+ PartialType<
181
+ Type<{ a: Type<0>; b: Type<0>; c: Type<0> }>,
182
+ // @ts-expect-error key "d" doesn't exist
183
+ ['a', 'd']
184
+ >,
185
+ 0
186
+ >('!=');
@@ -0,0 +1,261 @@
1
+ import { expectType } from 'ts-data-forge';
2
+ import { number } from '../primitives/index.mjs';
3
+ import { type Type, type TypeOf } from '../type.mjs';
4
+ import { optional } from './optional.mjs';
5
+ import { partial } from './partial.mjs';
6
+ import { record } from './record.mjs';
7
+
8
+ describe('partial', () => {
9
+ describe('fully partial', () => {
10
+ const ymdBase = record({
11
+ year: number(1900),
12
+ month: optional(number(1)),
13
+ date: optional(number(1)),
14
+ });
15
+
16
+ const ymd = partial(ymdBase);
17
+
18
+ expectType<
19
+ typeof ymd,
20
+ Type<Partial<Readonly<{ year: number; month: number; date: number }>>>
21
+ >('=');
22
+
23
+ type Ymd = TypeOf<typeof ymd>;
24
+
25
+ expectType<
26
+ Ymd,
27
+ Partial<Readonly<{ year: number; month: number; date: number }>>
28
+ >('=');
29
+
30
+ expectType<typeof ymd.defaultValue, Ymd>('=');
31
+
32
+ describe('is', () => {
33
+ test('truthy case', () => {
34
+ const x: UnknownRecord = {
35
+ year: 2000,
36
+ month: 12,
37
+ date: 12,
38
+ };
39
+
40
+ if (ymd.is(x)) {
41
+ expectType<typeof x, Ymd>('=');
42
+ } else {
43
+ expectType<typeof x, UnknownRecord>('=');
44
+ }
45
+
46
+ expect(ymd.is(x)).toBe(true);
47
+ });
48
+
49
+ test('falsy case', () => {
50
+ const x: UnknownRecord = {
51
+ year: 2000,
52
+ month: 'ab',
53
+ date: 'cd',
54
+ };
55
+
56
+ if (ymd.is(x)) {
57
+ expectType<typeof x, Ymd>('=');
58
+ } else {
59
+ expectType<typeof x, UnknownRecord>('=');
60
+ }
61
+
62
+ expect(ymd.is(x)).toBe(false);
63
+ });
64
+ });
65
+
66
+ describe('validate', () => {
67
+ test('falsy case 1', () => {
68
+ const x: UnknownRecord = {
69
+ year: 2000,
70
+ month: 'ab',
71
+ date: 'cd',
72
+ };
73
+
74
+ expect(ymd.validate(x).value).toStrictEqual([
75
+ `The value at record key "month" is expected to be <number>, but it is actually '"ab"'.`,
76
+ `The value is expected to be <number>, but it is actually '"ab"'.`,
77
+ ]);
78
+ });
79
+
80
+ test('falsy case 2', () => {
81
+ const x: UnknownRecord = {
82
+ year: 2000,
83
+ month: 'ab',
84
+ };
85
+
86
+ expect(ymd.validate(x).value).toStrictEqual([
87
+ `The value at record key "month" is expected to be <number>, but it is actually '"ab"'.`,
88
+ `The value is expected to be <number>, but it is actually '"ab"'.`,
89
+ ]);
90
+ });
91
+ });
92
+
93
+ describe('fill', () => {
94
+ test('from an empty record', () => {
95
+ const x: UnknownRecord = {};
96
+
97
+ expect(ymd.fill(x)).toStrictEqual({});
98
+ });
99
+
100
+ test('from a filled record', () => {
101
+ const x: UnknownRecord = {
102
+ year: 2000,
103
+ month: 999,
104
+ date: 999,
105
+ };
106
+
107
+ expect(ymd.fill(x)).toStrictEqual({
108
+ year: 2000,
109
+ month: 999,
110
+ date: 999,
111
+ });
112
+ });
113
+
114
+ test('from a partial record', () => {
115
+ const x: UnknownRecord = {
116
+ year: 2000,
117
+ };
118
+
119
+ expect(ymd.fill(x)).toStrictEqual({
120
+ year: 2000,
121
+ });
122
+ });
123
+
124
+ test('from a partial record with excess property', () => {
125
+ const x: UnknownRecord = {
126
+ year: 2000,
127
+ aaaaa: 9999,
128
+ };
129
+
130
+ expect(ymd.fill(x)).toStrictEqual({
131
+ year: 2000,
132
+ });
133
+ });
134
+ });
135
+ });
136
+
137
+ describe('partially partial', () => {
138
+ const ymdBase = record({
139
+ year: number(1900),
140
+ month: number(1),
141
+ date: number(1),
142
+ });
143
+
144
+ const ymd = partial(ymdBase, { keysToBeOptional: ['month', 'date'] });
145
+
146
+ type Ymd = TypeOf<typeof ymd>;
147
+
148
+ expectType<Ymd, Readonly<{ year: number; month?: number; date?: number }>>(
149
+ '=',
150
+ );
151
+
152
+ expectType<typeof ymd.defaultValue, Ymd>('=');
153
+
154
+ describe('is', () => {
155
+ test('truthy case', () => {
156
+ const x: UnknownRecord = {
157
+ year: 2000,
158
+ month: 12,
159
+ };
160
+
161
+ if (ymd.is(x)) {
162
+ expectType<typeof x, Ymd>('=');
163
+ } else {
164
+ expectType<typeof x, UnknownRecord>('=');
165
+ }
166
+
167
+ expect(ymd.is(x)).toBe(true);
168
+ });
169
+
170
+ test('falsy case 1', () => {
171
+ const x: UnknownRecord = {
172
+ year: 2000,
173
+ month: 'ab',
174
+ date: 'cd',
175
+ };
176
+
177
+ if (ymd.is(x)) {
178
+ expectType<typeof x, Ymd>('=');
179
+ } else {
180
+ expectType<typeof x, UnknownRecord>('=');
181
+ }
182
+
183
+ expect(ymd.is(x)).toBe(false);
184
+ });
185
+
186
+ test('falsy case 2', () => {
187
+ const x: UnknownRecord = {
188
+ date: 'cd',
189
+ };
190
+
191
+ if (ymd.is(x)) {
192
+ expectType<typeof x, Ymd>('=');
193
+ } else {
194
+ expectType<typeof x, UnknownRecord>('=');
195
+ }
196
+
197
+ expect(ymd.is(x)).toBe(false);
198
+ });
199
+ });
200
+
201
+ describe('validate', () => {
202
+ test('falsy case', () => {
203
+ const x: UnknownRecord = {
204
+ year: 2000,
205
+ month: 'ab',
206
+ date: 'cd',
207
+ };
208
+
209
+ expect(ymd.validate(x).value).toStrictEqual([
210
+ `The value at record key "month" is expected to be <number>, but it is actually '"ab"'.`,
211
+ `The value is expected to be <number>, but it is actually '"ab"'.`,
212
+ ]);
213
+ });
214
+ });
215
+
216
+ describe('fill', () => {
217
+ test('from an empty record', () => {
218
+ const x: UnknownRecord = {};
219
+
220
+ expect(ymd.fill(x)).toStrictEqual({
221
+ year: 1900,
222
+ });
223
+ });
224
+
225
+ test('from a filled record', () => {
226
+ const x: UnknownRecord = {
227
+ year: 2000,
228
+ month: 999,
229
+ date: 999,
230
+ };
231
+
232
+ expect(ymd.fill(x)).toStrictEqual({
233
+ year: 2000,
234
+ month: 999,
235
+ date: 999,
236
+ });
237
+ });
238
+
239
+ test('from a partial record', () => {
240
+ const x: UnknownRecord = {
241
+ year: 2000,
242
+ };
243
+
244
+ expect(ymd.fill(x)).toStrictEqual({
245
+ year: 2000,
246
+ });
247
+ });
248
+
249
+ test('from a partial record with excess property', () => {
250
+ const x: UnknownRecord = {
251
+ year: 2000,
252
+ aaaaa: 9999,
253
+ };
254
+
255
+ expect(ymd.fill(x)).toStrictEqual({
256
+ year: 2000,
257
+ });
258
+ });
259
+ });
260
+ });
261
+ });
@@ -0,0 +1,98 @@
1
+ import { expectType, isRecord, Obj, Result } from 'ts-data-forge';
2
+ import { type Type, type TypeOf } from '../type.mjs';
3
+ import {
4
+ createAssertFn,
5
+ createCastFn,
6
+ createIsFn,
7
+ validationErrorMessage,
8
+ } from '../utils/index.mjs';
9
+
10
+ /** Creates a record type with keys picked. */
11
+ export const pick = <
12
+ const R extends Type<UnknownRecord>,
13
+ const KeysToPick extends readonly (keyof TypeOf<R> & string)[],
14
+ >(
15
+ recordType: R,
16
+ keysToPick: KeysToPick,
17
+ options?: Partial<
18
+ Readonly<{
19
+ typeName: string;
20
+ }>
21
+ >,
22
+ ): PickedType<R, KeysToPick> => {
23
+ type V = Pick<TypeOf<R>, ArrayElement<KeysToPick>>;
24
+
25
+ const typeNameFilled: string =
26
+ options?.typeName ??
27
+ `Pick<${recordType.typeName}, ${keysToPick.join('|')}>`;
28
+
29
+ const defaultValue: V = Obj.pick(recordType.defaultValue, keysToPick) as V;
30
+
31
+ const validate: Type<V>['validate'] = (a) => {
32
+ if (!isRecord(a)) {
33
+ return Result.err([
34
+ validationErrorMessage(a, 'The value is expected to be a record'),
35
+ ]);
36
+ }
37
+
38
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
39
+ const objectFilled = {
40
+ ...Obj.omit(recordType.fill(a), keysToPick),
41
+ ...a,
42
+ } as TypeOf<R>;
43
+
44
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
45
+ return recordType.validate(objectFilled) as ReturnType<Type<V>['validate']>;
46
+ };
47
+
48
+ const fill: Type<V>['fill'] = (a) =>
49
+ Obj.pick(recordType.fill(a), keysToPick) as V;
50
+
51
+ return {
52
+ typeName: typeNameFilled,
53
+ defaultValue,
54
+ fill,
55
+ validate,
56
+ is: createIsFn(validate),
57
+ assertIs: createAssertFn(validate),
58
+ cast: createCastFn(validate),
59
+ };
60
+ };
61
+
62
+ type PickedType<
63
+ R extends Type<UnknownRecord>,
64
+ KeysToPick extends readonly (keyof TypeOf<R>)[],
65
+ > = Type<Pick<TypeOf<R>, ArrayElement<KeysToPick>>>;
66
+
67
+ expectType<
68
+ Pick<TypeOf<Type<{ a: Type<0>; b: Type<0>; c: Type<0> }>>, 'a' | 'b'>,
69
+ {
70
+ a: Type<0>;
71
+ b: Type<0>;
72
+ }
73
+ >('=');
74
+
75
+ expectType<
76
+ Type<Pick<TypeOf<Type<{ a: Type<0>; b: Type<0>; c: Type<0> }>>, 'a' | 'b'>>,
77
+ Type<{
78
+ a: Type<0>;
79
+ b: Type<0>;
80
+ }>
81
+ >('=');
82
+
83
+ expectType<
84
+ PickedType<Type<{ a: Type<0>; b: Type<0>; c: Type<0> }>, ['a', 'b']>,
85
+ Type<{
86
+ a: Type<0>;
87
+ b: Type<0>;
88
+ }>
89
+ >('=');
90
+
91
+ expectType<
92
+ PickedType<
93
+ Type<{ a: Type<0>; b: Type<0>; c: Type<0> }>,
94
+ // @ts-expect-error key "d" doesn't exist
95
+ ['a', 'd']
96
+ >,
97
+ 0
98
+ >('!=');
@@ -0,0 +1,116 @@
1
+ import { expectType } from 'ts-data-forge';
2
+ import { number } from '../primitives/index.mjs';
3
+ import { type Type, type TypeOf } from '../type.mjs';
4
+ import { pick } from './pick.mjs';
5
+ import { record } from './record.mjs';
6
+
7
+ describe('pick', () => {
8
+ const ym = pick(
9
+ record({
10
+ year: number(1900),
11
+ month: number(1),
12
+ date: number(1),
13
+ }),
14
+ ['year', 'month'],
15
+ );
16
+
17
+ expectType<typeof ym, Type<Readonly<{ year: number; month: number }>>>('=');
18
+
19
+ type Ym = TypeOf<typeof ym>;
20
+
21
+ expectType<Ym, Readonly<{ year: number; month: number }>>('=');
22
+
23
+ expectType<typeof ym.defaultValue, Ym>('=');
24
+
25
+ describe('is', () => {
26
+ test('truthy case', () => {
27
+ const x: UnknownRecord = {
28
+ year: 2000,
29
+ month: 12,
30
+ };
31
+
32
+ if (ym.is(x)) {
33
+ expectType<typeof x, Ym>('=');
34
+ } else {
35
+ expectType<typeof x, UnknownRecord>('=');
36
+ }
37
+
38
+ expect(ym.is(x)).toBe(true);
39
+ });
40
+
41
+ test('falsy case', () => {
42
+ const x: UnknownRecord = {
43
+ year: 2000,
44
+ month: 'ab',
45
+ };
46
+
47
+ if (ym.is(x)) {
48
+ expectType<typeof x, Ym>('=');
49
+ } else {
50
+ expectType<typeof x, UnknownRecord>('=');
51
+ }
52
+
53
+ expect(ym.is(x)).toBe(false);
54
+ });
55
+ });
56
+
57
+ describe('validate', () => {
58
+ test('falsy case', () => {
59
+ const x: UnknownRecord = {
60
+ year: 2000,
61
+ month: 'ab',
62
+ };
63
+
64
+ expect(ym.validate(x).value).toStrictEqual([
65
+ `The value at record key "month" is expected to be <number>, but it is actually '"ab"'.`,
66
+ `The value is expected to be <number>, but it is actually '"ab"'.`,
67
+ ]);
68
+ });
69
+ });
70
+
71
+ describe('fill', () => {
72
+ test('from an empty record', () => {
73
+ const x: UnknownRecord = {};
74
+
75
+ expect(ym.fill(x)).toStrictEqual({
76
+ year: 1900,
77
+ month: 1,
78
+ });
79
+ });
80
+
81
+ test('from a filled record', () => {
82
+ const x: UnknownRecord = {
83
+ year: 2000,
84
+ month: 999,
85
+ };
86
+
87
+ expect(ym.fill(x)).toStrictEqual({
88
+ year: 2000,
89
+ month: 999,
90
+ });
91
+ });
92
+
93
+ test('from a partial record', () => {
94
+ const x: UnknownRecord = {
95
+ year: 2000,
96
+ };
97
+
98
+ expect(ym.fill(x)).toStrictEqual({
99
+ year: 2000,
100
+ month: 1,
101
+ });
102
+ });
103
+
104
+ test('from a partial record with excess property', () => {
105
+ const x: UnknownRecord = {
106
+ year: 2000,
107
+ aaaaa: 9999,
108
+ };
109
+
110
+ expect(ym.fill(x)).toStrictEqual({
111
+ year: 2000,
112
+ month: 1,
113
+ });
114
+ });
115
+ });
116
+ });