@strictly/define 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) hide show
  1. package/.out/index.d.ts +17 -11
  2. package/.out/index.js +17 -11
  3. package/.out/transformers/copies/copy.d.ts +3 -3
  4. package/.out/transformers/copies/copy_to.d.ts +3 -3
  5. package/.out/transformers/copies/copy_to.js +4 -0
  6. package/.out/transformers/copies/mobx_copy.d.ts +4 -4
  7. package/.out/transformers/copies/mobx_copy.js +1 -1
  8. package/.out/transformers/copies/specs/copy_to.tests.js +8 -8
  9. package/.out/transformers/copies/specs/mobx_copy.tests.js +1 -1
  10. package/.out/transformers/flatteners/flatten_accessors_of_type.d.ts +5 -0
  11. package/.out/transformers/flatteners/flatten_accessors_of_type.js +11 -0
  12. package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.d.ts +2 -2
  13. package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.js +2 -2
  14. package/.out/transformers/flatteners/{flatten_type_def_to.d.ts → flatten_type_to.d.ts} +1 -1
  15. package/.out/transformers/flatteners/{flatten_type_def_to.js → flatten_type_to.js} +1 -1
  16. package/.out/transformers/flatteners/{flatten_type_defs_of.d.ts → flatten_types_of_type.d.ts} +1 -1
  17. package/.out/transformers/flatteners/flatten_types_of_type.js +7 -0
  18. package/.out/transformers/flatteners/flatten_validation_errors_of_type.d.ts +22 -0
  19. package/.out/transformers/flatteners/flatten_validation_errors_of_type.js +10 -0
  20. package/.out/transformers/flatteners/flatten_validators_of_validating_type.d.ts +4 -0
  21. package/.out/transformers/flatteners/flatten_validators_of_validating_type.js +17 -0
  22. package/.out/transformers/flatteners/{flatten_value_type_to.d.ts → flatten_value_to.d.ts} +4 -4
  23. package/.out/transformers/flatteners/{flatten_value_type_to.js → flatten_value_to.js} +7 -2
  24. package/.out/transformers/flatteners/flatten_values_of_type.d.ts +3 -0
  25. package/.out/transformers/flatteners/flatten_values_of_type.js +7 -0
  26. package/.out/transformers/flatteners/specs/{flatten_accessors_of.tests.js → flatten_accessors_of_type.tests.js} +5 -6
  27. package/.out/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.js +6 -6
  28. package/.out/transformers/flatteners/specs/{flatten_type_def_to.tests.js → flatten_type_to.tests.js} +15 -13
  29. package/.out/transformers/flatteners/specs/flatten_types_of_type.tests.js +18 -0
  30. package/.out/transformers/flatteners/specs/flatten_validation_errors_of_type.tests.js +25 -0
  31. package/.out/transformers/flatteners/specs/{flatten_value_type_to.tests.js → flatten_value_to.tests.js} +81 -58
  32. package/.out/transformers/flatteners/specs/{flatten_value_types_of.tests.js → flatten_values_of_type.tests.js} +4 -4
  33. package/.out/transformers/flatteners/specs/value_path_to_type_path.tests.js +5 -3
  34. package/.out/transformers/flatteners/value_path_to_type_path.d.ts +1 -1
  35. package/.out/tsconfig.tsbuildinfo +1 -1
  36. package/.out/types/builders.d.ts +55 -29
  37. package/.out/types/builders.js +116 -32
  38. package/.out/types/definitions.d.ts +1 -1
  39. package/.out/types/flattened_accessors_of_type.d.ts +9 -0
  40. package/.out/types/{flattened_type_defs_of.d.ts → flattened_types_of_type.d.ts} +9 -6
  41. package/.out/types/flattened_types_of_validating_type.d.ts +25 -0
  42. package/.out/types/flattened_validators_of_validating_type.d.ts +10 -0
  43. package/.out/types/flattened_values_of_type.d.ts +6 -0
  44. package/.out/types/{mobx_value_type_of.d.ts → mobx_value_of_type.d.ts} +2 -2
  45. package/.out/types/{partial_type_def_of.d.ts → partial_type_of_type.d.ts} +1 -1
  46. package/.out/types/path_of.d.ts +1 -0
  47. package/.out/types/{json_paths_of.d.ts → paths_of_type.d.ts} +5 -5
  48. package/.out/types/readonly_type_of_type.d.ts +29 -0
  49. package/.out/types/specs/builder.tests.js +73 -23
  50. package/.out/types/specs/{flattened_accessors_of.tests.js → flattened_accessors_of_type.tests.js} +1 -1
  51. package/.out/types/specs/{flattened_type_defs_of.tests.js → flattened_types_of_type.tests.js} +22 -15
  52. package/.out/types/specs/flattened_validators_of_validating_type.tests.js +24 -0
  53. package/.out/types/specs/flattened_values_of_type.tests.js +18 -0
  54. package/.out/types/specs/{partial_type_def_of.tests.js → partial_type_of_type.tests.js} +4 -4
  55. package/.out/types/specs/{json_paths_of.tests.js → paths_of_type.tests.js} +19 -19
  56. package/.out/types/specs/{readonly_type_def_of.tests.js → readonly_type_of_type.tests.js} +6 -6
  57. package/.out/types/specs/type_of_type.tests.d.ts +1 -0
  58. package/.out/types/specs/type_of_type.tests.js +27 -0
  59. package/.out/types/specs/value_of_type.tests.d.ts +1 -0
  60. package/.out/types/specs/{value_type_of.tests.js → value_of_type.tests.js} +1 -1
  61. package/.out/types/specs/value_to_type_paths_of_type.tests.d.ts +1 -0
  62. package/.out/types/specs/{value_to_type_paths_of.tests.js → value_to_type_paths_of_type.tests.js} +9 -9
  63. package/.out/types/specs/value_types_of_discriminated_union.tests.js +2 -2
  64. package/.out/types/strict_definitions.d.ts +2 -2
  65. package/.out/types/type_of_type.d.ts +49 -0
  66. package/.out/types/type_of_type.js +70 -0
  67. package/.out/types/validating_definitions.d.ts +49 -0
  68. package/.out/types/validating_definitions.js +1 -0
  69. package/.out/types/validating_type_def_with_error.d.ts +55 -0
  70. package/.out/types/validating_type_def_with_error.js +1 -0
  71. package/.out/types/value_of_type.d.ts +24 -0
  72. package/.out/types/value_of_type.js +1 -0
  73. package/.out/types/{value_to_type_paths_of.d.ts → value_to_type_paths_of_type.d.ts} +13 -13
  74. package/.out/types/value_to_type_paths_of_type.js +1 -0
  75. package/.out/types/value_types_of_discriminated_union.d.ts +5 -4
  76. package/.out/validation/errors_of_validators.d.ts +4 -0
  77. package/.out/validation/errors_of_validators.js +1 -0
  78. package/.out/validation/specs/errors_of_validators.tests.d.ts +1 -0
  79. package/.out/validation/specs/errors_of_validators.tests.js +9 -0
  80. package/.out/validation/specs/validators_of_values.tests.d.ts +1 -0
  81. package/.out/validation/specs/validators_of_values.tests.js +21 -0
  82. package/.out/validation/validator.d.ts +21 -0
  83. package/.out/validation/validator.js +26 -0
  84. package/.out/validation/validators/defined_validator.d.ts +10 -0
  85. package/.out/validation/validators/defined_validator.js +18 -0
  86. package/.out/validation/validators/minimum_string_length_validator.d.ts +16 -0
  87. package/.out/validation/validators/minimum_string_length_validator.js +23 -0
  88. package/.out/validation/validators_of_values.d.ts +4 -0
  89. package/.out/validation/validators_of_values.js +1 -0
  90. package/.turbo/turbo-build.log +9 -9
  91. package/.turbo/turbo-check-types.log +1 -1
  92. package/.turbo/turbo-release$colon$exports.log +1 -1
  93. package/README.md +16 -2
  94. package/dist/index.cjs +364 -76
  95. package/dist/index.d.cts +365 -114
  96. package/dist/index.d.ts +365 -114
  97. package/dist/index.js +355 -73
  98. package/index.ts +17 -11
  99. package/package.json +11 -3
  100. package/transformers/copies/copy.ts +4 -4
  101. package/transformers/copies/copy_to.ts +11 -7
  102. package/transformers/copies/mobx_copy.ts +6 -6
  103. package/transformers/copies/specs/copy_to.tests.ts +8 -8
  104. package/transformers/copies/specs/mobx_copy.tests.ts +3 -3
  105. package/transformers/flatteners/{flatten_accessors_of.ts → flatten_accessors_of_type.ts} +10 -10
  106. package/transformers/flatteners/flatten_json_value_to_type_paths_of.ts +5 -5
  107. package/transformers/flatteners/{flatten_type_def_to.ts → flatten_type_to.ts} +1 -1
  108. package/transformers/flatteners/{flatten_type_defs_of.ts → flatten_types_of_type.ts} +3 -3
  109. package/transformers/flatteners/flatten_validation_errors_of_type.ts +93 -0
  110. package/transformers/flatteners/flatten_validators_of_validating_type.ts +32 -0
  111. package/transformers/flatteners/{flatten_value_type_to.ts → flatten_value_to.ts} +12 -7
  112. package/transformers/flatteners/{flatten_value_types_of.ts → flatten_values_of_type.ts} +6 -6
  113. package/transformers/flatteners/specs/{flatten_accessors_of.tests.ts → flatten_accessors_of_type.tests.ts} +10 -11
  114. package/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.ts +6 -6
  115. package/transformers/flatteners/specs/{flatten_type_def_to.tests.ts → flatten_type_to.tests.ts} +15 -13
  116. package/transformers/flatteners/specs/flatten_types_of_type.tests.ts +27 -0
  117. package/transformers/flatteners/specs/flatten_validation_errors_of_type.tests.ts +48 -0
  118. package/transformers/flatteners/specs/{flatten_value_type_to.tests.ts → flatten_value_to.tests.ts} +124 -91
  119. package/transformers/flatteners/specs/{flatten_value_types_of.tests.ts → flatten_values_of_type.tests.ts} +4 -4
  120. package/transformers/flatteners/specs/value_path_to_type_path.tests.ts +20 -18
  121. package/transformers/flatteners/value_path_to_type_path.ts +4 -4
  122. package/types/builders.ts +239 -71
  123. package/types/definitions.ts +2 -2
  124. package/types/{flattened_accessors_of.ts → flattened_accessors_of_type.ts} +3 -3
  125. package/types/{flattened_type_defs_of.ts → flattened_types_of_type.ts} +22 -12
  126. package/types/flattened_types_of_validating_type.ts +152 -0
  127. package/types/flattened_validators_of_validating_type.ts +33 -0
  128. package/types/flattened_values_of_type.ts +11 -0
  129. package/types/{mobx_value_type_of.ts → mobx_value_of_type.ts} +2 -2
  130. package/types/{partial_type_def_of.ts → partial_type_of_type.ts} +3 -1
  131. package/types/{json_path_of.ts → path_of.ts} +1 -1
  132. package/types/{json_paths_of.ts → paths_of_type.ts} +5 -5
  133. package/types/readonly_type_of_type.ts +54 -0
  134. package/types/specs/builder.tests.ts +205 -54
  135. package/types/specs/{flattened_accessors_of.tests.ts → flattened_accessors_of_type.tests.ts} +3 -3
  136. package/types/specs/{flattened_type_defs_of.tests.ts → flattened_types_of_type.tests.ts} +82 -34
  137. package/types/specs/flattened_validators_of_validating_type.tests.ts +47 -0
  138. package/types/specs/flattened_values_of_type.tests.ts +39 -0
  139. package/types/specs/{partial_type_def_of.tests.ts → partial_type_of_type.tests.ts} +11 -11
  140. package/types/specs/{json_paths_of.tests.ts → paths_of_type.tests.ts} +47 -47
  141. package/types/specs/{readonly_type_def_of.tests.ts → readonly_type_of_type.tests.ts} +14 -14
  142. package/types/specs/type_of_type.tests.ts +60 -0
  143. package/types/specs/{value_type_of.tests.ts → value_of_type.tests.ts} +15 -15
  144. package/types/specs/{value_to_type_paths_of.tests.ts → value_to_type_paths_of_type.tests.ts} +26 -25
  145. package/types/specs/value_types_of_discriminated_union.tests.ts +6 -6
  146. package/types/strict_definitions.ts +3 -3
  147. package/types/type_of_type.ts +178 -0
  148. package/types/validating_definitions.ts +109 -0
  149. package/types/validating_type_def_with_error.ts +93 -0
  150. package/types/value_of_type.ts +84 -0
  151. package/types/{value_to_type_paths_of.ts → value_to_type_paths_of_type.ts} +30 -29
  152. package/types/value_types_of_discriminated_union.ts +4 -3
  153. package/validation/errors_of_validators.ts +10 -0
  154. package/validation/specs/errors_of_validators.tests.ts +20 -0
  155. package/validation/specs/validators_of_values.tests.ts +68 -0
  156. package/validation/validator.ts +103 -0
  157. package/validation/validators/defined_validator.ts +22 -0
  158. package/validation/validators/minimum_string_length_validator.ts +36 -0
  159. package/validation/validators_of_values.ts +17 -0
  160. package/.out/transformers/flatteners/flatten_accessors_of.d.ts +0 -5
  161. package/.out/transformers/flatteners/flatten_accessors_of.js +0 -11
  162. package/.out/transformers/flatteners/flatten_type_defs_of.js +0 -7
  163. package/.out/transformers/flatteners/flatten_value_types_of.d.ts +0 -3
  164. package/.out/transformers/flatteners/flatten_value_types_of.js +0 -7
  165. package/.out/transformers/flatteners/specs/flatten_type_defs_of.tests.js +0 -17
  166. package/.out/types/flattened_accessors_of.d.ts +0 -9
  167. package/.out/types/flattened_value_types_of.d.ts +0 -6
  168. package/.out/types/json_path_of.d.ts +0 -1
  169. package/.out/types/readonly_type_def_of.d.ts +0 -29
  170. package/.out/types/specs/flattened_value_types_of.tests.js +0 -11
  171. package/.out/types/value_type_of.d.ts +0 -24
  172. package/transformers/flatteners/specs/flatten_type_defs_of.tests.ts +0 -26
  173. package/types/flattened_value_types_of.ts +0 -11
  174. package/types/readonly_type_def_of.ts +0 -53
  175. package/types/specs/flattened_value_types_of.tests.ts +0 -21
  176. package/types/value_type_of.ts +0 -84
  177. /package/.out/transformers/flatteners/specs/{flatten_accessors_of.tests.d.ts → flatten_accessors_of_type.tests.d.ts} +0 -0
  178. /package/.out/transformers/flatteners/specs/{flatten_type_def_to.tests.d.ts → flatten_type_to.tests.d.ts} +0 -0
  179. /package/.out/transformers/flatteners/specs/{flatten_type_defs_of.tests.d.ts → flatten_types_of_type.tests.d.ts} +0 -0
  180. /package/.out/transformers/flatteners/specs/{flatten_value_type_to.tests.d.ts → flatten_validation_errors_of_type.tests.d.ts} +0 -0
  181. /package/.out/transformers/flatteners/specs/{flatten_value_types_of.tests.d.ts → flatten_value_to.tests.d.ts} +0 -0
  182. /package/.out/{types/flattened_accessors_of.js → transformers/flatteners/specs/flatten_values_of_type.tests.d.ts} +0 -0
  183. /package/.out/types/{flattened_type_defs_of.js → flattened_accessors_of_type.js} +0 -0
  184. /package/.out/types/{flattened_value_types_of.js → flattened_types_of_type.js} +0 -0
  185. /package/.out/types/{json_path_of.js → flattened_types_of_validating_type.js} +0 -0
  186. /package/.out/types/{json_paths_of.js → flattened_validators_of_validating_type.js} +0 -0
  187. /package/.out/types/{partial_type_def_of.js → flattened_values_of_type.js} +0 -0
  188. /package/.out/types/{mobx_value_type_of.js → mobx_value_of_type.js} +0 -0
  189. /package/.out/types/{readonly_type_def_of.js → partial_type_of_type.js} +0 -0
  190. /package/.out/types/{specs/flattened_accessors_of.tests.d.ts → path_of.js} +0 -0
  191. /package/.out/types/{specs/flattened_type_defs_of.tests.d.ts → paths_of_type.js} +0 -0
  192. /package/.out/types/{specs/flattened_value_types_of.tests.d.ts → readonly_type_of_type.js} +0 -0
  193. /package/.out/types/specs/{json_paths_of.tests.d.ts → flattened_accessors_of_type.tests.d.ts} +0 -0
  194. /package/.out/types/specs/{partial_type_def_of.tests.d.ts → flattened_types_of_type.tests.d.ts} +0 -0
  195. /package/.out/types/specs/{readonly_type_def_of.tests.d.ts → flattened_validators_of_validating_type.tests.d.ts} +0 -0
  196. /package/.out/types/specs/{value_to_type_paths_of.tests.d.ts → flattened_values_of_type.tests.d.ts} +0 -0
  197. /package/.out/types/specs/{value_type_of.tests.d.ts → partial_type_of_type.tests.d.ts} +0 -0
  198. /package/.out/types/{value_to_type_paths_of.js → specs/paths_of_type.tests.d.ts} +0 -0
  199. /package/.out/types/{value_type_of.js → specs/readonly_type_of_type.tests.d.ts} +0 -0
@@ -12,12 +12,12 @@ import {
12
12
  stringType,
13
13
  union,
14
14
  } from 'types/builders'
15
- import { type FlattenedTypeDefsOf } from 'types/flattened_type_defs_of'
16
- import { type ValueToTypePathsOf } from 'types/value_to_type_paths_of'
15
+ import { type FlattenedTypesOfType } from 'types/flattened_types_of_type'
16
+ import { type ValueToTypePathsOfType } from 'types/value_to_type_paths_of_type'
17
17
 
18
- describe('FlattenedJsonPathsOf', function () {
18
+ describe('ValueToTypePathsOfType', function () {
19
19
  describe('literal', function () {
20
- type T = ValueToTypePathsOf<typeof numberType>
20
+ type T = ValueToTypePathsOfType<typeof numberType.narrow>
21
21
 
22
22
  let t: {
23
23
  readonly $: '$',
@@ -29,7 +29,7 @@ describe('FlattenedJsonPathsOf', function () {
29
29
 
30
30
  describe('list', function () {
31
31
  const builder = list(list(numberType))
32
- type T = SimplifyDeep<ValueToTypePathsOf<typeof builder>>
32
+ type T = SimplifyDeep<ValueToTypePathsOfType<typeof builder.narrow>>
33
33
 
34
34
  let t: {
35
35
  readonly $: '$',
@@ -44,7 +44,7 @@ describe('FlattenedJsonPathsOf', function () {
44
44
  describe('record', function () {
45
45
  const l = list(numberType)
46
46
  const builder = record<typeof l, 'a' | 'b'>(l)
47
- type T = SimplifyDeep<ValueToTypePathsOf<typeof builder>>
47
+ type T = SimplifyDeep<ValueToTypePathsOfType<typeof builder>>
48
48
 
49
49
  let t: {
50
50
  readonly $: '$',
@@ -64,11 +64,12 @@ describe('FlattenedJsonPathsOf', function () {
64
64
 
65
65
  describe('object', function () {
66
66
  const builder = object()
67
- .set('a', list(numberType))
68
- .setOptional('b', booleanType)
69
- .setReadonly('c', stringType)
70
- .setReadonlyOptional('d', stringType)
71
- type T = SimplifyDeep<ValueToTypePathsOf<typeof builder>>
67
+ .field('a', list(numberType))
68
+ .optionalField('b', booleanType)
69
+ .readonlyField('c', stringType)
70
+ .readonlyOptionalField('d', stringType)
71
+
72
+ type T = SimplifyDeep<ValueToTypePathsOfType<typeof builder>>
72
73
 
73
74
  let t: {
74
75
  readonly $: '$',
@@ -83,12 +84,12 @@ describe('FlattenedJsonPathsOf', function () {
83
84
  })
84
85
 
85
86
  it('has the same value paths', function () {
86
- type ValuePaths = keyof FlattenedTypeDefsOf<typeof builder, null>
87
+ type ValuePaths = keyof FlattenedTypesOfType<typeof builder, null>
87
88
  expectTypeOf<ValuePaths>().toEqualTypeOf<keyof T>()
88
89
  })
89
90
 
90
91
  it('has the same type paths', function () {
91
- type TypePaths = keyof FlattenedTypeDefsOf<typeof builder, '*'>
92
+ type TypePaths = keyof FlattenedTypesOfType<typeof builder, '*'>
92
93
  expectTypeOf<TypePaths>().toEqualTypeOf<ValueOf<T>>()
93
94
  })
94
95
  })
@@ -96,9 +97,9 @@ describe('FlattenedJsonPathsOf', function () {
96
97
  describe('union', function () {
97
98
  describe('non-discriminated', function () {
98
99
  const builder = union()
99
- .add('1', list(numberType))
100
- .add('2', stringType)
101
- type T = SimplifyDeep<ValueToTypePathsOf<typeof builder>>
100
+ .or('1', list(numberType))
101
+ .or('2', stringType)
102
+ type T = SimplifyDeep<ValueToTypePathsOfType<typeof builder>>
102
103
 
103
104
  let t: {
104
105
  readonly $: '$',
@@ -112,9 +113,9 @@ describe('FlattenedJsonPathsOf', function () {
112
113
 
113
114
  describe('discriminated', function () {
114
115
  const builder = union('d')
115
- .add('1', object().set('a', booleanType).set('b', numberType))
116
- .add('2', object().set('x', numberType).set('y', stringType))
117
- type T = SimplifyDeep<ValueToTypePathsOf<typeof builder>>
116
+ .or('1', object().field('a', booleanType).field('b', numberType))
117
+ .or('2', object().field('x', numberType).field('y', stringType))
118
+ type T = SimplifyDeep<ValueToTypePathsOfType<typeof builder>>
118
119
 
119
120
  let t: {
120
121
  readonly $: '$',
@@ -128,12 +129,12 @@ describe('FlattenedJsonPathsOf', function () {
128
129
  })
129
130
 
130
131
  it('has the same value paths', function () {
131
- type ValuePaths = keyof FlattenedTypeDefsOf<typeof builder, null>
132
+ type ValuePaths = keyof FlattenedTypesOfType<typeof builder, null>
132
133
  expectTypeOf<ValuePaths>().toEqualTypeOf<keyof T>()
133
134
  })
134
135
 
135
136
  it('has the same type paths', function () {
136
- type TypePaths = keyof FlattenedTypeDefsOf<typeof builder, '*'>
137
+ type TypePaths = keyof FlattenedTypesOfType<typeof builder, '*'>
137
138
  expectTypeOf<TypePaths>().toEqualTypeOf<ValueOf<T>>()
138
139
  })
139
140
  })
@@ -142,7 +143,7 @@ describe('FlattenedJsonPathsOf', function () {
142
143
  describe('readonly', function () {
143
144
  const builder = list(list(numberType)).readonly()
144
145
 
145
- type T = SimplifyDeep<ValueToTypePathsOf<typeof builder>>
146
+ type T = SimplifyDeep<ValueToTypePathsOfType<typeof builder>>
146
147
 
147
148
  let t: {
148
149
  readonly $: '$',
@@ -157,7 +158,7 @@ describe('FlattenedJsonPathsOf', function () {
157
158
  describe('nullable', function () {
158
159
  const builder = nullable(list(nullable(list(numberType))))
159
160
 
160
- type T = SimplifyDeep<ValueToTypePathsOf<typeof builder>>
161
+ type T = SimplifyDeep<ValueToTypePathsOfType<typeof builder>>
161
162
 
162
163
  let t: {
163
164
  readonly $: '$',
@@ -169,12 +170,12 @@ describe('FlattenedJsonPathsOf', function () {
169
170
  })
170
171
 
171
172
  it('has the same value paths', function () {
172
- type ValuePaths = keyof FlattenedTypeDefsOf<typeof builder, null>
173
+ type ValuePaths = keyof FlattenedTypesOfType<typeof builder, null>
173
174
  expectTypeOf<ValuePaths>().toEqualTypeOf<keyof T>()
174
175
  })
175
176
 
176
177
  it('has the same type paths', function () {
177
- type TypePaths = keyof FlattenedTypeDefsOf<typeof builder, '*'>
178
+ type TypePaths = keyof FlattenedTypesOfType<typeof builder, '*'>
178
179
  expectTypeOf<TypePaths>().toEqualTypeOf<ValueOf<T>>()
179
180
  })
180
181
  })
@@ -10,20 +10,20 @@ import { type ValueTypesOfDiscriminatedUnion } from 'types/value_types_of_discri
10
10
  describe('ValueTypesOfDiscriminatedUnion', function () {
11
11
  it('matches expected type', function () {
12
12
  const { definition } = union('d')
13
- .add('a', object().set('x', numberType))
14
- .add('b', object().set('y', booleanType))
13
+ .or('a', object().field('x', numberType))
14
+ .or('b', object().field('y', booleanType))
15
15
  type T = SimplifyDeep<ValueTypesOfDiscriminatedUnion<
16
16
  typeof definition
17
17
  >>
18
18
 
19
19
  expectTypeOf<T>().toEqualTypeOf<{
20
20
  readonly a: {
21
- d: 'a',
22
- x: number,
21
+ readonly d: 'a',
22
+ readonly x: number,
23
23
  },
24
24
  readonly b: {
25
- d: 'b',
26
- y: boolean,
25
+ readonly d: 'b',
26
+ readonly y: boolean,
27
27
  },
28
28
  }>()
29
29
  })
@@ -63,16 +63,16 @@ export type StrictRecordTypeDef<
63
63
 
64
64
  // NOTE we use the `readonly` and `?` (partial) status of these field definitions
65
65
  // to describe the same attributes of the fields
66
- export type StrictStructuredTypeDefFields = {
66
+ export type StrictObjectTypeDefFields = {
67
67
  [Key: ObjectFieldKey]: AnyTypeDef,
68
68
  }
69
69
 
70
70
  // NOTE: we cannot collapse this type to
71
- // `StructuredTypeDef = StructuredTypeDefFields`
71
+ // `ObjectTypeDef = ObjectTypeDefFields`
72
72
  // as we rely on the `fields` field being unique to discriminate between different
73
73
  // TypeDefs
74
74
  export type StrictObjectTypeDef<
75
- Fields extends StrictStructuredTypeDefFields = StrictStructuredTypeDefFields,
75
+ Fields extends StrictObjectTypeDefFields = StrictObjectTypeDefFields,
76
76
  > = {
77
77
  readonly type: TypeDefType.Object,
78
78
  readonly fields: Fields,
@@ -0,0 +1,178 @@
1
+ import {
2
+ type IsFieldReadonly,
3
+ map,
4
+ UnreachableError,
5
+ } from '@strictly/base'
6
+ import {
7
+ type ListTypeDef,
8
+ type LiteralTypeDef,
9
+ type ObjectFieldKey,
10
+ type ObjectTypeDef,
11
+ type RecordTypeDef,
12
+ type Type,
13
+ type TypeDef,
14
+ TypeDefType,
15
+ type UnionTypeDef,
16
+ } from 'types/definitions'
17
+
18
+ // useful for turning annotated type to type (e.g. Removing validation from ValidatingDefinition)
19
+ export type TypeOfType<T extends Type> = T extends Type<infer D> ? {
20
+ readonly definition: TypeDefOfTypeDef<D>,
21
+ }
22
+ : never
23
+
24
+ type TypeDefOfTypeDef<D extends TypeDef> = D extends LiteralTypeDef ? TypeDefOfLiteralTypeDef<D>
25
+ : D extends ListTypeDef ? TypeDefOfListTypeDef<D>
26
+ : D extends RecordTypeDef ? TypeDefOfRecordTypeDef<D>
27
+ : D extends ObjectTypeDef ? TypeDefOfObjectTypeDef<D>
28
+ : D extends UnionTypeDef ? TypeDefOfUnionTypeDef<D>
29
+ : never
30
+
31
+ type TypeDefOfLiteralTypeDef<T extends LiteralTypeDef> = T extends LiteralTypeDef<infer V> ? {
32
+ readonly type: TypeDefType.Literal,
33
+ readonly valuePrototype: [V],
34
+ }
35
+ : never
36
+
37
+ type TypeDefOfListTypeDef<T extends ListTypeDef> = T extends ListTypeDef<infer E>
38
+ ? IsFieldReadonly<T, 'elements'> extends true ? {
39
+ readonly type: TypeDefType.List,
40
+ readonly elements: TypeDefOfTypeDef<E>,
41
+ }
42
+ : {
43
+ readonly type: TypeDefType.List,
44
+ elements: TypeDefOfTypeDef<E>,
45
+ }
46
+ : never
47
+
48
+ type TypeDefOfRecordTypeDef<T extends RecordTypeDef> = T extends RecordTypeDef<infer K, infer V>
49
+ ? undefined extends V ? IsFieldReadonly<T, 'valueTypeDef'> extends true ? {
50
+ readonly type: TypeDefType.Record,
51
+ readonly keyPrototype: K,
52
+ readonly valueTypeDef: TypeDefOfTypeDef<NonNullable<V>> | undefined,
53
+ }
54
+ : {
55
+ readonly type: TypeDefType.Record,
56
+ readonly keyPrototype: K,
57
+ valueTypeDef: TypeDefOfTypeDef<NonNullable<V>> | undefined,
58
+ }
59
+ : IsFieldReadonly<T, 'valueTypeDef'> extends true ? {
60
+ readonly type: TypeDefType.Record,
61
+ readonly keyPrototype: K,
62
+ readonly valueTypeDef: TypeDefOfTypeDef<NonNullable<V>>,
63
+ }
64
+ : {
65
+ readonly type: TypeDefType.Record,
66
+ readonly keyPrototype: K,
67
+ valueTypeDef: TypeDefOfTypeDef<NonNullable<V>>,
68
+ }
69
+ : never
70
+
71
+ type TypeDefOfObjectTypeDef<T extends ObjectTypeDef> = T extends ObjectTypeDef<infer Fields> ? {
72
+ readonly type: TypeDefType.Object,
73
+ readonly fields: {
74
+ [K in keyof Fields as K extends ObjectFieldKey ? K : never]: TypeDefOfTypeDef<NonNullable<Fields[K]>>
75
+ },
76
+ }
77
+ : never
78
+
79
+ type TypeDefOfUnionTypeDef<T extends UnionTypeDef> = T extends UnionTypeDef<infer D, infer U> ? {
80
+ readonly type: TypeDefType.Union,
81
+ readonly discriminator: D,
82
+ readonly unions: {
83
+ [K in keyof U]: TypeDefOfTypeDef<U[K]>
84
+ },
85
+ }
86
+ : never
87
+
88
+ export function typeOfType<T extends Type>({ definition }: T): TypeOfType<T> {
89
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
90
+ return {
91
+ definition: typeDefOfTypeDef(definition),
92
+ } as TypeOfType<T>
93
+ }
94
+
95
+ function typeDefOfTypeDef<T extends TypeDef>(t: T): TypeDefOfTypeDef<T> {
96
+ switch (t.type) {
97
+ case TypeDefType.Literal:
98
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
99
+ return typeDefOfLiteralTypeDef(t) as TypeDefOfTypeDef<T>
100
+ case TypeDefType.List:
101
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
102
+ return typeDefOfListTypeDef(t) as TypeDefOfTypeDef<T>
103
+ case TypeDefType.Record:
104
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
105
+ return typeDefOfRecordTypeDef(t) as TypeDefOfTypeDef<T>
106
+ case TypeDefType.Object:
107
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
108
+ return typeDefOfObjectTypeDef(t) as TypeDefOfTypeDef<T>
109
+ case TypeDefType.Union:
110
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
111
+ return typeDefOfUnionTypeDef(t) as TypeDefOfTypeDef<T>
112
+ default:
113
+ throw new UnreachableError(t)
114
+ }
115
+ }
116
+
117
+ function typeDefOfLiteralTypeDef<T extends LiteralTypeDef>({
118
+ type,
119
+ valuePrototype,
120
+ }: T): TypeDefOfLiteralTypeDef<T> {
121
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
122
+ return {
123
+ type,
124
+ valuePrototype,
125
+ } as TypeDefOfLiteralTypeDef<T>
126
+ }
127
+
128
+ function typeDefOfListTypeDef<T extends ListTypeDef>({
129
+ type,
130
+ elements,
131
+ }: T): TypeDefOfListTypeDef<T> {
132
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
133
+ return {
134
+ type,
135
+ elements: typeDefOfTypeDef(elements),
136
+ } as TypeDefOfListTypeDef<T>
137
+ }
138
+
139
+ function typeDefOfRecordTypeDef<T extends RecordTypeDef>({
140
+ type,
141
+ keyPrototype,
142
+ valueTypeDef,
143
+ }: T): TypeDefOfRecordTypeDef<T> {
144
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
145
+ return {
146
+ type,
147
+ keyPrototype,
148
+ valueTypeDef: typeDefOfTypeDef(valueTypeDef),
149
+ } as TypeDefOfRecordTypeDef<T>
150
+ }
151
+
152
+ function typeDefOfObjectTypeDef<T extends ObjectTypeDef>({
153
+ type,
154
+ fields,
155
+ }: T): TypeDefOfObjectTypeDef<T> {
156
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
157
+ return {
158
+ type,
159
+ fields: map(fields, function (_k, v) {
160
+ return typeDefOfTypeDef(v)
161
+ }),
162
+ } as TypeDefOfObjectTypeDef<T>
163
+ }
164
+
165
+ function typeDefOfUnionTypeDef<T extends UnionTypeDef>({
166
+ type,
167
+ discriminator,
168
+ unions,
169
+ }: T): TypeDefOfUnionTypeDef<T> {
170
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
171
+ return {
172
+ type,
173
+ discriminator,
174
+ unions: map(unions, function (_k, v) {
175
+ return typeDefOfTypeDef(v)
176
+ }),
177
+ } as TypeDefOfUnionTypeDef<T>
178
+ }
@@ -0,0 +1,109 @@
1
+ import {
2
+ type ObjectFieldKey,
3
+ type RecordKeyType,
4
+ type TypeDefType,
5
+ type UnionKey,
6
+ } from './definitions'
7
+
8
+ // type defs with error types, lets us attach validation to types
9
+
10
+ export type ValidatingType<T extends ValidatingTypeDef = ValidatingTypeDef> = {
11
+ readonly definition: T,
12
+ }
13
+
14
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
+ export type Rule<E = any, V = any> = (v: V) => E | null
16
+
17
+ export type ErrorOfValidatingTypeDef<T extends ValidatingTypeDef> = T extends ValidatingTypeDef<infer E> ? E : never
18
+
19
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
+ export type ValidatingTypeDef<E = any> =
21
+ | ValidatingLiteralTypeDef<E>
22
+ | ValidatingListTypeDef<E>
23
+ | ValidatingRecordTypeDef<E>
24
+ | ValidatingObjectTypeDef<E>
25
+ | ValidatingUnionTypeDef<E>
26
+
27
+ // used to avoid TS complaining about circular references
28
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
+ type AnyTypeDef = any
30
+
31
+ // literal
32
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
+ export type ValidatingLiteralTypeDef<E = any, V = any> = {
34
+ readonly type: TypeDefType.Literal,
35
+ readonly valuePrototype: [V],
36
+ readonly rule: Rule<E>,
37
+ readonly required: boolean,
38
+ readonly readonly: boolean,
39
+ }
40
+
41
+ // list
42
+ export type ValidatingListTypeDef<
43
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
44
+ E = any,
45
+ Ele extends ValidatingTypeDef = AnyTypeDef,
46
+ > = {
47
+ readonly type: TypeDefType.List,
48
+ // readonly is inherited by the output
49
+ readonly elements: Ele,
50
+ readonly rule: Rule<E>,
51
+ readonly required: boolean,
52
+ readonly readonly: boolean,
53
+ }
54
+
55
+ // map
56
+ export type ValidatingRecordTypeDef<
57
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
+ E = any,
59
+ K extends RecordKeyType = RecordKeyType,
60
+ // if `V` includes `undefined` the map is partial
61
+ V extends ValidatingTypeDef | undefined = AnyTypeDef,
62
+ > = {
63
+ readonly type: TypeDefType.Record,
64
+ // never actually populate
65
+ readonly keyPrototype: K,
66
+ // readonly is inherited by the output
67
+ readonly valueTypeDef: V,
68
+ readonly rule: Rule<E>,
69
+ readonly required: boolean,
70
+ readonly readonly: boolean,
71
+ }
72
+
73
+ // structured type
74
+
75
+ // NOTE we use the `readonly` and `?` (partial) status of these field definitions
76
+ // to describe the same attributes of the fields
77
+ export type ValidatingObjectTypeDefFields = {
78
+ [Key: ObjectFieldKey]: AnyTypeDef,
79
+ }
80
+
81
+ // NOTE: we cannot collapse this type to
82
+ // `StructuredTypeDef = StructuredTypeDefFields`
83
+ // as we rely on the `fields` field being unique to discriminate between different
84
+ // TypeDefs
85
+ export type ValidatingObjectTypeDef<
86
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
87
+ E = any,
88
+ Fields extends ValidatingObjectTypeDefFields = ValidatingObjectTypeDefFields,
89
+ > = {
90
+ readonly type: TypeDefType.Object,
91
+ readonly fields: Fields,
92
+ readonly rule: Rule<E>,
93
+ readonly required: boolean,
94
+ readonly readonly: boolean,
95
+ }
96
+
97
+ export type ValidatingUnionTypeDef<
98
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
99
+ E = any,
100
+ D extends string | null = string | null,
101
+ U extends Readonly<Record<UnionKey, AnyTypeDef>> = Readonly<Record<UnionKey, AnyTypeDef>>,
102
+ > = {
103
+ readonly discriminator: D,
104
+ readonly type: TypeDefType.Union,
105
+ readonly unions: U,
106
+ readonly rule: Rule<E>,
107
+ readonly required: boolean,
108
+ readonly readonly: boolean,
109
+ }
@@ -0,0 +1,93 @@
1
+ import { type IsFieldReadonly } from '@strictly/base'
2
+ import {
3
+ type TypeDefType,
4
+ } from 'types/definitions'
5
+ import {
6
+ type Rule,
7
+ type ValidatingListTypeDef,
8
+ type ValidatingLiteralTypeDef,
9
+ type ValidatingObjectTypeDef,
10
+ type ValidatingRecordTypeDef,
11
+ type ValidatingTypeDef,
12
+ type ValidatingUnionTypeDef,
13
+ } from './validating_definitions'
14
+
15
+ // doesn't work, loses
16
+ // export type ValidatingTypeDefWithError2<T extends ValidatingTypeDef, E2> = T extends ValidatingTypeDef<infer E1>
17
+ // ? T & { rule: Rule<E1 | E2> }
18
+ // : never
19
+
20
+ export type ValidatingTypeDefWithError<T extends ValidatingTypeDef, E> = T extends ValidatingLiteralTypeDef
21
+ ? ValidatingLiteralTypeDefWithError<T, E>
22
+ : T extends ValidatingListTypeDef ? ValidatingListTypeDefWithError<T, E>
23
+ : T extends ValidatingRecordTypeDef ? ValidatingRecordTypeDefWithError<T, E>
24
+ : T extends ValidatingObjectTypeDef ? ValidatingObjectTypeDefWithError<T, E>
25
+ : T extends ValidatingUnionTypeDef ? ValidatingUnionTypeDefWithError<T, E>
26
+ : never
27
+
28
+ type ValidatingLiteralTypeDefWithError<T extends ValidatingLiteralTypeDef, E2> = T extends
29
+ ValidatingLiteralTypeDef<infer E1, infer V> ? {
30
+ readonly type: TypeDefType.Literal,
31
+ readonly valuePrototype: [V],
32
+ readonly rule: Rule<E1 | E2>,
33
+ readonly required: boolean,
34
+ readonly readonly: boolean,
35
+ }
36
+ : never
37
+
38
+ type ValidatingListTypeDefWithError<T extends ValidatingListTypeDef, E2> = T extends
39
+ ValidatingListTypeDef<infer E1, infer E> ? IsFieldReadonly<T, 'elements'> extends true ? {
40
+ readonly type: TypeDefType.List,
41
+ readonly elements: E,
42
+ readonly rule: Rule<E1 | E2>,
43
+ readonly required: boolean,
44
+ readonly readonly: boolean,
45
+ }
46
+ : {
47
+ readonly type: TypeDefType.List,
48
+ elements: E,
49
+ readonly rule: Rule<E1 | E2>,
50
+ readonly required: boolean,
51
+ readonly readonly: boolean,
52
+ }
53
+ : never
54
+
55
+ type ValidatingRecordTypeDefWithError<T extends ValidatingRecordTypeDef, E2> = T extends
56
+ ValidatingRecordTypeDef<infer E1, infer K, infer V> ? IsFieldReadonly<T, 'valueTypeDef'> extends true ? {
57
+ readonly type: TypeDefType.Record,
58
+ readonly keyPrototype: K,
59
+ readonly valueTypeDef: V,
60
+ readonly rule: Rule<E1 | E2>,
61
+ readonly required: boolean,
62
+ readonly readonly: boolean,
63
+ }
64
+ : {
65
+ readonly type: TypeDefType.Record,
66
+ readonly keyPrototype: K,
67
+ valueTypeDef: V,
68
+ readonly rule: Rule<E1 | E2>,
69
+ readonly required: boolean,
70
+ readonly readonly: boolean,
71
+ }
72
+ : never
73
+
74
+ type ValidatingObjectTypeDefWithError<T extends ValidatingObjectTypeDef, E2> = T extends
75
+ ValidatingObjectTypeDef<infer E1, infer Fields> ? {
76
+ readonly type: TypeDefType.Object,
77
+ readonly fields: Fields,
78
+ readonly rule: Rule<E1 | E2>,
79
+ readonly required: boolean,
80
+ readonly readonly: boolean,
81
+ }
82
+ : never
83
+
84
+ type ValidatingUnionTypeDefWithError<T extends ValidatingUnionTypeDef, E2> = T extends
85
+ ValidatingUnionTypeDef<infer E1, infer D, infer U> ? {
86
+ readonly type: TypeDefType.Union,
87
+ readonly discriminator: D,
88
+ readonly unions: U,
89
+ readonly rule: Rule<E1 | E2>,
90
+ readonly required: boolean,
91
+ readonly readonly: boolean,
92
+ }
93
+ : never
@@ -0,0 +1,84 @@
1
+ import {
2
+ type IsFieldReadonly,
3
+ } from '@strictly/base'
4
+ import {
5
+ type ListTypeDef,
6
+ type LiteralTypeDef,
7
+ type ObjectTypeDef,
8
+ type RecordTypeDef,
9
+ type Type,
10
+ type TypeDef,
11
+ type UnionTypeDef,
12
+ } from './definitions'
13
+
14
+ export type ValueOfType<
15
+ T,
16
+ Extra = {},
17
+ > = T extends Type ? ValueOfTypeDef<T['definition'], Extra> : never
18
+
19
+ export type ValueOfTypeDef<
20
+ F extends TypeDef,
21
+ Extra = {},
22
+ > = F extends LiteralTypeDef ? ValueOfLiteralTypeDef<F>
23
+ : F extends ListTypeDef ? ValueOfListTypeDef<F, Extra>
24
+ : F extends RecordTypeDef ? ValueOfRecordTypeDef<F, Extra>
25
+ : F extends ObjectTypeDef ? ValueOfObjectTypeDef<F, Extra>
26
+ : F extends UnionTypeDef ? ValueOfUnionTypeDef<F, Extra>
27
+ : never
28
+
29
+ type ValueOfLiteralTypeDef<F extends LiteralTypeDef> = F['valuePrototype'][number]
30
+
31
+ type ValueOfListTypeDef<F extends ListTypeDef, Extra> = IsFieldReadonly<F, 'elements'> extends true
32
+ ? readonly ValueOfTypeDef<
33
+ F['elements'],
34
+ Extra
35
+ >[] & Extra
36
+ : ValueOfTypeDef<
37
+ F['elements'],
38
+ Extra
39
+ >[] & Extra
40
+
41
+ type ValueOfRecordTypeDef<
42
+ F extends RecordTypeDef,
43
+ Extra,
44
+ > = undefined extends F['valueTypeDef']
45
+ // partial
46
+ ? IsFieldReadonly<F, 'valueTypeDef'> extends true
47
+ // readonly
48
+ ? {
49
+ readonly [k in F['keyPrototype']]?: ValueOfTypeDef<F['valueTypeDef'], Extra>
50
+ }
51
+ : {
52
+ [k in F['keyPrototype']]?: ValueOfTypeDef<F['valueTypeDef'], Extra>
53
+ }
54
+ // complete
55
+ : IsFieldReadonly<F, 'valueTypeDef'> extends true
56
+ // readonly
57
+ ? {
58
+ readonly [k in F['keyPrototype']]: ValueOfTypeDef<F['valueTypeDef'], Extra>
59
+ }
60
+ : {
61
+ [k in F['keyPrototype']]: ValueOfTypeDef<F['valueTypeDef'], Extra>
62
+ }
63
+
64
+ type ValueOfObjectTypeDef<
65
+ F extends ObjectTypeDef,
66
+ Extra,
67
+ > = F extends ObjectTypeDef<infer Fields> ? {
68
+ [K in keyof Fields]: ValueOfTypeDef<
69
+ Fields[K],
70
+ Extra
71
+ >
72
+ } & Extra
73
+ : never
74
+
75
+ type ValueOfUnionTypeDef<
76
+ F extends UnionTypeDef,
77
+ Extra,
78
+ > = F extends UnionTypeDef<infer D, infer U> ? D extends string ? {
79
+ [K in keyof U]: ValueOfTypeDef<U[K], Extra> & Readonly<Record<D, K>>
80
+ }[keyof U]
81
+ : {
82
+ [K in keyof U]: ValueOfTypeDef<U[K], Extra>
83
+ }[keyof U]
84
+ : never