inferred-types 0.19.0 → 0.22.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 (133) hide show
  1. package/.eslintrc +36 -20
  2. package/.github/workflows/main.yml +34 -0
  3. package/.github/workflows/other.yml +15 -0
  4. package/.vscode/settings.json +10 -2
  5. package/dist/index.d.ts +403 -8538
  6. package/dist/index.js +35 -60
  7. package/dist/index.mjs +24 -48
  8. package/package.json +34 -23
  9. package/src/shared/valueTypes.ts +1 -1
  10. package/src/types/ExpandRecursively.ts +10 -1
  11. package/src/types/FunctionType.ts +1 -1
  12. package/src/types/SimplifyObject.ts +12 -0
  13. package/src/types/Transformer.ts +1 -1
  14. package/src/types/alphabetic/CamelCase.ts +1 -1
  15. package/src/types/alphabetic/CapFirstAlpha.ts +7 -3
  16. package/src/types/alphabetic/Dasherize.ts +8 -5
  17. package/src/types/alphabetic/KebabCase.ts +1 -1
  18. package/src/types/alphabetic/PascalCase.ts +3 -1
  19. package/src/types/alphabetic/SnakeCase.ts +8 -3
  20. package/src/types/dictionary/DictPartialApplication.ts +30 -0
  21. package/src/types/dictionary/DictPrependWithFn.ts +23 -0
  22. package/src/types/dictionary/DictReturnValues.ts +27 -0
  23. package/src/types/dictionary/RequireProps.ts +15 -0
  24. package/src/types/{type-conversion → dictionary}/SameKeys.ts +0 -0
  25. package/src/types/dictionary/index.ts +8 -3
  26. package/src/types/{props.ts → dictionary/props.ts} +1 -1
  27. package/src/types/functions/FinalReturn.ts +12 -0
  28. package/{on-hold/types → src/types/functions}/index.ts +4 -3
  29. package/src/types/index.ts +4 -3
  30. package/src/types/ruleset-types.ts +3 -3
  31. package/src/types/runtime.ts +18 -18
  32. package/src/types/string-literals/LeftWhitespace.ts +5 -2
  33. package/src/types/string-literals/RightWhitespace.ts +5 -2
  34. package/src/types/tuples/FirstKey.ts +3 -3
  35. package/src/types/tuples/FromDictArray.ts +5 -4
  36. package/src/types/type-conversion/index.ts +2 -5
  37. package/src/utility/api/api.ts +8 -7
  38. package/src/utility/createFnWithProps.ts +26 -1
  39. package/src/utility/dictionary/dictionaryTransform.ts +5 -4
  40. package/src/utility/dictionary/entries.ts +9 -5
  41. package/src/utility/dictionary/index.ts +2 -3
  42. package/src/utility/dictionary/kv/dictToKv.ts +5 -3
  43. package/src/utility/dictionary/kv/filterDictArray.ts +5 -7
  44. package/src/utility/dictionary/kv/kv.ts +8 -3
  45. package/src/utility/dictionary/kv/kvToDict.ts +3 -2
  46. package/src/utility/dictionary/mapValues.ts +8 -5
  47. package/src/utility/dictionary/strArrayToDict.ts +1 -2
  48. package/src/utility/keys.ts +8 -4
  49. package/src/utility/lists/groupBy.ts +2 -9
  50. package/src/utility/literals/arrayToObject.ts +29 -29
  51. package/src/utility/literals/defineType.ts +6 -6
  52. package/src/utility/literals/identity.ts +5 -2
  53. package/src/utility/literals/literal.ts +3 -3
  54. package/src/utility/ruleset.ts +11 -9
  55. package/src/utility/runtime/condition.ts +1 -1
  56. package/src/utility/runtime/conditions/isFalse.ts +2 -2
  57. package/src/utility/runtime/conditions/isFunction.ts +2 -2
  58. package/src/utility/runtime/conditions/isObject.ts +4 -1
  59. package/src/utility/runtime/conditions/isTrue.ts +1 -1
  60. package/src/utility/runtime/ifTypeOf.ts +1 -1
  61. package/src/utility/runtime/type.ts +3 -1
  62. package/src/utility/runtime/withValue.ts +5 -3
  63. package/src/utility/state/Configurator.ts +2 -2
  64. package/src/utility/state/FluentConfigurator.ts +2 -2
  65. package/src/utility/state/index.ts +2 -3
  66. package/tests/CamelCase-spec.ts +2 -0
  67. package/tests/ExplicitFunction-spec.ts +3 -2
  68. package/tests/IncludeAndRetain-spec.ts +7 -12
  69. package/tests/Includes-spec.ts +2 -0
  70. package/tests/KebabCase-spec.ts +2 -0
  71. package/tests/MutationIdentity-spec.ts +2 -1
  72. package/tests/PascalCase-spec.ts +2 -0
  73. package/tests/Pluralize-spec.ts +2 -0
  74. package/tests/SnakeCase-spec.ts +2 -0
  75. package/tests/Where.spec.ts +9 -10
  76. package/tests/arrayToKeyLookup-spec.ts +5 -5
  77. package/tests/arrayToObject-spec.ts +12 -8
  78. package/tests/constructor.spec.ts +2 -0
  79. package/tests/createFnWithProps.spec.ts +2 -2
  80. package/tests/defineType-spec.ts +17 -11
  81. package/tests/dictionary/DictPartialApplication.test.ts +43 -0
  82. package/tests/dictionary/DictReturnValues.test.ts +32 -0
  83. package/tests/dictionary/Get.spec.ts +5 -3
  84. package/tests/dictionary/PrependValuesWithFunction.ts +34 -0
  85. package/tests/dictionary/RequireProps.test.ts +21 -0
  86. package/tests/dictionaryTransform-spec.ts +3 -3
  87. package/tests/ifTypeOf-spec.ts +31 -20
  88. package/tests/kv/KeyValue-spec.ts +4 -4
  89. package/tests/kv/SameKeys-spec.ts +6 -9
  90. package/tests/kv/dict-to-kv-and-back.spec.ts +5 -4
  91. package/tests/kv/entries-spec.ts +8 -9
  92. package/tests/kv/keys.spec.ts +9 -19
  93. package/tests/kv/kv-spec.ts +4 -4
  94. package/tests/lists/UniqueForProp.spec.ts +3 -1
  95. package/tests/literal-spec.ts +5 -3
  96. package/tests/mapValues-spec.ts +5 -3
  97. package/tests/props-spec.ts +3 -2
  98. package/tests/runtime/condition.spec.ts +2 -1
  99. package/tests/runtime/type.spec.ts +4 -3
  100. package/tests/strArrayToDict-spec.ts +6 -6
  101. package/tests/string-literals/AllCaps.spec.ts +6 -15
  102. package/tests/string-literals/CamelCase.spec.ts +13 -32
  103. package/tests/string-literals/Dasherize.spec.ts +13 -31
  104. package/tests/string-literals/HasUppercase.spec.ts +6 -15
  105. package/tests/string-literals/PascalCase.spec.ts +13 -32
  106. package/tests/string-literals/SnakeCase.spec.ts +13 -32
  107. package/tests/string-literals/whitespace-capture.spec.ts +10 -28
  108. package/tests/tuples/TupleToUnion.spec.ts +3 -1
  109. package/tests/withValue.spec.ts +4 -3
  110. package/tsconfig.json +8 -19
  111. package/vitest.config.ts +13 -0
  112. package/jest.config.ts +0 -20
  113. package/on-hold/Builder/Builder.ts +0 -66
  114. package/on-hold/Builder/BuilderApi.ts +0 -21
  115. package/on-hold/Builder/IdentityToMutationApi.ts +0 -44
  116. package/on-hold/Builder/MutationToFluentApi.ts +0 -60
  117. package/on-hold/Builder/index.ts +0 -35
  118. package/on-hold/tests/Builder-spec.ts +0 -310
  119. package/on-hold/tests/RuleSet-spec.ts +0 -59
  120. package/on-hold/tests/ToFluent-spec.ts +0 -60
  121. package/on-hold/tests/api-spec.ts +0 -73
  122. package/on-hold/tests/data/builder-data.ts +0 -53
  123. package/on-hold/tests/filterDictArray-spec.ts +0 -50
  124. package/on-hold/tests/run-time-spec.ts +0 -44
  125. package/on-hold/type.ts +0 -20
  126. package/on-hold/types/object.ts +0 -19
  127. package/on-hold/types/prop.ts +0 -28
  128. package/on-hold/types/scalar.ts +0 -37
  129. package/src/types/dictionary/NotEmptyObject.ts +0 -9
  130. package/src/types/type-conversion/UnwrapValue.ts +0 -13
  131. package/src/types/type-conversion/WrapValue.ts +0 -15
  132. package/src/utility/dictionary/valuesOfProp.ts +0 -13
  133. package/src/utility/state/KeyStorage.ts +0 -23
@@ -1,73 +0,0 @@
1
- import { Equal, Expect } from "@type-challenges/utils";
2
- import { Api, PrivateKeys, PublicKeys } from "~/types";
3
- import { defineType } from "~/utility";
4
- import { api } from "~/utility/api/api";
5
-
6
- describe("Api<T> type", () => {
7
- it("PublicKeys<T> and PrivateKeys<T> type utils extract the right keys", () => {
8
- const api = {
9
- _state: 123,
10
- _name: "hey ho",
11
- greet: "hi",
12
- age: 12,
13
- };
14
-
15
- const f = string;
16
-
17
- type Test = typeof api;
18
- type Private = PrivateKeys<Test>;
19
- type Public = PublicKeys<Test>;
20
-
21
- type cases = [
22
- Expect<Equal<Private, "_state" | "_name">>,
23
- Expect<Equal<Public, "greet" | "age">>
24
- ];
25
- const cases: cases = [true, true];
26
- expect(cases).toBe(cases);
27
- });
28
-
29
- it.skip("privateApi() creates a definition for a private API", () => {
30
- const food = api({ _model: "food", _description: "let's eat" })({
31
- category: string,
32
- stars: 5 as number,
33
- });
34
- const pub = food();
35
- });
36
-
37
- it("The Api<T> utility hides all private keys while preserving type info", () => {
38
- const simple = {
39
- _state: 123,
40
- _name: "hey ho",
41
- greet: "hi",
42
- age: 12,
43
- };
44
- type Simple = Api<typeof simple>;
45
-
46
- const midling = defineType({ id: 123 })({
47
- _state: 123,
48
- _name: "hey ho",
49
- greet: "hi",
50
- age: 12,
51
- });
52
- type Midling = Api<typeof midling>;
53
-
54
- const advanced = defineType({ id: 123, _exclude: ["greet"] })({ greet: "hi", age: 12 });
55
- type Advanced = Api<typeof advanced>;
56
-
57
- type cases = [
58
- // a simple API with no literals doesn't need generics
59
- // to preserve state
60
- Expect<Equal<Simple, { greet: string; age: number }>>,
61
- // if the public interface has type literals this is
62
- // accommodated without any special effort
63
- Expect<Equal<Midling, { greet: string; age: number; id: 123 }>>,
64
- Expect<Equal<Midling, { greet: string; age: number; id: 123 }>>
65
- // in the situation where the private members have
66
- // literals that need preservation, we need be assured
67
- // the API implementation can preserve this strong typing
68
- // However, to start the public API is quite easily preserved
69
- ];
70
- const cases: cases = [true, true];
71
- expect(cases).toBe(cases);
72
- });
73
- });
@@ -1,53 +0,0 @@
1
- import { Builder, BuilderApi } from "~/Builder";
2
- import { isNonNullObject } from "common-types";
3
- import { TypeGuard } from "~/types";
4
-
5
-
6
- export type BuilderState = { foo: number; bar: number; baz?: string };
7
-
8
- const endpoint = BuilderApi<BuilderState>();
9
- const f0 = endpoint("setFoo", (s) => (n: number) => ({
10
- ...s,
11
- foo: n,
12
- }));
13
-
14
- const f1 = endpoint("incFoo", (s) => () => ({
15
- ...s,
16
- foo: s.foo !== undefined ? s.foo + 1 : 1,
17
- }));
18
- const f2 = endpoint("decFoo", (s) => () => ({
19
- ...s,
20
- foo: s.foo !== undefined ? s.foo - 1 : 0,
21
- }));
22
- const f3 = endpoint("setBar", (s) => (n: number) => ({
23
- ...s,
24
- bar: n,
25
- }));
26
- const f4 = endpoint("incBar", (s) => () => ({
27
- ...s,
28
- bar: s.bar !== undefined ? s.bar + 1 : 1,
29
- }));
30
- const f5 = endpoint("setBaz", (s) => (m: string) => ({
31
- ...s,
32
- baz: m,
33
- }));
34
-
35
- const tg: TypeGuard<BuilderState> = (input: unknown): input is BuilderState => {
36
- return (
37
- isNonNullObject(input) &&
38
- typeof (input as BuilderState).foo === "number" &&
39
- typeof (input as BuilderState).bar === "number"
40
- );
41
- };
42
-
43
- /**
44
- * Creates a builder initialized to the state passed in.
45
- * This builder has foo, bar, and optionally a baz property
46
- */
47
- export function createBuilder<T extends Partial<BuilderState>>(s: T) {
48
- return Builder(tg, { ...f0, ...f1, ...f2, ...f3, ...f4, ...f5 })(s);
49
- }
50
-
51
- export function createStateIdentityApi() {
52
- return { ...f0, ...f1, ...f2, ...f3, ...f4, ...f5 };
53
- }
@@ -1,50 +0,0 @@
1
- import { defineType, dictToArray, filterDictArray } from "~/utility";
2
-
3
- describe("filterDictArray()", () => {
4
-
5
- it("an object converted into a DictArray can be filter down to a subset", () => {
6
- const obj = defineType({ id: 123 })({ color: "red", favorite: true });
7
-
8
- const arr = dictToArray(obj);
9
-
10
- const filtered = filterDictArray(arr, (k, _v) => {
11
- return k !== "color";
12
- });
13
-
14
- // run time tests
15
- expect(arr).toHaveLength(3);
16
- expect(filtered).toHaveLength(2);
17
- expect(filtered.map(i => i[0])).toContain("id");
18
- expect(filtered.map(i => i[0])).toContain("favorite");
19
- expect(filtered.map(i => i[0])).not.toContain("color");
20
-
21
- // TODO: uncomment type test below once working!
22
-
23
- // type tests
24
- type cases = [
25
- // now that "color" has been removed from `T` the type
26
- // should no longer be the same DictArray
27
- // ExpectFalse<Equal<Filtered, DictArray<Obj>>>,
28
- // Expect<Equal<Filtered, DictArray<Omit<Obj, "color">>>>,
29
- ];
30
- const cases: cases = [];
31
- });
32
-
33
- // TODO: return this test once functionality is working correctly
34
-
35
- it.skip("a filtered down DictArray can be converted back to an object", () => {
36
- // const obj = defineType({ id: 123 })({ color: "red", favorite: true });
37
- // type Obj = typeof obj;
38
- // const arr = dictToArray(obj);
39
- // type Arr = typeof arr;
40
-
41
- // const filtered = filterDictArray(arr, (k, _v) => {
42
- // return k !== "color";
43
- // });
44
- // type Filtered = typeof filtered;
45
-
46
- // const reverted = arrayToDict(filtered);
47
- // type Reverted = typeof reverted;
48
- });
49
-
50
- });
@@ -1,44 +0,0 @@
1
- import { Equal, Expect } from "@type-challenges/utils";
2
- import { TypeGuard } from "~/types";
3
- import { number, string } from "~/utility/runtime";
4
-
5
- describe("run-time types", () => {
6
-
7
- it("string()", () => {
8
- const s = string();
9
- type S = typeof s;
10
-
11
- expect(s.type).toBe("string");
12
-
13
- type cases = [
14
- Expect<Equal<S["__kind"], "type">>,
15
- Expect<Equal<S["type"], string>>,
16
- Expect<Equal<S["is"], TypeGuard<string>>>
17
- ];
18
- const cases: cases = [true, true, true];
19
- expect(cases).toBe(cases);
20
- });
21
-
22
- it("number()", () => {
23
- const n = number();
24
- type N = typeof n;
25
-
26
- expect(n.type).toBe("number");
27
-
28
- type cases = [
29
- Expect<Equal<N["__kind"], "type">>,
30
- Expect<Equal<N["type"], number>>,
31
- Expect<Equal<N["is"], TypeGuard<number>>>
32
- ];
33
- const cases: cases = [true, true, true];
34
- expect(cases).toBe(cases);
35
- });
36
-
37
- it("empty object()", () => {
38
- // const o = object();
39
- // type O = typeof o;
40
- });
41
-
42
-
43
-
44
- });
package/on-hold/type.ts DELETED
@@ -1,20 +0,0 @@
1
- import { TypeGuard } from "~/types";
2
- import { RuntimeType } from "../../../types/runtime";
3
-
4
- /**
5
- * Creates a run-time type that is conveyed to Typescript as well
6
- */
7
- export const type =
8
- <T extends any>() =>
9
- <N extends string, O extends object>(
10
- name: N,
11
- validate: TypeGuard<T>,
12
- _options: O
13
- ): RuntimeType<T> => {
14
- return {
15
- __kind: "type",
16
-
17
- type: name as unknown as T,
18
- is: validate,
19
- };
20
- };
@@ -1,19 +0,0 @@
1
- // import type { TypeOptions, TypeGuard } from "~/types";
2
- // import { type } from "./type";
3
-
4
- export type ObjectOption<N extends string | undefined> = {
5
- name?: N;
6
- };
7
-
8
- // export const object = <N extends string | undefined>(props: PropsCallback, opts: TypeOptions<ObjectOption<N>> = {}) => {
9
-
10
- // /**
11
- // * Object based type-guard
12
- // */
13
- // const tg: TypeGuard<null> = (v: unknown): v is null => {
14
- // return v === "null";
15
- // };
16
-
17
-
18
- // return type<null>()("null", tg, opts);
19
- // };
@@ -1,28 +0,0 @@
1
- import { isNonNullObject } from "common-types";
2
- import { TypeGuard } from "~/types/TypeGuard";
3
- import { keys } from "~/utility/keys";
4
- import { RuntimeProp, RuntimeType } from "~/types/runtime";
5
-
6
- export const prop = <
7
- P extends PropertyKey,
8
- T extends RuntimeType<any>,
9
- O extends object
10
- >(prop: Readonly<P>, type: Readonly<T>, _opts: O = {} as O): RuntimeProp<P, T> => {
11
-
12
- /**
13
- * Default type guard for a property; relies on underlying type's typeguard for validation
14
- */
15
- const tg: TypeGuard<Record<P, T["type"]>> = (v: unknown): v is Record<P, T["type"]> => {
16
- return isNonNullObject(v) && keys(v as Record<P, any>).includes(prop) && type.is((v as Record<P, any>)[prop as P]);
17
- };
18
-
19
- return {
20
- __kind: "prop",
21
-
22
- key: prop,
23
- valueType: type.type as unknown as Readonly<T["type"]>,
24
- type: type.type as unknown as Record<P, T["type"]>,
25
-
26
- is: tg
27
- };
28
- };
@@ -1,37 +0,0 @@
1
- import { TypeGuard } from "~/types";
2
- import { TypeOptions } from "../../../types/runtime";
3
-
4
- export const string = (opts: TypeOptions = {}) => {
5
- const tg: TypeGuard<string> = (v: unknown): v is string => {
6
- return typeof v === "string";
7
- };
8
- return type<string>()("string", tg, opts);
9
- };
10
-
11
- export const number = (opts: TypeOptions = {}) => {
12
- const tg: TypeGuard<number> = (v: unknown): v is number => {
13
- return typeof v === "number";
14
- };
15
- return type<number>()("number", tg, opts);
16
- };
17
-
18
- export const boolean = (opts: TypeOptions = {}) => {
19
- const tg: TypeGuard<boolean> = (v: unknown): v is boolean => {
20
- return typeof v === "boolean";
21
- };
22
- return type<boolean>()("string", tg, opts);
23
- };
24
-
25
- export const fn = (opts: TypeOptions = {}) => {
26
- const tg: TypeGuard<Function> = (v: unknown): v is Function => {
27
- return typeof v === "function";
28
- };
29
- return type<Function>()("function", tg, opts);
30
- };
31
-
32
- export const nullValue = (opts: TypeOptions = {}) => {
33
- const tg: TypeGuard<null> = (v: unknown): v is null => {
34
- return v === "null";
35
- };
36
- return type<null>()("null", tg, opts);
37
- };
@@ -1,9 +0,0 @@
1
- import { ExpandRecursively } from "../ExpandRecursively";
2
- import { Keys, Length, UnionToTuple } from "../index";
3
-
4
- /**
5
- * Accepts an object with at least one property defined on it
6
- */
7
- export type NotEmptyObject<T extends {}> = ExpandRecursively<
8
- Length<UnionToTuple<Keys<T>>> extends 0 ? false : T
9
- >;
@@ -1,13 +0,0 @@
1
-
2
- /**
3
- * Given a dictionary of key/values, where the value is a function, this
4
- * type utility will maintain the keys but change the values to whatever
5
- * the `ReturnType` of the function was.
6
- * ```ts
7
- * // { foo: string }
8
- * type Test = UnwrapValue<{ foo: (name: string) => name }>
9
- * ```
10
- */
11
- export type UnwrapValue<T extends Record<string, (...args: any[]) => any>> = {
12
- [K in keyof T]: T[K] extends Function ? ReturnType<T[K]> : never;
13
- }[keyof T];
@@ -1,15 +0,0 @@
1
- type ValueFunction<T extends any> = <R extends any>(v: T) => R;
2
-
3
- /**
4
- * **SameKeys**
5
- *
6
- * Given a dictionary `T` and a type-strong wrapper function `F`, returns a dictionary with the
7
- * same keys but
8
- *
9
- * Note: meant to be used as part of an _extends_ clause in most cases.
10
- */
11
- export type WrapValue<T extends {}, F extends ValueFunction<T>> = {
12
- [P in keyof T]: ReturnType<F>;
13
- };
14
-
15
-
@@ -1,13 +0,0 @@
1
-
2
-
3
- // TODO: implement
4
-
5
- export function valuesOfProp<_T extends object>() {
6
- // return (arr: T[], prop: keyof T) => {
7
-
8
- // for (const i of arr) {
9
- // const v = literal(i[prop]);
10
-
11
- // }
12
- // };
13
- }
@@ -1,23 +0,0 @@
1
- /* eslint-disable no-use-before-define */
2
-
3
- export type KeyStorage<S extends readonly string[]> = (state: S) => {
4
- add: <N extends readonly string[]>(...store: N) => KeyStorageApi<readonly [...S, ...N]>;
5
- remove: <R extends string>(key: R) => KeyStorageApi<Exclude<S, R>>;
6
- done: () => S;
7
- };
8
-
9
- export type KeyStorageApi<S extends readonly string[]> = ReturnType<KeyStorage<S>>;
10
-
11
- //TODO: need to make this work or remove
12
-
13
- export function KeyStorage() {
14
- const api = <S extends readonly string[]>(state: S) => ({
15
- add: <N extends readonly string[]>(...store: N) => {
16
- return api<readonly [...S, ...N]>([...state, ...store]);
17
- },
18
- remove: <R extends string>(key: R) => api<Exclude<S, R>>(state.filter(i => i !== key) as unknown as Exclude<S, R>),
19
- done: () => state
20
- });
21
-
22
- return api([]);
23
- }