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.
- package/.eslintrc +36 -20
- package/.github/workflows/main.yml +34 -0
- package/.github/workflows/other.yml +15 -0
- package/.vscode/settings.json +10 -2
- package/dist/index.d.ts +403 -8538
- package/dist/index.js +35 -60
- package/dist/index.mjs +24 -48
- package/package.json +34 -23
- package/src/shared/valueTypes.ts +1 -1
- package/src/types/ExpandRecursively.ts +10 -1
- package/src/types/FunctionType.ts +1 -1
- package/src/types/SimplifyObject.ts +12 -0
- package/src/types/Transformer.ts +1 -1
- package/src/types/alphabetic/CamelCase.ts +1 -1
- package/src/types/alphabetic/CapFirstAlpha.ts +7 -3
- package/src/types/alphabetic/Dasherize.ts +8 -5
- package/src/types/alphabetic/KebabCase.ts +1 -1
- package/src/types/alphabetic/PascalCase.ts +3 -1
- package/src/types/alphabetic/SnakeCase.ts +8 -3
- package/src/types/dictionary/DictPartialApplication.ts +30 -0
- package/src/types/dictionary/DictPrependWithFn.ts +23 -0
- package/src/types/dictionary/DictReturnValues.ts +27 -0
- package/src/types/dictionary/RequireProps.ts +15 -0
- package/src/types/{type-conversion → dictionary}/SameKeys.ts +0 -0
- package/src/types/dictionary/index.ts +8 -3
- package/src/types/{props.ts → dictionary/props.ts} +1 -1
- package/src/types/functions/FinalReturn.ts +12 -0
- package/{on-hold/types → src/types/functions}/index.ts +4 -3
- package/src/types/index.ts +4 -3
- package/src/types/ruleset-types.ts +3 -3
- package/src/types/runtime.ts +18 -18
- package/src/types/string-literals/LeftWhitespace.ts +5 -2
- package/src/types/string-literals/RightWhitespace.ts +5 -2
- package/src/types/tuples/FirstKey.ts +3 -3
- package/src/types/tuples/FromDictArray.ts +5 -4
- package/src/types/type-conversion/index.ts +2 -5
- package/src/utility/api/api.ts +8 -7
- package/src/utility/createFnWithProps.ts +26 -1
- package/src/utility/dictionary/dictionaryTransform.ts +5 -4
- package/src/utility/dictionary/entries.ts +9 -5
- package/src/utility/dictionary/index.ts +2 -3
- package/src/utility/dictionary/kv/dictToKv.ts +5 -3
- package/src/utility/dictionary/kv/filterDictArray.ts +5 -7
- package/src/utility/dictionary/kv/kv.ts +8 -3
- package/src/utility/dictionary/kv/kvToDict.ts +3 -2
- package/src/utility/dictionary/mapValues.ts +8 -5
- package/src/utility/dictionary/strArrayToDict.ts +1 -2
- package/src/utility/keys.ts +8 -4
- package/src/utility/lists/groupBy.ts +2 -9
- package/src/utility/literals/arrayToObject.ts +29 -29
- package/src/utility/literals/defineType.ts +6 -6
- package/src/utility/literals/identity.ts +5 -2
- package/src/utility/literals/literal.ts +3 -3
- package/src/utility/ruleset.ts +11 -9
- package/src/utility/runtime/condition.ts +1 -1
- package/src/utility/runtime/conditions/isFalse.ts +2 -2
- package/src/utility/runtime/conditions/isFunction.ts +2 -2
- package/src/utility/runtime/conditions/isObject.ts +4 -1
- package/src/utility/runtime/conditions/isTrue.ts +1 -1
- package/src/utility/runtime/ifTypeOf.ts +1 -1
- package/src/utility/runtime/type.ts +3 -1
- package/src/utility/runtime/withValue.ts +5 -3
- package/src/utility/state/Configurator.ts +2 -2
- package/src/utility/state/FluentConfigurator.ts +2 -2
- package/src/utility/state/index.ts +2 -3
- package/tests/CamelCase-spec.ts +2 -0
- package/tests/ExplicitFunction-spec.ts +3 -2
- package/tests/IncludeAndRetain-spec.ts +7 -12
- package/tests/Includes-spec.ts +2 -0
- package/tests/KebabCase-spec.ts +2 -0
- package/tests/MutationIdentity-spec.ts +2 -1
- package/tests/PascalCase-spec.ts +2 -0
- package/tests/Pluralize-spec.ts +2 -0
- package/tests/SnakeCase-spec.ts +2 -0
- package/tests/Where.spec.ts +9 -10
- package/tests/arrayToKeyLookup-spec.ts +5 -5
- package/tests/arrayToObject-spec.ts +12 -8
- package/tests/constructor.spec.ts +2 -0
- package/tests/createFnWithProps.spec.ts +2 -2
- package/tests/defineType-spec.ts +17 -11
- package/tests/dictionary/DictPartialApplication.test.ts +43 -0
- package/tests/dictionary/DictReturnValues.test.ts +32 -0
- package/tests/dictionary/Get.spec.ts +5 -3
- package/tests/dictionary/PrependValuesWithFunction.ts +34 -0
- package/tests/dictionary/RequireProps.test.ts +21 -0
- package/tests/dictionaryTransform-spec.ts +3 -3
- package/tests/ifTypeOf-spec.ts +31 -20
- package/tests/kv/KeyValue-spec.ts +4 -4
- package/tests/kv/SameKeys-spec.ts +6 -9
- package/tests/kv/dict-to-kv-and-back.spec.ts +5 -4
- package/tests/kv/entries-spec.ts +8 -9
- package/tests/kv/keys.spec.ts +9 -19
- package/tests/kv/kv-spec.ts +4 -4
- package/tests/lists/UniqueForProp.spec.ts +3 -1
- package/tests/literal-spec.ts +5 -3
- package/tests/mapValues-spec.ts +5 -3
- package/tests/props-spec.ts +3 -2
- package/tests/runtime/condition.spec.ts +2 -1
- package/tests/runtime/type.spec.ts +4 -3
- package/tests/strArrayToDict-spec.ts +6 -6
- package/tests/string-literals/AllCaps.spec.ts +6 -15
- package/tests/string-literals/CamelCase.spec.ts +13 -32
- package/tests/string-literals/Dasherize.spec.ts +13 -31
- package/tests/string-literals/HasUppercase.spec.ts +6 -15
- package/tests/string-literals/PascalCase.spec.ts +13 -32
- package/tests/string-literals/SnakeCase.spec.ts +13 -32
- package/tests/string-literals/whitespace-capture.spec.ts +10 -28
- package/tests/tuples/TupleToUnion.spec.ts +3 -1
- package/tests/withValue.spec.ts +4 -3
- package/tsconfig.json +8 -19
- package/vitest.config.ts +13 -0
- package/jest.config.ts +0 -20
- package/on-hold/Builder/Builder.ts +0 -66
- package/on-hold/Builder/BuilderApi.ts +0 -21
- package/on-hold/Builder/IdentityToMutationApi.ts +0 -44
- package/on-hold/Builder/MutationToFluentApi.ts +0 -60
- package/on-hold/Builder/index.ts +0 -35
- package/on-hold/tests/Builder-spec.ts +0 -310
- package/on-hold/tests/RuleSet-spec.ts +0 -59
- package/on-hold/tests/ToFluent-spec.ts +0 -60
- package/on-hold/tests/api-spec.ts +0 -73
- package/on-hold/tests/data/builder-data.ts +0 -53
- package/on-hold/tests/filterDictArray-spec.ts +0 -50
- package/on-hold/tests/run-time-spec.ts +0 -44
- package/on-hold/type.ts +0 -20
- package/on-hold/types/object.ts +0 -19
- package/on-hold/types/prop.ts +0 -28
- package/on-hold/types/scalar.ts +0 -37
- package/src/types/dictionary/NotEmptyObject.ts +0 -9
- package/src/types/type-conversion/UnwrapValue.ts +0 -13
- package/src/types/type-conversion/WrapValue.ts +0 -15
- package/src/utility/dictionary/valuesOfProp.ts +0 -13
- package/src/utility/state/KeyStorage.ts +0 -23
|
@@ -1,22 +1,28 @@
|
|
|
1
|
-
import { Narrowable } from "
|
|
2
|
-
|
|
1
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* **UniqueDictionary**
|
|
6
|
-
*
|
|
5
|
+
*
|
|
7
6
|
* A dictionary converted by `arrayToObject()` which expects each key `S` to have a only a
|
|
8
7
|
* single/unique value.
|
|
9
8
|
*/
|
|
10
|
-
export type UniqueDictionary<
|
|
9
|
+
export type UniqueDictionary<
|
|
10
|
+
S extends PropertyKey,
|
|
11
|
+
N extends Narrowable,
|
|
12
|
+
T extends Record<keyof T, N> & Record<S, any>
|
|
13
|
+
> = { [V in T as V[S]]: V };
|
|
11
14
|
|
|
12
15
|
/**
|
|
13
16
|
* **GeneralDictionary**
|
|
14
|
-
*
|
|
17
|
+
*
|
|
15
18
|
* A dictionary converted by `arrayToObject()` which expects each key `S` to have an
|
|
16
19
|
* array of values.
|
|
17
20
|
*/
|
|
18
|
-
export type GeneralDictionary<
|
|
19
|
-
|
|
21
|
+
export type GeneralDictionary<
|
|
22
|
+
S extends PropertyKey,
|
|
23
|
+
N extends Narrowable,
|
|
24
|
+
T extends Record<keyof T, N> & Record<S, any>
|
|
25
|
+
> = { [V in T as V[S]]: V[] };
|
|
20
26
|
|
|
21
27
|
export type ArrayConverter<S extends PropertyKey, U extends boolean> =
|
|
22
28
|
/**
|
|
@@ -28,45 +34,40 @@ export type ArrayConverter<S extends PropertyKey, U extends boolean> =
|
|
|
28
34
|
arr: readonly T[]
|
|
29
35
|
) => true extends U ? UniqueDictionary<S, N, T> : GeneralDictionary<S, N, T>;
|
|
30
36
|
|
|
31
|
-
|
|
32
37
|
/**
|
|
33
38
|
* Converts an array of objects into a dictionary by picking a property name contained
|
|
34
39
|
* by all objects and using that as the key to the dictionary.
|
|
35
|
-
*
|
|
40
|
+
*
|
|
36
41
|
* ```ts
|
|
37
|
-
* const arr = [
|
|
42
|
+
* const arr = [
|
|
38
43
|
* { kind: "color", favorite: "blue", likes: 100 },
|
|
39
44
|
* { kind: "song", favorite: "some song", likes: 25 }
|
|
40
45
|
* ];
|
|
41
46
|
* const dict = arrayToObject("kind")(arr);
|
|
42
47
|
* ```
|
|
43
|
-
*
|
|
48
|
+
*
|
|
44
49
|
* This will produce a dictionary with keys of `color` and `song`.
|
|
45
50
|
*/
|
|
46
|
-
export function arrayToObject<
|
|
47
|
-
S extends PropertyKey,
|
|
48
|
-
U extends boolean
|
|
49
|
-
>(prop: S, unique?: U) {
|
|
51
|
+
export function arrayToObject<S extends PropertyKey, U extends boolean>(prop: S, unique?: U) {
|
|
50
52
|
type X = true extends U ? true : false;
|
|
51
53
|
/**
|
|
52
54
|
* **arrayToObject** - partially applied
|
|
53
|
-
*
|
|
55
|
+
*
|
|
54
56
|
* pass in an array of objects to complete application of arrayToObject()
|
|
55
57
|
*/
|
|
56
|
-
const transform: ArrayConverter<S, X> = <
|
|
58
|
+
const transform: ArrayConverter<S, X> = <
|
|
59
|
+
N extends Narrowable,
|
|
60
|
+
T extends Record<keyof T, N> & Record<S, any>
|
|
61
|
+
>(
|
|
57
62
|
arr: readonly T[]
|
|
58
63
|
): true extends X ? UniqueDictionary<S, N, T> : GeneralDictionary<S, N, T> => {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
(acc, v) =>
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const existing = acc[v[prop] as T[S]] || [];
|
|
67
|
-
return { ...acc, [v[prop]]: [...existing, v] };
|
|
68
|
-
}, {} as GeneralDictionary<S, N, T>
|
|
69
|
-
);
|
|
64
|
+
const result =
|
|
65
|
+
unique !== false
|
|
66
|
+
? arr.reduce((acc, v) => ({ ...acc, [v[prop]]: v }), {} as UniqueDictionary<S, N, T>)
|
|
67
|
+
: arr.reduce((acc, v) => {
|
|
68
|
+
const existing = acc[v[prop] as T[S]] || [];
|
|
69
|
+
return { ...acc, [v[prop]]: [...existing, v] };
|
|
70
|
+
}, {} as GeneralDictionary<S, N, T>);
|
|
70
71
|
|
|
71
72
|
// type cast based on `U`
|
|
72
73
|
return result as true extends X ? UniqueDictionary<S, N, T> : GeneralDictionary<S, N, T>;
|
|
@@ -75,4 +76,3 @@ export function arrayToObject<
|
|
|
75
76
|
return transform;
|
|
76
77
|
// return converter(prop, unique);
|
|
77
78
|
}
|
|
78
|
-
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { ExpandRecursively
|
|
1
|
+
import { ExpandRecursively } from "src/types/ExpandRecursively";
|
|
2
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Build a _type_ from two run-time dictionaries.
|
|
5
|
-
*
|
|
6
|
+
*
|
|
6
7
|
* 1. The _first_ -- which is optional -- is interpreted as a _literal_ type definition
|
|
7
8
|
* 2. The _second_ dictionary is interpreted as a "wide" definition of prop types
|
|
8
9
|
*/
|
|
9
|
-
export function defineType<
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
>(literal: TLiteral = {} as TLiteral) {
|
|
10
|
+
export function defineType<N extends Narrowable, TLiteral extends Record<string, N>>(
|
|
11
|
+
literal: TLiteral = {} as TLiteral
|
|
12
|
+
) {
|
|
13
13
|
/**
|
|
14
14
|
* Add any key/value pairs which you want to have _wide_ types associated;
|
|
15
15
|
* literal types are defined already and stated above.
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Narrowable } from "
|
|
1
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* An identity function for any type, with the goal of preserving literal type information
|
|
4
5
|
* whereever possible.
|
|
@@ -6,4 +7,6 @@ import { Narrowable } from "~/types/Narrowable";
|
|
|
6
7
|
export const identity = <
|
|
7
8
|
N extends Narrowable,
|
|
8
9
|
T extends Record<any, N> | number | string | boolean | symbol | undefined | null
|
|
9
|
-
>(
|
|
10
|
+
>(
|
|
11
|
+
v: T
|
|
12
|
+
) => v;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-use-before-define */
|
|
2
2
|
|
|
3
|
-
import { Narrowable } from "
|
|
3
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Takes an object as input --which has an `id` property and returns it as the same
|
|
@@ -39,8 +39,8 @@ export function idTypeGuard<T extends { id: I }, I extends PropertyKey>(
|
|
|
39
39
|
/**
|
|
40
40
|
* Takes an object as input and infers the narrow literal types of the property
|
|
41
41
|
* values on the object.
|
|
42
|
-
*
|
|
43
|
-
* > Note: this addresses this [a known TS gap](https://github.com/microsoft/TypeScript/issues/30680).
|
|
42
|
+
*
|
|
43
|
+
* > Note: this addresses this [a known TS gap](https://github.com/microsoft/TypeScript/issues/30680).
|
|
44
44
|
* > Hopefully at some point this will be addressed in the language.
|
|
45
45
|
*/
|
|
46
46
|
export function literal<N extends Narrowable, T extends Record<keyof T, N>>(obj: T) {
|
package/src/utility/ruleset.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-use-before-define */
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
3
4
|
|
|
4
5
|
// TODO: get ruleset functionality working!
|
|
5
6
|
|
|
@@ -15,21 +16,23 @@ import type { Narrowable } from "~/types";
|
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* **ruleSet**
|
|
18
|
-
*
|
|
19
|
+
*
|
|
19
20
|
* Defines a ruleset composed of _dynamic_ and _static_ boolean operators.
|
|
20
|
-
*
|
|
21
|
+
*
|
|
21
22
|
* - the first function call defines _dynamic_ props (_optional_)
|
|
22
23
|
* - the second function call defines static values
|
|
23
|
-
*
|
|
24
|
+
*
|
|
24
25
|
* ```ts
|
|
25
|
-
* const rs = ruleSet(
|
|
26
|
+
* const rs = ruleSet(
|
|
26
27
|
* r => r.state()( { maybe: r => r.extends({ foo: 1 }) })
|
|
27
|
-
* )(
|
|
28
|
-
* { color: true, age: false }
|
|
28
|
+
* )(
|
|
29
|
+
* { color: true, age: false }
|
|
29
30
|
* );
|
|
30
31
|
* ```
|
|
31
32
|
*/
|
|
32
|
-
export function ruleSet<N extends Narrowable, TState extends Record<keyof TState, N>>(
|
|
33
|
+
export function ruleSet<N extends Narrowable, TState extends Record<keyof TState, N>>(
|
|
34
|
+
defn?: TState
|
|
35
|
+
) {
|
|
33
36
|
// return <
|
|
34
37
|
// TStatic extends { [K in keyof TStatic]: Readonly<true | false> },
|
|
35
38
|
// >(rules: TStatic) => {
|
|
@@ -37,4 +40,3 @@ export function ruleSet<N extends Narrowable, TState extends Record<keyof TState
|
|
|
37
40
|
// };
|
|
38
41
|
return defn;
|
|
39
42
|
}
|
|
40
|
-
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Narrowable } from "
|
|
2
|
-
import { IsBoolean } from "
|
|
1
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
2
|
+
import { IsBoolean } from "./isBoolean";
|
|
3
3
|
|
|
4
4
|
export type IsFalse<T extends Narrowable> = IsBoolean<T> extends true
|
|
5
5
|
? // is a boolean
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FunctionType } from "
|
|
1
|
+
import { FunctionType } from "src/types/FunctionType";
|
|
2
2
|
|
|
3
3
|
export type IsFunction<T> = T extends FunctionType ? true : false;
|
|
4
4
|
|
|
@@ -11,7 +11,7 @@ export type IsFunction<T> = T extends FunctionType ? true : false;
|
|
|
11
11
|
* ```
|
|
12
12
|
*
|
|
13
13
|
* Note: the runtime `typeof [variable]` will correctly say "function" when a function is
|
|
14
|
-
*
|
|
14
|
+
* encountered but if that function also has object types defined then the type will be a big
|
|
15
15
|
* and ugly union type. This function will give you a proper boolean value in both cases.
|
|
16
16
|
*/
|
|
17
17
|
export function isFunction<T extends unknown>(input: T): IsFunction<T> {
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import { FunctionType
|
|
1
|
+
import { FunctionType } from "src/types/FunctionType";
|
|
2
|
+
import { Mutable } from "src/types/Mutable";
|
|
3
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
4
|
+
import { Not } from "src/types/Not";
|
|
2
5
|
|
|
3
6
|
export type IsObject<T> = Mutable<T> extends Record<string, any>
|
|
4
7
|
? // an object of some type
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { WithValue } from "src/types/dictionary";
|
|
4
|
+
import { ExpandRecursively } from "src/types/ExpandRecursively";
|
|
5
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
6
|
+
import { entries } from "../dictionary/entries";
|
|
7
|
+
import { type, TypeDefinition } from "./type";
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
10
|
* **withValue**
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { ExpandRecursively } from "src/types/ExpandRecursively";
|
|
2
|
+
|
|
2
3
|
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
|
|
3
4
|
function omit<T extends {}, K extends Array<keyof T>>(obj: T, ...removals: K) {
|
|
4
5
|
const untyped = removals as Array<unknown>;
|
|
@@ -8,7 +9,6 @@ function omit<T extends {}, K extends Array<keyof T>>(obj: T, ...removals: K) {
|
|
|
8
9
|
>;
|
|
9
10
|
}
|
|
10
11
|
|
|
11
|
-
import { ExpandRecursively } from "~/types";
|
|
12
12
|
export interface IConfigurator<C = {}> {
|
|
13
13
|
set<V, K extends string, KV = { [U in K]: V }>(
|
|
14
14
|
key: K,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ExpandRecursively } from "src/types/ExpandRecursively";
|
|
2
2
|
|
|
3
3
|
export interface IFluentConfigurator<C> {
|
|
4
4
|
/**
|
|
@@ -28,7 +28,7 @@ export function FluentConfigurator<I>(initial: I = {} as I) {
|
|
|
28
28
|
const api = <C>(current: C): IFluentConfigurator<C> => {
|
|
29
29
|
return {
|
|
30
30
|
set<V, K extends string, KV = { [U in K]: V }>(key: K, value: V) {
|
|
31
|
-
const keyValue =
|
|
31
|
+
const keyValue = { [key]: value as V } as unknown as KV;
|
|
32
32
|
const updated = { ...keyValue, ...current };
|
|
33
33
|
|
|
34
34
|
return api<C & KV>(updated);
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
// #autoindex
|
|
2
2
|
// #region autoindexed files
|
|
3
|
-
// index last changed at:
|
|
4
|
-
// hash-code:
|
|
3
|
+
// index last changed at: 7th Jul, 2022, 01:15 PM ( GMT-7 )
|
|
4
|
+
// hash-code: 14769606
|
|
5
5
|
|
|
6
6
|
// file exports
|
|
7
7
|
export * from "./Configurator";
|
|
8
8
|
export * from "./FluentConfigurator";
|
|
9
|
-
export * from "./KeyStorage";
|
|
10
9
|
|
|
11
10
|
// #endregion
|
|
12
11
|
|
package/tests/CamelCase-spec.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { ExplicitFunction } from "../src/utility";
|
|
3
4
|
import type { Expect, Equal } from "@type-challenges/utils";
|
|
4
5
|
|
|
5
6
|
describe("ExplicitFunction()", () => {
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { Include, Retain } from "../src/types";
|
|
2
4
|
import { Equal, Expect } from "@type-challenges/utils";
|
|
3
5
|
|
|
4
6
|
// INCLUDE
|
|
@@ -8,10 +10,7 @@ describe("Include<T, U> utility", () => {
|
|
|
8
10
|
type Foo = Include<typeof str, string>;
|
|
9
11
|
type NotFoo = Include<typeof str, number>;
|
|
10
12
|
|
|
11
|
-
type cases = [
|
|
12
|
-
Expect<Equal<Foo, "foo">>,
|
|
13
|
-
Expect<Equal<NotFoo, never>>
|
|
14
|
-
];
|
|
13
|
+
type cases = [Expect<Equal<Foo, "foo">>, Expect<Equal<NotFoo, never>>];
|
|
15
14
|
const c: cases = [true, true];
|
|
16
15
|
expect(c).toBe(c);
|
|
17
16
|
});
|
|
@@ -33,12 +32,11 @@ describe("Include<T, U> utility", () => {
|
|
|
33
32
|
Expect<Equal<RelatedFoo, "foo">>,
|
|
34
33
|
// this behavior is adjusted if you include
|
|
35
34
|
// the optional `L` generic as a true value
|
|
36
|
-
Expect<Equal<NotLiteralFoo, never
|
|
35
|
+
Expect<Equal<NotLiteralFoo, never>>
|
|
37
36
|
];
|
|
38
37
|
const c: cases = [true, true, true, true];
|
|
39
38
|
expect(c).toBe(c);
|
|
40
39
|
});
|
|
41
|
-
|
|
42
40
|
});
|
|
43
41
|
|
|
44
42
|
// RETAIN
|
|
@@ -52,7 +50,7 @@ describe("Retain<T, U> utility", () => {
|
|
|
52
50
|
type cases = [
|
|
53
51
|
Expect<Equal<FooBar, { foo: number; bar: number }>>,
|
|
54
52
|
Expect<Equal<Baz, { baz: string }>>,
|
|
55
|
-
Expect<Equal<FooBaz, { foo: number; baz: string }
|
|
53
|
+
Expect<Equal<FooBaz, { foo: number; baz: string }>>
|
|
56
54
|
];
|
|
57
55
|
const c: cases = [true, true, true];
|
|
58
56
|
expect(c).toBe(c);
|
|
@@ -62,11 +60,8 @@ describe("Retain<T, U> utility", () => {
|
|
|
62
60
|
type Obj = { foo: 1; bar?: number; baz: "hi" };
|
|
63
61
|
type FooBar = Retain<Obj, "foo" | "bar">;
|
|
64
62
|
|
|
65
|
-
type cases = [
|
|
66
|
-
Expect<Equal<FooBar, { foo: 1; bar?: number }>>,
|
|
67
|
-
];
|
|
63
|
+
type cases = [Expect<Equal<FooBar, { foo: 1; bar?: number }>>];
|
|
68
64
|
const c: cases = [true];
|
|
69
65
|
expect(c).toBe(c);
|
|
70
66
|
});
|
|
71
|
-
|
|
72
67
|
});
|
package/tests/Includes-spec.ts
CHANGED
package/tests/KebabCase-spec.ts
CHANGED
package/tests/PascalCase-spec.ts
CHANGED
package/tests/Pluralize-spec.ts
CHANGED
package/tests/SnakeCase-spec.ts
CHANGED
package/tests/Where.spec.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
|
|
1
3
|
import { Expect, Equal } from "@type-challenges/utils";
|
|
2
4
|
import { Where, WhereNot } from "../src/types/Where";
|
|
3
5
|
|
|
4
6
|
describe("Where<T,U> and WhereNot<T,U> type utility", () => {
|
|
5
|
-
|
|
6
7
|
it("readonly string array works correctly", () => {
|
|
7
8
|
const arr = ["foo", "bar", "baz"] as const;
|
|
8
9
|
type A = typeof arr;
|
|
@@ -11,29 +12,27 @@ describe("Where<T,U> and WhereNot<T,U> type utility", () => {
|
|
|
11
12
|
|
|
12
13
|
type cases = [
|
|
13
14
|
//
|
|
14
|
-
Expect<Equal<B, "bar"| "baz">>,
|
|
15
|
+
Expect<Equal<B, "bar" | "baz">>,
|
|
15
16
|
Expect<Equal<F, "foo">>
|
|
16
17
|
];
|
|
17
|
-
|
|
18
|
-
const c: cases = [
|
|
18
|
+
|
|
19
|
+
const c: cases = [true, true];
|
|
19
20
|
expect(c).toBe(c);
|
|
20
21
|
});
|
|
21
22
|
|
|
22
23
|
it("object with literal keys works", () => {
|
|
23
|
-
const arr = {foo: true, bar: true, baz: true} as const;
|
|
24
|
+
const arr = { foo: true, bar: true, baz: true } as const;
|
|
24
25
|
type A = typeof arr;
|
|
25
26
|
type B = Where<A, `ba${string}`>;
|
|
26
27
|
type F = WhereNot<A, `ba${string}`>;
|
|
27
28
|
|
|
28
29
|
type cases = [
|
|
29
30
|
//
|
|
30
|
-
Expect<Equal<B, "bar"| "baz">>,
|
|
31
|
+
Expect<Equal<B, "bar" | "baz">>,
|
|
31
32
|
Expect<Equal<F, "foo">>
|
|
32
33
|
];
|
|
33
|
-
|
|
34
|
-
const c: cases = [
|
|
34
|
+
|
|
35
|
+
const c: cases = [true, true];
|
|
35
36
|
expect(c).toBe(c);
|
|
36
37
|
});
|
|
37
|
-
|
|
38
|
-
|
|
39
38
|
});
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
|
|
1
3
|
import { Equal, Expect } from "@type-challenges/utils";
|
|
2
|
-
import { arrayToKeyLookup } from "
|
|
4
|
+
import { arrayToKeyLookup } from "../src/utility";
|
|
3
5
|
|
|
4
6
|
describe("arrayToKeyLookup()", () => {
|
|
5
7
|
it("simple string array is converted", () => {
|
|
@@ -8,10 +10,8 @@ describe("arrayToKeyLookup()", () => {
|
|
|
8
10
|
expect(lookup.bar).toBe(true);
|
|
9
11
|
expect(lookup.baz).toBe(true);
|
|
10
12
|
|
|
11
|
-
type cases = [
|
|
12
|
-
Expect<Equal<typeof lookup, Record<"foo" | "bar" | "baz", true>>>
|
|
13
|
-
];
|
|
13
|
+
type cases = [Expect<Equal<typeof lookup, Record<"foo" | "bar" | "baz", true>>>];
|
|
14
14
|
const cases: cases = [true];
|
|
15
15
|
expect(cases).toBe(cases);
|
|
16
16
|
});
|
|
17
|
-
});
|
|
17
|
+
});
|
|
@@ -1,15 +1,21 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
1
2
|
|
|
2
3
|
import { Equal, Expect, ExpectExtends } from "@type-challenges/utils";
|
|
3
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
ArrayConverter,
|
|
6
|
+
arrayToObject,
|
|
7
|
+
GeneralDictionary,
|
|
8
|
+
UniqueDictionary,
|
|
9
|
+
defineType,
|
|
10
|
+
keys,
|
|
11
|
+
} from "../src/utility";
|
|
4
12
|
|
|
5
13
|
const foo = { name: "foo", age: 123, color: "red" } as const;
|
|
6
14
|
const foo2 = { name: "foo", age: 44, color: "orange" } as const;
|
|
7
15
|
const bar = { name: "bar", age: 456, color: "blue" } as const;
|
|
8
16
|
const baz = { name: "baz", age: 42, color: "green" } as const;
|
|
9
17
|
|
|
10
|
-
|
|
11
18
|
describe("arrayToObject => ", () => {
|
|
12
|
-
|
|
13
19
|
it("partial application of arrayToObject()", () => {
|
|
14
20
|
const t1 = arrayToObject("name", false);
|
|
15
21
|
type T1 = typeof t1;
|
|
@@ -28,7 +34,7 @@ describe("arrayToObject => ", () => {
|
|
|
28
34
|
// The response types for each type is as expected
|
|
29
35
|
Expect<ExpectExtends<GeneralDictionary<"name", any, any>, ReturnType<T1>>>,
|
|
30
36
|
Expect<ExpectExtends<UniqueDictionary<"name", any, any>, ReturnType<T2>>>,
|
|
31
|
-
Expect<ExpectExtends<UniqueDictionary<"name", any, any>, ReturnType<T3
|
|
37
|
+
Expect<ExpectExtends<UniqueDictionary<"name", any, any>, ReturnType<T3>>>
|
|
32
38
|
];
|
|
33
39
|
|
|
34
40
|
const cases: cases = [true, true, true, true, true, true];
|
|
@@ -71,7 +77,7 @@ describe("arrayToObject => ", () => {
|
|
|
71
77
|
// however when we passed in two "types" for "foo" we get a union
|
|
72
78
|
Expect<Equal<typeof dict2["foo"], SomeFoo>>,
|
|
73
79
|
// once we state that foo is not unique, the type expanded to an array
|
|
74
|
-
Expect<Equal<typeof dict3["foo"], SomeFoo[]
|
|
80
|
+
Expect<Equal<typeof dict3["foo"], SomeFoo[]>>
|
|
75
81
|
];
|
|
76
82
|
const cases: cases = [true, true, true, true, true];
|
|
77
83
|
expect(cases).toBe(cases);
|
|
@@ -100,12 +106,10 @@ describe("arrayToObject => ", () => {
|
|
|
100
106
|
Expect<Equal<Slug["bob"]["slug"], "bob">>,
|
|
101
107
|
// "age" and "color" are expressed as their widened types
|
|
102
108
|
Expect<Equal<Id["1"]["age"], number>>,
|
|
103
|
-
Expect<Equal<Id["1"]["color"], string
|
|
109
|
+
Expect<Equal<Id["1"]["color"], string>>
|
|
104
110
|
];
|
|
105
111
|
|
|
106
112
|
const cases: cases = [true, true, true, true, true, true];
|
|
107
113
|
expect(cases).toBe(cases);
|
|
108
114
|
});
|
|
109
|
-
|
|
110
|
-
|
|
111
115
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
import { createFnWithProps } from "
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { createFnWithProps } from "../src/index";
|
|
3
3
|
|
|
4
4
|
describe("createFnWithProps()", () => {
|
|
5
5
|
it("simple fn and prop are combined, type is retained", () => {
|
package/tests/defineType-spec.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { ExpectExtends } from "~/types";
|
|
3
|
-
import { defineType } from "~/utility";
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
4
2
|
|
|
3
|
+
import type { Expect, Equal } from "@type-challenges/utils";
|
|
4
|
+
import { ExpectExtends } from "../src/types";
|
|
5
|
+
import { defineType } from "../src/utility";
|
|
5
6
|
|
|
6
7
|
describe("inferredType() utility", () => {
|
|
7
8
|
it("only specify a wide type", () => {
|
|
@@ -22,7 +23,16 @@ describe("inferredType() utility", () => {
|
|
|
22
23
|
Expect<Equal<typeof t, { foo: string; bar: number }>>,
|
|
23
24
|
// for some odd reason, when we let the default value for literal
|
|
24
25
|
// we get something odd
|
|
25
|
-
Expect<
|
|
26
|
+
Expect<
|
|
27
|
+
Equal<
|
|
28
|
+
typeof t2,
|
|
29
|
+
{
|
|
30
|
+
foo: string;
|
|
31
|
+
bar: number;
|
|
32
|
+
[x: string]: string | number | boolean | symbol | void | object | {} | null | undefined;
|
|
33
|
+
}
|
|
34
|
+
>
|
|
35
|
+
>,
|
|
26
36
|
// fortunately it does extend the intended type
|
|
27
37
|
Expect<ExpectExtends<typeof t2, { foo: string; bar: number }>>
|
|
28
38
|
];
|
|
@@ -32,9 +42,7 @@ describe("inferredType() utility", () => {
|
|
|
32
42
|
|
|
33
43
|
it("only specify a literal type", () => {
|
|
34
44
|
const t = defineType({ foo: 1, bar: "hi" })();
|
|
35
|
-
type cases = [
|
|
36
|
-
Expect<Equal<typeof t, { foo: 1; bar: "hi" }>>
|
|
37
|
-
];
|
|
45
|
+
type cases = [Expect<Equal<typeof t, { foo: 1; bar: "hi" }>>];
|
|
38
46
|
const cases: cases = [true];
|
|
39
47
|
expect(cases).toBe(cases);
|
|
40
48
|
});
|
|
@@ -42,10 +50,8 @@ describe("inferredType() utility", () => {
|
|
|
42
50
|
it("specify both wide and literal merged type", () => {
|
|
43
51
|
const t = defineType({ bar: 1 })({ foo: "" });
|
|
44
52
|
|
|
45
|
-
type cases = [
|
|
46
|
-
Expect<Equal<typeof t, { foo: string; bar: 1 }>>
|
|
47
|
-
];
|
|
53
|
+
type cases = [Expect<Equal<typeof t, { foo: string; bar: 1 }>>];
|
|
48
54
|
const cases: cases = [true];
|
|
49
55
|
expect(cases).toBe(cases);
|
|
50
56
|
});
|
|
51
|
-
});
|
|
57
|
+
});
|