inferred-types 0.37.0 → 0.37.1

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 (280) hide show
  1. package/dist/index.d.ts +2745 -0
  2. package/dist/index.js +854 -0
  3. package/package.json +6 -12
  4. package/.eslintrc +0 -77
  5. package/.github/workflows/main.yml +0 -34
  6. package/.github/workflows/other.yml +0 -15
  7. package/.nvmrc +0 -1
  8. package/.prettierrc.js +0 -3
  9. package/.vscode/launch.json +0 -27
  10. package/.vscode/settings.json +0 -19
  11. package/devops/build.js +0 -243
  12. package/docs/builder-overview.md +0 -45
  13. package/docs/builder.md +0 -94
  14. package/src/errors/ReadOnlyViolation.ts +0 -3
  15. package/src/errors/index.ts +0 -12
  16. package/src/index.ts +0 -14
  17. package/src/runtime/README.md +0 -3
  18. package/src/runtime/api/api.ts +0 -10
  19. package/src/runtime/api/index.ts +0 -12
  20. package/src/runtime/builders/Model.ts +0 -25
  21. package/src/runtime/builders/url.ts +0 -15
  22. package/src/runtime/combinators/and.ts +0 -15
  23. package/src/runtime/combinators/filter.ts +0 -267
  24. package/src/runtime/combinators/index.ts +0 -4
  25. package/src/runtime/combinators/not.ts +0 -15
  26. package/src/runtime/combinators/or.ts +0 -5
  27. package/src/runtime/createFnWithProps.ts +0 -37
  28. package/src/runtime/dictionary/arrayToKeyLookup.ts +0 -19
  29. package/src/runtime/dictionary/defineProperties.ts +0 -35
  30. package/src/runtime/dictionary/dictArr.ts +0 -69
  31. package/src/runtime/dictionary/dictionaryTransform.ts +0 -22
  32. package/src/runtime/dictionary/entries.ts +0 -33
  33. package/src/runtime/dictionary/index.ts +0 -21
  34. package/src/runtime/dictionary/kv/dictToKv.ts +0 -21
  35. package/src/runtime/dictionary/kv/filterDictArray.ts +0 -31
  36. package/src/runtime/dictionary/kv/index.ts +0 -15
  37. package/src/runtime/dictionary/kv/kv.ts +0 -18
  38. package/src/runtime/dictionary/kv/kvToDict.ts +0 -24
  39. package/src/runtime/dictionary/mapTo.ts +0 -206
  40. package/src/runtime/dictionary/mapValues.ts +0 -23
  41. package/src/runtime/dictionary/merge.ts +0 -36
  42. package/src/runtime/dictionary/strArrayToDict.ts +0 -12
  43. package/src/runtime/index.ts +0 -23
  44. package/src/runtime/keys.ts +0 -16
  45. package/src/runtime/lists/asArray.ts +0 -38
  46. package/src/runtime/lists/createConverter.ts +0 -62
  47. package/src/runtime/lists/groupBy.ts +0 -15
  48. package/src/runtime/lists/index.ts +0 -15
  49. package/src/runtime/literals/ExplicitFunction.ts +0 -10
  50. package/src/runtime/literals/Suggest.ts +0 -19
  51. package/src/runtime/literals/arrayToObject.ts +0 -78
  52. package/src/runtime/literals/box.ts +0 -98
  53. package/src/runtime/literals/defineType.ts +0 -20
  54. package/src/runtime/literals/ensureLeading.ts +0 -17
  55. package/src/runtime/literals/ensureTrailing.ts +0 -17
  56. package/src/runtime/literals/identity.ts +0 -12
  57. package/src/runtime/literals/index.ts +0 -23
  58. package/src/runtime/literals/literal.ts +0 -48
  59. package/src/runtime/literals/pathJoin.ts +0 -33
  60. package/src/runtime/literals/stripLeading.ts +0 -15
  61. package/src/runtime/literals/stripTrailing.ts +0 -15
  62. package/src/runtime/literals/wide.ts +0 -13
  63. package/src/runtime/ruleset.ts +0 -42
  64. package/src/runtime/runtime/README.md +0 -3
  65. package/src/runtime/runtime/condition.ts +0 -13
  66. package/src/runtime/runtime/index.ts +0 -16
  67. package/src/runtime/runtime/type.ts +0 -127
  68. package/src/runtime/runtime/withValue.ts +0 -39
  69. package/src/runtime/state/Configurator.ts +0 -76
  70. package/src/runtime/state/FluentConfigurator.ts +0 -49
  71. package/src/runtime/state/index.ts +0 -13
  72. package/src/runtime/type-checks/ifSameType.ts +0 -20
  73. package/src/runtime/type-checks/ifTypeOf.ts +0 -124
  74. package/src/runtime/type-checks/index.ts +0 -24
  75. package/src/runtime/type-checks/isArray.ts +0 -34
  76. package/src/runtime/type-checks/isBoolean.ts +0 -24
  77. package/src/runtime/type-checks/isFalse.ts +0 -29
  78. package/src/runtime/type-checks/isFunction.ts +0 -26
  79. package/src/runtime/type-checks/isNull.ts +0 -26
  80. package/src/runtime/type-checks/isNumber.ts +0 -20
  81. package/src/runtime/type-checks/isObject.ts +0 -22
  82. package/src/runtime/type-checks/isString.ts +0 -34
  83. package/src/runtime/type-checks/isSymbol.ts +0 -3
  84. package/src/runtime/type-checks/isTrue.ts +0 -31
  85. package/src/runtime/type-checks/isUndefined.ts +0 -33
  86. package/src/runtime/type-checks/startsWith.ts +0 -107
  87. package/src/runtime/valueTypes.ts +0 -48
  88. package/src/types/Api.ts +0 -16
  89. package/src/types/Constructor.ts +0 -5
  90. package/src/types/EnumValues.ts +0 -14
  91. package/src/types/ExpandRecursively.ts +0 -12
  92. package/src/types/FunctionType.ts +0 -19
  93. package/src/types/If.ts +0 -10
  94. package/src/types/Include.ts +0 -18
  95. package/src/types/KeyedRecord.ts +0 -21
  96. package/src/types/Keys.ts +0 -26
  97. package/src/types/Length.ts +0 -8
  98. package/src/types/Mutable.ts +0 -8
  99. package/src/types/Narrowable.ts +0 -5
  100. package/src/types/Not.ts +0 -13
  101. package/src/types/Numeric.ts +0 -20
  102. package/src/types/Opaque.ts +0 -11
  103. package/src/types/README.md +0 -3
  104. package/src/types/Retain.ts +0 -16
  105. package/src/types/SimplifyObject.ts +0 -12
  106. package/src/types/Transformer.ts +0 -15
  107. package/src/types/TypeGuard.ts +0 -7
  108. package/src/types/Where.ts +0 -40
  109. package/src/types/alphabetic/AllCaps.ts +0 -15
  110. package/src/types/alphabetic/CamelCase.ts +0 -3
  111. package/src/types/alphabetic/CapFirstAlpha.ts +0 -23
  112. package/src/types/alphabetic/CapitalizeWords.ts +0 -9
  113. package/src/types/alphabetic/Cardinality.ts +0 -80
  114. package/src/types/alphabetic/DashToSnake.ts +0 -3
  115. package/src/types/alphabetic/DashUppercase.ts +0 -19
  116. package/src/types/alphabetic/Dasherize.ts +0 -23
  117. package/src/types/alphabetic/EnsureLeading.ts +0 -24
  118. package/src/types/alphabetic/EnsureTrailing.ts +0 -24
  119. package/src/types/alphabetic/HasUppercase.ts +0 -16
  120. package/src/types/alphabetic/IsCapitalized.ts +0 -18
  121. package/src/types/alphabetic/KebabCase.ts +0 -13
  122. package/src/types/alphabetic/LowerAllCaps.ts +0 -5
  123. package/src/types/alphabetic/PascalCase.ts +0 -29
  124. package/src/types/alphabetic/PathJoin.ts +0 -54
  125. package/src/types/alphabetic/Pluralize.ts +0 -112
  126. package/src/types/alphabetic/SnakeCase.ts +0 -27
  127. package/src/types/alphabetic/StripLeading.ts +0 -23
  128. package/src/types/alphabetic/StripTrailing.ts +0 -23
  129. package/src/types/alphabetic/Url.ts +0 -29
  130. package/src/types/alphabetic/alpha-characters.ts +0 -81
  131. package/src/types/alphabetic/index.ts +0 -32
  132. package/src/types/append-types.ts +0 -10
  133. package/src/types/boolean-logic/EndsWith.ts +0 -40
  134. package/src/types/boolean-logic/Extends.ts +0 -22
  135. package/src/types/boolean-logic/HasParameters.ts +0 -21
  136. package/src/types/boolean-logic/Includes.ts +0 -33
  137. package/src/types/boolean-logic/IsLiteral.ts +0 -101
  138. package/src/types/boolean-logic/IsScalar.ts +0 -20
  139. package/src/types/boolean-logic/IsUndefined.ts +0 -20
  140. package/src/types/boolean-logic/Or.ts +0 -12
  141. package/src/types/boolean-logic/StartsWith.ts +0 -42
  142. package/src/types/boolean-logic/TypeDefault.ts +0 -58
  143. package/src/types/boolean-logic/array.ts +0 -57
  144. package/src/types/boolean-logic/boolean.ts +0 -84
  145. package/src/types/boolean-logic/equivalency.ts +0 -15
  146. package/src/types/boolean-logic/index.ts +0 -14
  147. package/src/types/boolean-logic/object.ts +0 -30
  148. package/src/types/boolean-logic/string.ts +0 -21
  149. package/src/types/dictionary/DictChangeValue.ts +0 -34
  150. package/src/types/dictionary/DictPartialApplication.ts +0 -30
  151. package/src/types/dictionary/DictPrependWithFn.ts +0 -23
  152. package/src/types/dictionary/DictReturnValues.ts +0 -27
  153. package/src/types/dictionary/Get.ts +0 -14
  154. package/src/types/dictionary/MapTo.ts +0 -428
  155. package/src/types/dictionary/MutableProps.ts +0 -19
  156. package/src/types/dictionary/RequireProps.ts +0 -15
  157. package/src/types/dictionary/SameKeys.ts +0 -11
  158. package/src/types/dictionary/index.ts +0 -21
  159. package/src/types/dictionary/props.ts +0 -145
  160. package/src/types/fluent/fluent.ts +0 -61
  161. package/src/types/fluent/index.ts +0 -12
  162. package/src/types/functions/FinalReturn.ts +0 -12
  163. package/src/types/functions/LogicFunction.ts +0 -4
  164. package/src/types/functions/index.ts +0 -13
  165. package/src/types/index.ts +0 -49
  166. package/src/types/kv/DictFromKv.ts +0 -3
  167. package/src/types/kv/KeyValue.ts +0 -12
  168. package/src/types/kv/KvFrom.ts +0 -8
  169. package/src/types/kv/KvTuple.ts +0 -14
  170. package/src/types/kv/index.ts +0 -15
  171. package/src/types/lists/AfterFirst.ts +0 -10
  172. package/src/types/lists/ConvertAndMap.ts +0 -152
  173. package/src/types/lists/FilterTuple.ts +0 -20
  174. package/src/types/lists/First.ts +0 -8
  175. package/src/types/lists/FirstString.ts +0 -4
  176. package/src/types/lists/Split.ts +0 -21
  177. package/src/types/lists/UniqueForProp.ts +0 -18
  178. package/src/types/lists/index.ts +0 -18
  179. package/src/types/literal-unions/OptRequired.ts +0 -4
  180. package/src/types/literal-unions/index.ts +0 -1
  181. package/src/types/maybe.ts +0 -9
  182. package/src/types/ruleset-types.ts +0 -76
  183. package/src/types/runtime.ts +0 -39
  184. package/src/types/string-literals/Break.ts +0 -22
  185. package/src/types/string-literals/Concat.ts +0 -9
  186. package/src/types/string-literals/LeftWhitespace.ts +0 -13
  187. package/src/types/string-literals/Replace.ts +0 -17
  188. package/src/types/string-literals/RightWhitespace.ts +0 -13
  189. package/src/types/string-literals/StringLength.ts +0 -34
  190. package/src/types/string-literals/Trim.ts +0 -15
  191. package/src/types/string-literals/TrimLeft.ts +0 -14
  192. package/src/types/string-literals/TrimRight.ts +0 -14
  193. package/src/types/string-literals/form-fields.ts +0 -15
  194. package/src/types/string-literals/index.ts +0 -21
  195. package/src/types/tuples/DictArray.ts +0 -21
  196. package/src/types/tuples/FirstKey.ts +0 -10
  197. package/src/types/tuples/FirstKeyValue.ts +0 -9
  198. package/src/types/tuples/FirstOfEach.ts +0 -13
  199. package/src/types/tuples/FromDictArray.ts +0 -14
  200. package/src/types/tuples/SecondOfEach.ts +0 -13
  201. package/src/types/tuples/index.ts +0 -17
  202. package/src/types/type-conversion/TupleToUnion.ts +0 -9
  203. package/src/types/type-conversion/UnionToIntersection.ts +0 -9
  204. package/src/types/type-conversion/UnionToTuple.ts +0 -20
  205. package/src/types/type-conversion/Widen.ts +0 -22
  206. package/src/types/type-conversion/index.ts +0 -16
  207. package/src/types/type-testing.ts +0 -14
  208. package/tests/CamelCase-spec.ts +0 -20
  209. package/tests/Configurator-spec.hold +0 -106
  210. package/tests/ExplicitFunction-spec.ts +0 -50
  211. package/tests/IncludeAndRetain-spec.ts +0 -67
  212. package/tests/Includes-spec.ts +0 -36
  213. package/tests/KebabCase-spec.ts +0 -21
  214. package/tests/PascalCase-spec.ts +0 -22
  215. package/tests/Pluralize-spec.ts +0 -23
  216. package/tests/SnakeCase-spec.ts +0 -21
  217. package/tests/Where.spec.ts +0 -38
  218. package/tests/arrayToKeyLookup-spec.ts +0 -17
  219. package/tests/arrayToObject-spec.ts +0 -115
  220. package/tests/boolean-logic/Contains.test.ts +0 -55
  221. package/tests/boolean-logic/HasParameters.ts +0 -29
  222. package/tests/boolean-logic/IsLiteral.spec.ts +0 -65
  223. package/tests/boolean-logic/boolean.spec.ts +0 -21
  224. package/tests/boolean-logic/filter.spec.ts +0 -52
  225. package/tests/constructor.spec.ts +0 -42
  226. package/tests/createFnWithProps.spec.ts +0 -14
  227. package/tests/data/SimpleTable.hold +0 -21
  228. package/tests/data/index.ts +0 -12
  229. package/tests/data/playlist.hold +0 -25
  230. package/tests/data/song.hold +0 -23
  231. package/tests/defineType-spec.ts +0 -57
  232. package/tests/dictionary/DictChangeValue.test.ts +0 -30
  233. package/tests/dictionary/DictPartialApplication.test.ts +0 -43
  234. package/tests/dictionary/DictReturnValues.test.ts +0 -36
  235. package/tests/dictionary/Get.spec.ts +0 -28
  236. package/tests/dictionary/IntersectingKeys.test.ts +0 -42
  237. package/tests/dictionary/MutableProps.test.ts +0 -30
  238. package/tests/dictionary/OptionalKeys.test.ts +0 -24
  239. package/tests/dictionary/PrependValuesWithFunction.test.ts +0 -34
  240. package/tests/dictionary/RequireProps.test.ts +0 -21
  241. package/tests/dictionary/RequiredKeys.test.ts +0 -53
  242. package/tests/dictionary/TypeDefault.test.ts +0 -76
  243. package/tests/dictionary/mapTo.test.ts +0 -388
  244. package/tests/dictionary/merge.test.ts +0 -41
  245. package/tests/dictionaryTransform-spec.ts +0 -280
  246. package/tests/ifTypeOf-spec.ts +0 -191
  247. package/tests/kv/KeyValue-spec.ts +0 -22
  248. package/tests/kv/SameKeys-spec.ts +0 -35
  249. package/tests/kv/dict-to-kv-and-back.spec.ts +0 -169
  250. package/tests/kv/entries-spec.ts +0 -65
  251. package/tests/kv/keys.spec.ts +0 -66
  252. package/tests/kv/kv-spec.ts +0 -20
  253. package/tests/lists/First.test.ts +0 -43
  254. package/tests/lists/Length.test.ts +0 -19
  255. package/tests/lists/Split.test.ts +0 -29
  256. package/tests/lists/UniqueForProp.spec.ts +0 -59
  257. package/tests/lists/asArray.test.ts +0 -109
  258. package/tests/lists/dictArr.test.ts +0 -110
  259. package/tests/literal-spec.ts +0 -82
  260. package/tests/literals/AllCaps.spec.ts +0 -33
  261. package/tests/literals/CamelCase.spec.ts +0 -109
  262. package/tests/literals/Dasherize.spec.ts +0 -107
  263. package/tests/literals/EnsureStripLeadingTrailing.test.ts +0 -79
  264. package/tests/literals/HasUppercase.spec.ts +0 -33
  265. package/tests/literals/PascalCase.spec.ts +0 -109
  266. package/tests/literals/PathJoin.test.ts +0 -111
  267. package/tests/literals/SnakeCase.spec.ts +0 -109
  268. package/tests/literals/box.test.ts +0 -85
  269. package/tests/literals/whitespace-capture.spec.ts +0 -60
  270. package/tests/mapValues-spec.ts +0 -50
  271. package/tests/props-spec.ts +0 -234
  272. package/tests/runtime/condition.spec.ts +0 -19
  273. package/tests/runtime/if-is.spec.ts +0 -390
  274. package/tests/runtime/map-and-convert.test.ts +0 -31
  275. package/tests/runtime/type.spec.ts +0 -190
  276. package/tests/strArrayToDict-spec.ts +0 -40
  277. package/tests/tuples/TupleToUnion.spec.ts +0 -20
  278. package/tests/withValue.spec.ts +0 -173
  279. package/tsconfig.json +0 -36
  280. package/vitest.config.ts +0 -17
@@ -1,85 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import { Equal, Expect } from "@type-challenges/utils";
3
- import { Box, box, BoxedFnParams, BoxValue, unbox } from "src/runtime";
4
- import { First } from "src/types/lists/First";
5
-
6
- // [Instantiation Expressions](https://devblogs.microsoft.com/typescript/announcing-typescript-4-7-beta/#instantiation-expressions)
7
-
8
- // this exercise was started around the time that Instantiation expressions
9
- // were first introduced and with the hope that this would help narrow functions
10
- // which have an inner generic that provides narrow typing.
11
-
12
- describe("boxing / unboxing", () => {
13
- it("box a function with generic", () => {
14
- const fn = <T extends string>(i: T) => `Hello ${i}` as const;
15
- const b = box(fn);
16
- const globalUnbox = unbox(b);
17
- type UB = typeof globalUnbox;
18
- const usingValue = b.value("foo");
19
- const unboxedNarrow = b.unbox("foo");
20
- const unboxedWide = b.unbox("foo" as string);
21
-
22
- const bn = box(42);
23
- type BN = typeof bn;
24
- const rn = bn.unbox();
25
- type RN = typeof rn;
26
- const rn2 = bn.value;
27
- type RN2 = typeof rn2;
28
-
29
- type Hybrid = 42 | 56 | typeof fn;
30
- const h = box(42 as Hybrid);
31
- type H = typeof h;
32
- type HV = H["value"];
33
-
34
- // runtime
35
- expect(typeof b).toBe("object");
36
- expect(
37
- typeof globalUnbox,
38
- `using unbox(fn) should have returned function: ${globalUnbox}`
39
- ).toBe("function");
40
- expect(usingValue).toBe("Hello foo");
41
- expect(unboxedNarrow).toBe("Hello foo");
42
- expect(unboxedWide).toBe("Hello foo");
43
-
44
- expect(rn).toBe(42);
45
- expect(rn2).toBe(42);
46
-
47
- // type tests
48
- type B = typeof b;
49
- type BV = BoxValue<B>;
50
- type BF = First<BoxedFnParams<B>>;
51
-
52
- type cases = [
53
- // FUNCTIONS
54
- Expect<Equal<B, Box<typeof fn>>>,
55
- Expect<Equal<BV, typeof fn>>, //
56
- Expect<Equal<BF, string>>,
57
- /** unboxing fn results in same fn including generics */
58
- Expect<Equal<UB, typeof fn>>,
59
- /** this uses the VALUE prop and provides the TARGET resolution for unbox */
60
- Expect<Equal<typeof usingValue, "Hello foo">>,
61
- /**
62
- * using the passthrough feature of `b.unbox(val)` we would ideally like to
63
- * get same narrow type as test above but it is reduced to an "ok" level for
64
- * now
65
- */
66
- Expect<Equal<typeof unboxedNarrow, `Hello ${string}`>>,
67
- /**
68
- * Note, however, that the wide value of string passed in resolves in the same
69
- * manner which helps to point to where resolution is being lost
70
- */
71
- Expect<Equal<typeof unboxedWide, `Hello ${string}`>>,
72
-
73
- // NUMBER BOXes
74
- Expect<Equal<BN, Box<42>>>,
75
- /** unboxing preserves numeric literal */
76
- Expect<Equal<RN, 42>>,
77
- /** can pick off the value prop for same type */
78
- Expect<Equal<RN2, 42>>,
79
-
80
- // HYBRID
81
- Expect<Equal<HV, Hybrid>>
82
- ];
83
- const cases: cases = [true, true, true, true, true, true, true, true, true, true, true];
84
- });
85
- });
@@ -1,60 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
-
3
- import { Equal, Expect } from "@type-challenges/utils";
4
- import { LeftWhitespace, RightWhitespace } from "src/types";
5
-
6
- describe("LeftWhitespace<T> utility", () => {
7
- it("no whitespace results in empty string literal", () => {
8
- type T1 = LeftWhitespace<"foobar">;
9
- type T2 = LeftWhitespace<"foobar \n">;
10
-
11
- type cases = [Expect<Equal<T1, "">>, Expect<Equal<T2, "">>];
12
- const c: cases = [true, true];
13
- expect(c).toBe(c);
14
- });
15
-
16
- it("left whitespace is captured from string literal, right whitespace ignored", () => {
17
- type T1 = LeftWhitespace<" foobar">;
18
- type T2 = LeftWhitespace<"\n\t foobar \n">;
19
-
20
- type cases = [Expect<Equal<T1, " ">>, Expect<Equal<T2, "\n\t ">>];
21
- const c: cases = [true, true];
22
- expect(c).toBe(c);
23
- });
24
-
25
- it("a 'string' type passed in returns a string", () => {
26
- type T1 = LeftWhitespace<string>;
27
-
28
- type cases = [Expect<Equal<T1, string>>];
29
- const c: cases = [true];
30
- expect(c).toBe(c);
31
- });
32
- });
33
-
34
- describe("RightWhitespace<T> utility", () => {
35
- it("no whitespace results in empty string literal", () => {
36
- type T1 = RightWhitespace<"foobar">;
37
- type T2 = RightWhitespace<"\n\tfoobar">;
38
-
39
- type cases = [Expect<Equal<T1, "">>, Expect<Equal<T2, "">>];
40
- const c: cases = [true, true];
41
- expect(c).toBe(c);
42
- });
43
-
44
- it("right whitespace is captured from string literal, left whitespace ignored", () => {
45
- type T1 = RightWhitespace<"foobar ">;
46
- type T2 = RightWhitespace<"\n\t foobar \n">;
47
-
48
- type cases = [Expect<Equal<T1, " ">>, Expect<Equal<T2, " \n">>];
49
- const c: cases = [true, true];
50
- expect(c).toBe(c);
51
- });
52
-
53
- it("a 'string' type passed in returns a string", () => {
54
- type T1 = RightWhitespace<string>;
55
-
56
- type cases = [Expect<Equal<T1, string>>];
57
- const c: cases = [true];
58
- expect(c).toBe(c);
59
- });
60
- });
@@ -1,50 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
-
3
- import { mapValues, literal } from "../src/runtime";
4
- import type { Expect, Equal, ExpectFalse } from "@type-challenges/utils";
5
-
6
- describe("mapValues() utility", () => {
7
- it("simple transform while maintaining wide type through transform", () => {
8
- const colors = { red: 4, blue: 2, green: 3 };
9
- const x2 = mapValues(colors, (v) => v * 2);
10
-
11
- expect(x2.red).toBe(colors.red * 2);
12
- expect(x2.blue).toBe(colors.blue * 2);
13
- expect(x2.green).toBe(colors.green * 2);
14
-
15
- type cases = [
16
- // although not always the case, because the value
17
- // isn't changed in it's type properties they are
18
- // equal in _type_ after transform
19
- Expect<Equal<typeof colors, typeof x2>>,
20
- // what always should be true is that both dictionaries
21
- // share the same keys
22
- Expect<Equal<keyof typeof colors, keyof typeof x2>>
23
- ];
24
- const cases: cases = [true, true];
25
- expect(cases).toBe(cases);
26
- });
27
-
28
- it("simple transform but with input type set to literals", () => {
29
- const colors = literal({ red: 4, blue: 2, green: 3 });
30
- const x2 = mapValues(colors, (v) => v * 2);
31
-
32
- expect(x2.red).toBe(colors.red * 2);
33
- expect(x2.blue).toBe(colors.blue * 2);
34
- expect(x2.green).toBe(colors.green * 2);
35
-
36
- type cases = [
37
- // by making the initial type take on literal values
38
- // instead of generalizing to "number" we no longer
39
- // have equality of types after the transform
40
- ExpectFalse<Equal<typeof colors, typeof x2>>,
41
- // note that the transformed values are wide,
42
- // unlike the input to the process
43
- Expect<Equal<typeof x2["blue"], number>>,
44
- // As always, the keys remain the same
45
- Expect<Equal<keyof typeof colors, keyof typeof x2>>
46
- ];
47
- const cases: cases = [false, true, true];
48
- expect(cases).toBe(cases);
49
- });
50
- });
@@ -1,234 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
-
3
- import type { Expect, Equal } from "@type-challenges/utils";
4
- import {
5
- KeysWithValue,
6
- OptionalKeys,
7
- RequiredKeys,
8
- RequiredProps,
9
- WithValue,
10
- WithStringKeys,
11
- StringKeys,
12
- NumericKeys,
13
- NonStringKeys,
14
- WithNumericKeys,
15
- } from "src/types/dictionary/props";
16
-
17
- type T0 = { foo: number; bar: number; baz: string };
18
- type T1 = { foo: number; bar: number; baz?: string };
19
- type T2 = { foo: number; bar?: number; baz?: string };
20
- type T3 = { foo?: number; bar?: number; baz?: string };
21
-
22
- describe("Dictionary Type Utils", () => {
23
- it("RequiredKeys<T> extracts the right keys", () => {
24
- type cases = [
25
- Expect<Equal<RequiredKeys<T0>, "foo" | "bar" | "baz">>,
26
- Expect<Equal<RequiredKeys<T1>, "foo" | "bar">>,
27
- Expect<Equal<RequiredKeys<T2>, "foo">>,
28
- Expect<Equal<RequiredKeys<T3>, never>>
29
- ];
30
- const cases: cases = [true, true, true, true];
31
- expect(cases).toBe(cases);
32
- });
33
-
34
- it("OptionalKeys<T> extracts the right keys", () => {
35
- type cases = [
36
- Expect<Equal<OptionalKeys<T0>, never>>,
37
- Expect<Equal<OptionalKeys<T1>, "baz">>,
38
- Expect<Equal<OptionalKeys<T2>, "baz" | "bar">>,
39
- Expect<Equal<OptionalKeys<T3>, "foo" | "bar" | "baz">>
40
- ];
41
- const cases: cases = [true, true, true, true];
42
- expect(cases).toBe(cases);
43
- });
44
-
45
- it("OptionalKeys<T> extracts the right keys", () => {
46
- type cases = [
47
- Expect<Equal<OptionalKeys<T0>, never>>,
48
- Expect<Equal<OptionalKeys<T1>, "baz">>,
49
- Expect<Equal<OptionalKeys<T2>, "baz" | "bar">>,
50
- Expect<Equal<OptionalKeys<T3>, "foo" | "bar" | "baz">>
51
- ];
52
- const cases: cases = [true, true, true, true];
53
- expect(cases).toBe(cases);
54
- });
55
-
56
- it("RequiredProps<T> extracts the KV's", () => {
57
- type cases = [
58
- Expect<Equal<RequiredProps<T0>, T0>>,
59
- Expect<Equal<RequiredProps<T1>, Omit<T0, "baz">>>,
60
- Expect<Equal<RequiredProps<T2>, Omit<T2, "baz" | "bar">>>,
61
- Expect<Equal<RequiredProps<T3>, Omit<T3, "foo" | "bar" | "baz">>>
62
- ];
63
- const cases: cases = [true, true, true, true];
64
- expect(cases).toBe(cases);
65
- });
66
-
67
- type LiteralType = {
68
- foo: 1;
69
- foo2: number;
70
- bar: true;
71
- baz: false;
72
- baz2: false;
73
- wide: boolean;
74
- greet: "hi";
75
- };
76
-
77
- const narrowType = { foo: 1, foo2: 2, bar: true, baz: false, baz2: false, greet: "hi" } as const;
78
- type NarrowType = typeof narrowType;
79
- const wideType = { foo: 1, foo2: 2, bar: true, baz: false, baz2: false, greet: "hi" };
80
- type WideType = typeof wideType;
81
-
82
- it("KeysWithValue<type, obj> identifies keys of the given object which have a value of specified type", () => {
83
- type SL = KeysWithValue<string, LiteralType>;
84
- type SN = KeysWithValue<string, NarrowType>;
85
- type SW = KeysWithValue<string, WideType>;
86
-
87
- type StrLiteralNarrow = KeysWithValue<"hi", NarrowType>;
88
- type StrLiteralWide = KeysWithValue<"hi", WideType>;
89
-
90
- type Num = KeysWithValue<number, LiteralType>;
91
- type NumNarrow = KeysWithValue<number, NarrowType>;
92
- type NumWide = KeysWithValue<number, WideType>;
93
-
94
- type LT = KeysWithValue<true, LiteralType>;
95
- type LF = KeysWithValue<false, LiteralType>;
96
- type LB = KeysWithValue<boolean, LiteralType>;
97
-
98
- type TrueNarrow = KeysWithValue<true, NarrowType>;
99
- type TrueWide = KeysWithValue<true, WideType>;
100
-
101
- type BooleanNarrow = KeysWithValue<boolean, NarrowType>;
102
- type BooleanWide = KeysWithValue<boolean, WideType>;
103
-
104
- type cases = [
105
- Expect<Equal<SL, "greet">>,
106
- Expect<Equal<SN, "greet">>,
107
- Expect<Equal<SW, "greet">>,
108
-
109
- // the type "hi" IS matched with a narrow type definition
110
- Expect<Equal<StrLiteralNarrow, "greet">>,
111
- // the type "hi" is not matched to the wider type string
112
- Expect<Equal<StrLiteralWide, never>>,
113
-
114
- Expect<Equal<Num, "foo" | "foo2">>,
115
- Expect<Equal<NumNarrow, "foo" | "foo2">>,
116
- Expect<Equal<NumWide, "foo" | "foo2">>,
117
-
118
- Expect<Equal<LT, "bar">>,
119
- Expect<Equal<LF, "baz" | "baz2">>,
120
- Expect<Equal<LB, "bar" | "baz" | "baz2" | "wide">>,
121
-
122
- // the literal type "true" can be matched when dealing a literal type
123
- Expect<Equal<TrueNarrow, "bar">>,
124
- // but no keys are matched when the key's value has been broadened to "boolean"
125
- Expect<Equal<TrueWide, never>>,
126
- // regardless of whether a type is `true`, `false`, or `boolean` they all
127
- // match up with the `boolean` type
128
- Expect<Equal<BooleanNarrow, "bar" | "baz" | "baz2">>,
129
- Expect<Equal<BooleanWide, "bar" | "baz" | "baz2">>
130
- ];
131
- const cases: cases = [
132
- true,
133
- true,
134
- true,
135
- true,
136
- true,
137
- true,
138
- true,
139
- true,
140
- true,
141
- true,
142
- true,
143
- true,
144
- true,
145
- true,
146
- true,
147
- ];
148
- expect(cases).toBe(cases);
149
- });
150
-
151
- it("WithValue<type, obj> reduces the types on the object effectively", () => {
152
- type Literal = { foo: 1; bar: true; baz: false; baz2: false };
153
-
154
- type S = WithValue<string, T0>;
155
- type N = WithValue<number, T0>;
156
- type LT = WithValue<true, Literal>;
157
- type LF = WithValue<false, Literal>;
158
-
159
- type cases = [
160
- Expect<Equal<S, { baz: string }>>,
161
- Expect<Equal<N, { foo: number; bar: number }>>,
162
- Expect<Equal<LT, { bar: true }>>,
163
- Expect<Equal<LF, { baz: false; baz2: false }>>
164
- ];
165
- const cases: cases = [true, true, true, true];
166
- expect(cases).toBe(cases);
167
- });
168
-
169
- it("WithValue<type, obj> reduces the non-literal types from typeof", () => {
170
- const t = { foo: 1, bar: true, baz: false, baz2: false, xyz: "hi" };
171
- type T = typeof t;
172
-
173
- type S = WithValue<string, T>;
174
- type N = WithValue<number, T>;
175
- type B = WithValue<boolean, T>;
176
-
177
- type cases = [
178
- Expect<Equal<S, { xyz: string }>>,
179
- Expect<Equal<N, { foo: number }>>,
180
- Expect<Equal<B, { bar: boolean; baz: boolean; baz2: boolean }>>
181
- ];
182
- const cases: cases = [true, true, true];
183
- expect(cases).toBe(cases);
184
- });
185
-
186
- it("WithValue<t,o> reduces to literal types when runtime var set as 'const'", () => {
187
- const t = { foo: 1, bar: true, baz: false, baz2: false, xyz: "hi" } as const;
188
- type T = typeof t;
189
-
190
- type S = WithValue<string, T>;
191
- type N = WithValue<number, T>;
192
- type F = WithValue<false, T>;
193
-
194
- type cases = [
195
- Expect<Equal<S, { readonly xyz: "hi" }>>,
196
- Expect<Equal<N, { readonly foo: 1 }>>,
197
- Expect<Equal<F, { readonly baz: false; readonly baz2: false }>>
198
- ];
199
- const cases: cases = [true, true, true];
200
- expect(cases).toBe(cases);
201
- });
202
-
203
- it("WithValue<Function, O> reduces to kv's with a function but retains functions signatuer", () => {
204
- type T = WithValue<Function, { a: number; b: string; c: () => "hello"; d: () => "world" }>;
205
-
206
- type cases = [Expect<Equal<T, { c: () => "hello"; d: () => "world" }>>];
207
- const cases: cases = [true];
208
- expect(cases).toBe(cases);
209
- });
210
-
211
- it("WithStringKeys<T> and WithNumericKeys<T> reduce type to appropriate subset", () => {
212
- const t1 = { foo: 456, bar: "hi", 1: "a number", 2: "another pesky number" };
213
- type T1 = typeof t1;
214
- type SK = StringKeys<T1>;
215
- type NK = NumericKeys<T1>;
216
- type NonString = NonStringKeys<T1>;
217
- type StringKeysOnly = WithStringKeys<T1>;
218
- type NumericKeysOnly = WithNumericKeys<T1>;
219
-
220
- type cases = [
221
- // the building blocks is being able to determine the string
222
- // and numeric keys on an object
223
- Expect<Equal<SK, "foo" | "bar">>,
224
- Expect<Equal<NK, 1 | 2>>,
225
- Expect<Equal<NonString, 1 | 2>>,
226
- // they key based primitives allows us to to then just ask
227
- // for the object with only string-based keys represented
228
- Expect<Equal<StringKeysOnly, { foo: number; bar: string }>>,
229
- Expect<Equal<NumericKeysOnly, { 1: string; 2: string }>>
230
- ];
231
- const cases: cases = [true, true, true, true, true];
232
- expect(cases).toBe(cases);
233
- });
234
- });
@@ -1,19 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import { condition, isFunction, isTrue } from "src/runtime";
3
-
4
- describe("condition() utility", () => {
5
- it("basic type checks through condition abstraction", () => {
6
- const trueIsTrue = condition(isTrue, true);
7
- const falseAintTrue = condition(isTrue, false);
8
- const simpleFnIsAFunction = condition(isFunction, () => "hello world");
9
-
10
- // run time
11
- expect(trueIsTrue).toBe(true);
12
- expect(falseAintTrue).toBe(false);
13
- expect(simpleFnIsAFunction).toBe(true);
14
-
15
- // typing
16
-
17
- //TODO: get this working!
18
- });
19
- });