inferred-types 0.16.0 → 0.18.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.
- package/.tsbuildinfo +1 -0
- package/devops/build.js +1 -3
- package/dist/{types/utility/state/KeyStorage.d.ts → index.d.ts} +9767 -8195
- package/dist/index.js +659 -0
- package/dist/index.mjs +577 -0
- package/on-hold/tests/api-spec.ts +9 -15
- package/package.json +27 -39
- package/pnpm-lock.yaml +2807 -3858
- package/src/errors/InferenceError.ts +1 -1
- package/src/shared/randomString.ts +1 -1
- package/src/types/Constructor.ts +5 -0
- package/src/types/Not.ts +3 -1
- package/src/types/Transformer.ts +1 -2
- package/src/types/Where.ts +36 -0
- package/src/types/alphabetic/PascalCase.ts +5 -4
- package/src/types/alphabetic/index.ts +9 -5
- package/src/types/{fluent.ts → fluent/fluent.ts} +8 -9
- package/src/types/fluent/index.ts +42 -0
- package/src/types/index.ts +13 -7
- package/src/types/ruleset-types.ts +1 -1
- package/src/types/{SameKeys.ts → type-conversion/SameKeys.ts} +0 -0
- package/src/types/type-conversion/WrapValue.ts +15 -0
- package/src/types/type-conversion/index.ts +11 -4
- package/src/utility/dictionary/dictionaryTransform.ts +6 -7
- package/tests/Where.spec.ts +39 -0
- package/tests/constructor.spec.ts +40 -0
- package/tests/data/index.ts +8 -3
- package/tests/kv/dict-to-kv-and-back.spec.ts +2 -6
- package/tsconfig.json +4 -3
- package/dist/cjs/index.js +0 -21
- package/dist/es/index.js +0 -17
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/types/Mutation/MutationFunction.d.ts +0 -16
- package/dist/types/Mutation/MutationIdentity.d.ts +0 -30
- package/dist/types/Mutation/index.d.ts +0 -2
- package/dist/types/errors/InferenceError.d.ts +0 -1
- package/dist/types/errors/index.d.ts +0 -1
- package/dist/types/index.d.ts +0 -4
- package/dist/types/shared/index.d.ts +0 -3
- package/dist/types/shared/randomString.d.ts +0 -1
- package/dist/types/shared/uuid.d.ts +0 -1
- package/dist/types/shared/valueTypes.d.ts +0 -27
- package/dist/types/types/Api.d.ts +0 -10
- package/dist/types/types/EnumValues.d.ts +0 -14
- package/dist/types/types/ExpandRecursively.d.ts +0 -3
- package/dist/types/types/First.d.ts +0 -4
- package/dist/types/types/FunctionType.d.ts +0 -9
- package/dist/types/types/If.d.ts +0 -10
- package/dist/types/types/Include.d.ts +0 -16
- package/dist/types/types/Includes.d.ts +0 -5
- package/dist/types/types/KeyedRecord.d.ts +0 -22
- package/dist/types/types/Keys.d.ts +0 -17
- package/dist/types/types/Length.d.ts +0 -8
- package/dist/types/types/Mutable.d.ts +0 -6
- package/dist/types/types/Narrowable.d.ts +0 -5
- package/dist/types/types/Not.d.ts +0 -11
- package/dist/types/types/Numeric.d.ts +0 -14
- package/dist/types/types/Opaque.d.ts +0 -12
- package/dist/types/types/Retain.d.ts +0 -16
- package/dist/types/types/SameKeys.d.ts +0 -11
- package/dist/types/types/Transformer.d.ts +0 -11
- package/dist/types/types/TypeGuard.d.ts +0 -7
- package/dist/types/types/alphabetic/AllCaps.d.ts +0 -12
- package/dist/types/types/alphabetic/CamelCase.d.ts +0 -2
- package/dist/types/types/alphabetic/CapFirstAlpha.d.ts +0 -13
- package/dist/types/types/alphabetic/CapitalizeWords.d.ts +0 -4
- package/dist/types/types/alphabetic/DashToSnake.d.ts +0 -1
- package/dist/types/types/alphabetic/DashUppercase.d.ts +0 -14
- package/dist/types/types/alphabetic/Dasherize.d.ts +0 -15
- package/dist/types/types/alphabetic/HasUppercase.d.ts +0 -13
- package/dist/types/types/alphabetic/Intrinsic.d.ts +0 -4
- package/dist/types/types/alphabetic/IsCapitalized.d.ts +0 -15
- package/dist/types/types/alphabetic/KebabCase.d.ts +0 -12
- package/dist/types/types/alphabetic/LowerAllCaps.d.ts +0 -4
- package/dist/types/types/alphabetic/PascalCase.d.ts +0 -16
- package/dist/types/types/alphabetic/Pluralize.d.ts +0 -36
- package/dist/types/types/alphabetic/SnakeCase.d.ts +0 -14
- package/dist/types/types/alphabetic/alpha-characters.d.ts +0 -36
- package/dist/types/types/alphabetic/index.d.ts +0 -16
- package/dist/types/types/append-types.d.ts +0 -9
- package/dist/types/types/dictionary/Get.d.ts +0 -7
- package/dist/types/types/dictionary/index.d.ts +0 -1
- package/dist/types/types/fluent.d.ts +0 -51
- package/dist/types/types/index.d.ts +0 -34
- package/dist/types/types/kv/DictFromKv.d.ts +0 -6
- package/dist/types/types/kv/KeyValue.d.ts +0 -11
- package/dist/types/types/kv/KvFrom.d.ts +0 -13
- package/dist/types/types/kv/KvTuple.d.ts +0 -14
- package/dist/types/types/kv/index.d.ts +0 -4
- package/dist/types/types/lists/UniqueForProp.d.ts +0 -14
- package/dist/types/types/lists/index.d.ts +0 -1
- package/dist/types/types/maybe.d.ts +0 -8
- package/dist/types/types/props.d.ts +0 -100
- package/dist/types/types/ruleset-types.d.ts +0 -59
- package/dist/types/types/runtime.d.ts +0 -33
- package/dist/types/types/string-literals/Break.d.ts +0 -12
- package/dist/types/types/string-literals/Concat.d.ts +0 -8
- package/dist/types/types/string-literals/LeftWhitespace.d.ts +0 -9
- package/dist/types/types/string-literals/Replace.d.ts +0 -11
- package/dist/types/types/string-literals/RightWhitespace.d.ts +0 -9
- package/dist/types/types/string-literals/StringLength.d.ts +0 -10
- package/dist/types/types/string-literals/Trim.d.ts +0 -11
- package/dist/types/types/string-literals/TrimLeft.d.ts +0 -11
- package/dist/types/types/string-literals/TrimRight.d.ts +0 -11
- package/dist/types/types/string-literals/form-fields.d.ts +0 -12
- package/dist/types/types/string-literals/index.d.ts +0 -10
- package/dist/types/types/tuples/DictArray.d.ts +0 -19
- package/dist/types/types/tuples/FirstKey.d.ts +0 -8
- package/dist/types/types/tuples/FirstKeyValue.d.ts +0 -8
- package/dist/types/types/tuples/FirstOfEach.d.ts +0 -11
- package/dist/types/types/tuples/FromDictArray.d.ts +0 -10
- package/dist/types/types/tuples/SecondOfEach.d.ts +0 -10
- package/dist/types/types/tuples/index.d.ts +0 -6
- package/dist/types/types/type-conversion/TupleToUnion.d.ts +0 -10
- package/dist/types/types/type-conversion/UnionToIntersection.d.ts +0 -5
- package/dist/types/types/type-conversion/UnionToTuple.d.ts +0 -9
- package/dist/types/types/type-conversion/index.d.ts +0 -3
- package/dist/types/types/type-testing.d.ts +0 -13
- package/dist/types/utility/api/api.d.ts +0 -2
- package/dist/types/utility/api/index.d.ts +0 -1
- package/dist/types/utility/createFnWithProps.d.ts +0 -1
- package/dist/types/utility/dictionary/arrayToKeyLookup.d.ts +0 -9
- package/dist/types/utility/dictionary/dictFilter.d.ts +0 -8
- package/dist/types/utility/dictionary/dictionaryTransform.d.ts +0 -9
- package/dist/types/utility/dictionary/entries.d.ts +0 -17
- package/dist/types/utility/dictionary/index.d.ts +0 -8
- package/dist/types/utility/dictionary/kv/dictToKv.d.ts +0 -16
- package/dist/types/utility/dictionary/kv/filterDictArray.d.ts +0 -9
- package/dist/types/utility/dictionary/kv/index.d.ts +0 -4
- package/dist/types/utility/dictionary/kv/kv.d.ts +0 -10
- package/dist/types/utility/dictionary/kv/kvToDict.d.ts +0 -15
- package/dist/types/utility/dictionary/mapValues.d.ts +0 -13
- package/dist/types/utility/dictionary/strArrayToDict.d.ts +0 -6
- package/dist/types/utility/dictionary/valuesOfProp.d.ts +0 -1
- package/dist/types/utility/index.d.ts +0 -11
- package/dist/types/utility/keys.d.ts +0 -5
- package/dist/types/utility/lists/groupBy.d.ts +0 -12
- package/dist/types/utility/lists/index.d.ts +0 -1
- package/dist/types/utility/literals/ExplicitFunction.d.ts +0 -6
- package/dist/types/utility/literals/arrayToObject.d.ts +0 -41
- package/dist/types/utility/literals/defineType.d.ts +0 -8
- package/dist/types/utility/literals/identity.d.ts +0 -6
- package/dist/types/utility/literals/index.d.ts +0 -5
- package/dist/types/utility/literals/literal.d.ts +0 -41
- package/dist/types/utility/map-reduce/filter.d.ts +0 -26
- package/dist/types/utility/map-reduce/index.d.ts +0 -1
- package/dist/types/utility/modelling/IoModel.d.ts +0 -26
- package/dist/types/utility/modelling/Model.d.ts +0 -22
- package/dist/types/utility/modelling/index.d.ts +0 -1
- package/dist/types/utility/ruleset.d.ts +0 -18
- package/dist/types/utility/runtime/condition.d.ts +0 -3
- package/dist/types/utility/runtime/conditions/index.d.ts +0 -12
- package/dist/types/utility/runtime/conditions/isArray.d.ts +0 -2
- package/dist/types/utility/runtime/conditions/isBoolean.d.ts +0 -5
- package/dist/types/utility/runtime/conditions/isFalse.d.ts +0 -4
- package/dist/types/utility/runtime/conditions/isFunction.d.ts +0 -15
- package/dist/types/utility/runtime/conditions/isLiteral.d.ts +0 -2
- package/dist/types/utility/runtime/conditions/isNull.d.ts +0 -1
- package/dist/types/utility/runtime/conditions/isNumber.d.ts +0 -1
- package/dist/types/utility/runtime/conditions/isObject.d.ts +0 -10
- package/dist/types/utility/runtime/conditions/isString.d.ts +0 -2
- package/dist/types/utility/runtime/conditions/isSymbol.d.ts +0 -1
- package/dist/types/utility/runtime/conditions/isTrue.d.ts +0 -20
- package/dist/types/utility/runtime/conditions/isUndefined.d.ts +0 -1
- package/dist/types/utility/runtime/ifTypeOf.d.ts +0 -23
- package/dist/types/utility/runtime/index.d.ts +0 -5
- package/dist/types/utility/runtime/type.d.ts +0 -27
- package/dist/types/utility/runtime/withValue.d.ts +0 -19
- package/dist/types/utility/state/Configurator.d.ts +0 -36
- package/dist/types/utility/state/FluentConfigurator.d.ts +0 -27
- package/dist/types/utility/state/index.d.ts +0 -3
- package/src/types/alphabetic/Intrinsic.ts +0 -6
- package/src/utility/modelling/IoModel.ts +0 -38
- package/tests/FluentApi-spec.ts +0 -90
- package/tests/FluentConfigurator-spec.hold +0 -88
- package/tests/IoModel-spec.hold +0 -209
- package/tests/lists/groupBy.spec.ts +0 -0
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import * as t from "io-ts";
|
|
2
|
-
import type { PascalCase } from "../../types/index";
|
|
3
|
-
/**
|
|
4
|
-
* Defines an `io-ts` based **model**.
|
|
5
|
-
*
|
|
6
|
-
* - `<T>` represents type of the model
|
|
7
|
-
* - `<S>` is the model's literal type
|
|
8
|
-
*
|
|
9
|
-
* Note: unlink the default `io-ts` model/codec, the
|
|
10
|
-
* `name` variable is an explicit type alias and not a string
|
|
11
|
-
* type. This helps to preserve it's type inference even though
|
|
12
|
-
* the model itself does have a runtime definition of it's type.
|
|
13
|
-
*/
|
|
14
|
-
export declare type IoModel<M, N extends string> = Omit<t.Type<M>, "name"> & {
|
|
15
|
-
name: N;
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* **Model**
|
|
19
|
-
*
|
|
20
|
-
* A function which returns a `io-ts` based model/codec but with a _literal_ name
|
|
21
|
-
* property, and a `kind` which uniquely distinguishes itself as a "model".
|
|
22
|
-
*
|
|
23
|
-
* **Note:** the name of a model is expected to be in _PascalCase_ and will be converted
|
|
24
|
-
* to it if not passed in correctly.
|
|
25
|
-
*/
|
|
26
|
-
export declare function IoModel<RP extends t.Props, OP extends t.Props | {}, N extends string>(name: N, req: RP, optional?: OP): IoModel<{ [K in keyof RP]: t.TypeOf<RP[K]>; } & { [K_1 in keyof OP]?: t.TypeOf<OP[K_1]> | undefined; }, PascalCase<N>>;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export declare function Model<N extends string, _R extends {} = {}, _O extends {} = {}>(name: N): {
|
|
2
|
-
required<P extends string>(_prop: P): {
|
|
3
|
-
required<P_1 extends string>(_prop: P_1): any;
|
|
4
|
-
optional<P_2 extends string>(_prop: P_2): any;
|
|
5
|
-
done(): {
|
|
6
|
-
__model__: N;
|
|
7
|
-
__kind__: "model";
|
|
8
|
-
};
|
|
9
|
-
};
|
|
10
|
-
optional<P_3 extends string>(_prop: P_3): {
|
|
11
|
-
required<P_1 extends string>(_prop: P_1): any;
|
|
12
|
-
optional<P_2 extends string>(_prop: P_2): any;
|
|
13
|
-
done(): {
|
|
14
|
-
__model__: N;
|
|
15
|
-
__kind__: "model";
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
done(): {
|
|
19
|
-
__model__: N;
|
|
20
|
-
__kind__: "model";
|
|
21
|
-
};
|
|
22
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./Model";
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { Narrowable } from "../types/index";
|
|
2
|
-
/**
|
|
3
|
-
* **ruleSet**
|
|
4
|
-
*
|
|
5
|
-
* Defines a ruleset composed of _dynamic_ and _static_ boolean operators.
|
|
6
|
-
*
|
|
7
|
-
* - the first function call defines _dynamic_ props (_optional_)
|
|
8
|
-
* - the second function call defines static values
|
|
9
|
-
*
|
|
10
|
-
* ```ts
|
|
11
|
-
* const rs = ruleSet(
|
|
12
|
-
* r => r.state()( { maybe: r => r.extends({ foo: 1 }) })
|
|
13
|
-
* )(
|
|
14
|
-
* { color: true, age: false }
|
|
15
|
-
* );
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
export declare function ruleSet<N extends Narrowable, TState extends Record<keyof TState, N>>(defn?: TState): TState | undefined;
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { Narrowable } from "../../types/index";
|
|
2
|
-
export declare type Condition<TInput extends Narrowable, TResult extends boolean> = (input: TInput) => TResult;
|
|
3
|
-
export declare const condition: <TInput extends Narrowable, C extends Condition<Narrowable, boolean>>(c: C, input: TInput) => boolean;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export * from "./isArray";
|
|
2
|
-
export * from "./isBoolean";
|
|
3
|
-
export * from "./isFalse";
|
|
4
|
-
export * from "./isFunction";
|
|
5
|
-
export * from "./isLiteral";
|
|
6
|
-
export * from "./isNull";
|
|
7
|
-
export * from "./isNumber";
|
|
8
|
-
export * from "./isObject";
|
|
9
|
-
export * from "./isString";
|
|
10
|
-
export * from "./isSymbol";
|
|
11
|
-
export * from "./isTrue";
|
|
12
|
-
export * from "./isUndefined";
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { Narrowable } from "../../../types/index";
|
|
2
|
-
import { IsBoolean } from "../../index";
|
|
3
|
-
export declare type IsFalse<T extends Narrowable> = IsBoolean<T> extends true ? T extends false ? true : true extends T ? false : unknown : false;
|
|
4
|
-
export declare function isFalse<T>(i: T): IsFalse<T>;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { FunctionType } from "../../../types/index";
|
|
2
|
-
export declare type IsFunction<T> = T extends FunctionType ? true : false;
|
|
3
|
-
/**
|
|
4
|
-
* Checks whether a passed in value is a function and ensures run-time and types
|
|
5
|
-
* are consistent.
|
|
6
|
-
* ```ts
|
|
7
|
-
* // true
|
|
8
|
-
* const yup = isFunction(() => "hello world");
|
|
9
|
-
* ```
|
|
10
|
-
*
|
|
11
|
-
* Note: the runtime `typeof [variable]` will correctly say "function" when a function is
|
|
12
|
-
* encounted but if that function also has object types defined then the type will be a big
|
|
13
|
-
* and ugly union type. This function will give you a proper boolean value in both cases.
|
|
14
|
-
*/
|
|
15
|
-
export declare function isFunction<T extends unknown>(input: T): IsFunction<T>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function isNull<T>(i: T): T extends null ? true : false;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function isNumber<T>(i: T): T extends number ? true : false;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { FunctionType, Mutable, Narrowable, Not } from "../../../types/index";
|
|
2
|
-
export declare type IsObject<T> = Mutable<T> extends Record<string, any> ? T extends FunctionType ? false : Mutable<T> extends any[] ? false : true : false;
|
|
3
|
-
export declare type ObjectType = Not<Record<string, Narrowable>, FunctionType>;
|
|
4
|
-
/**
|
|
5
|
-
* Detects whether the passed in `v` is of type "object" where an object
|
|
6
|
-
* is defined to be a string keyed dictionary style object. This means that
|
|
7
|
-
* arrays are excluded, as well as functions which also have properties hanging
|
|
8
|
-
* off of them.
|
|
9
|
-
*/
|
|
10
|
-
export declare function isObject<T extends unknown>(i: T): IsObject<T>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function isSymbol<T>(i: T): T extends symbol ? true : false;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Narrowable } from "../../../types/index";
|
|
2
|
-
import type { IsBoolean } from "./isBoolean";
|
|
3
|
-
/**
|
|
4
|
-
* Type utility which returns `true` or `false` based on
|
|
5
|
-
* whether the type holds the narrow "true" type.
|
|
6
|
-
* ```ts
|
|
7
|
-
* // true
|
|
8
|
-
* type T = IsTrue<true>;
|
|
9
|
-
* // unknown
|
|
10
|
-
* type U = IsTrue<boolean>;
|
|
11
|
-
* // false
|
|
12
|
-
* type F = IsTrue<false>;
|
|
13
|
-
* type F2 = IsTrue<"false">;
|
|
14
|
-
* ```
|
|
15
|
-
*/
|
|
16
|
-
export declare type IsTrue<T> = IsBoolean<T> extends true ? T extends true ? true : T extends false ? false : unknown : false;
|
|
17
|
-
/**
|
|
18
|
-
* Run-time and type checking of whether a variable is `true`.
|
|
19
|
-
*/
|
|
20
|
-
export declare function isTrue<T extends Narrowable>(i: T): IsTrue<T>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function isUndefined<T>(i: T): undefined extends T ? true : false;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { Narrowable } from "../../types/Narrowable";
|
|
2
|
-
/**
|
|
3
|
-
* A conditional clause used in the application of the `ifTypeOf` utility
|
|
4
|
-
*/
|
|
5
|
-
export declare type ExtendsClause<N extends Narrowable, TValue extends Record<keyof TValue, N> | number | string | boolean | symbol> = <TBase extends any>(base: TBase) => TValue extends TBase ? true : false;
|
|
6
|
-
/**
|
|
7
|
-
* A conditional clause used in the application of the `ifTypeOf` utility
|
|
8
|
-
*/
|
|
9
|
-
export declare type ExtendsNarrowlyClause<N extends Narrowable, TValue extends Record<keyof TValue, N> | number | string | boolean | symbol> = <NB extends Narrowable, TBase extends Record<keyof TBase, NB> | number | string | boolean | symbol>(base: TBase) => TValue extends TBase ? true : false;
|
|
10
|
-
/**
|
|
11
|
-
* **TypeCondition**
|
|
12
|
-
*
|
|
13
|
-
* A partially applied type from the `ifTypeOf` utility where the base type has been
|
|
14
|
-
* defined and we now need to express the type which is intended to extend it.
|
|
15
|
-
*
|
|
16
|
-
* - `extends` - compares with _wide_ types
|
|
17
|
-
* - `narrowlyExtends` - compares with _narrow_ / _literal_ types
|
|
18
|
-
*/
|
|
19
|
-
export declare type TypeCondition<N extends Narrowable, TValue extends Record<keyof TValue, N> | number | string | boolean | symbol> = {
|
|
20
|
-
extends: ExtendsClause<N, TValue>;
|
|
21
|
-
narrowlyExtends: ExtendsNarrowlyClause<N, TValue>;
|
|
22
|
-
};
|
|
23
|
-
export declare const ifTypeOf: <N extends Narrowable, TValue extends string | number | boolean | symbol | Record<keyof TValue, N>>(val: TValue) => TypeCondition<N, TValue>;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { Narrowable, TypeGuard } from "../../types/index";
|
|
2
|
-
import { isString, isBoolean, isNumber, isFunction, isSymbol, isNull, isTrue, isFalse, isUndefined, isObject, isArray } from "./conditions/index";
|
|
3
|
-
export declare type Type<T extends any, V extends Function> = {
|
|
4
|
-
name: string;
|
|
5
|
-
type: T;
|
|
6
|
-
typeGuard: TypeGuard<T>;
|
|
7
|
-
is: V;
|
|
8
|
-
};
|
|
9
|
-
export declare const typeApi: () => {
|
|
10
|
-
readonly string: Type<string, typeof isString>;
|
|
11
|
-
readonly boolean: Type<boolean, typeof isBoolean>;
|
|
12
|
-
readonly number: Type<number, typeof isNumber>;
|
|
13
|
-
readonly function: Type<Function | (Function & {
|
|
14
|
-
[key: string]: any;
|
|
15
|
-
}), typeof isFunction>;
|
|
16
|
-
readonly null: Type<null, typeof isNull>;
|
|
17
|
-
readonly symbol: Type<symbol, typeof isSymbol>;
|
|
18
|
-
readonly undefined: Type<undefined, typeof isUndefined>;
|
|
19
|
-
readonly true: Type<true, typeof isTrue>;
|
|
20
|
-
readonly false: Type<false, typeof isFalse>;
|
|
21
|
-
readonly object: Type<Record<string, Narrowable>, typeof isObject>;
|
|
22
|
-
readonly array: Type<any[], typeof isArray>;
|
|
23
|
-
};
|
|
24
|
-
export declare type TypeApi = ReturnType<typeof typeApi>;
|
|
25
|
-
export declare function isType<T extends any, V extends Function>(t: unknown): t is Type<T, V>;
|
|
26
|
-
export declare type TypeDefinition<T extends any, V extends Function> = (defn: TypeApi) => Type<T, V>;
|
|
27
|
-
export declare function type<T extends any, V extends Function>(fn: TypeDefinition<T, V>): Type<T, V>;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { ExpandRecursively, Narrowable } from "../../types/index";
|
|
2
|
-
import { TypeDefinition } from "./index";
|
|
3
|
-
/**
|
|
4
|
-
* **withValue**
|
|
5
|
-
*
|
|
6
|
-
* Reduces a dictionary object -- in both _type_ and _run-time_ structure -- to only those
|
|
7
|
-
* key/value pairs which have a specified value. For instance:
|
|
8
|
-
*
|
|
9
|
-
* ```ts
|
|
10
|
-
* const obj = { foo: 1, bar: 2, message: "hi there" };
|
|
11
|
-
* // { message: "hi there" }
|
|
12
|
-
* const onlyStrings = withValue(t => t.string)(obj);
|
|
13
|
-
* // { foo: 1 }
|
|
14
|
-
* const justOne = withValue(t => t.literal(1))(obj);
|
|
15
|
-
* ```
|
|
16
|
-
*
|
|
17
|
-
* Note: _often useful to provide run-time type profiles with the_ `inferredType` _utility_
|
|
18
|
-
*/
|
|
19
|
-
export declare function withValue<T extends any, V extends Function>(td: TypeDefinition<T, V>): <N extends Narrowable, R extends Record<string, N>>(obj: R) => ExpandRecursively<Pick<R, import("../../types/index").KeysWithValue<T, R>>>;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { ExpandRecursively } from "../../types/index";
|
|
2
|
-
export interface IConfigurator<C = {}> {
|
|
3
|
-
set<V, K extends string, KV = {
|
|
4
|
-
[U in K]: V;
|
|
5
|
-
}>(key: K, value: V): asserts this is IConfigurator<ExpandRecursively<C & KV>>;
|
|
6
|
-
remove<K extends string & keyof C>(key: K): asserts this is IConfigurator<ExpandRecursively<Omit<C, K>>>;
|
|
7
|
-
done(): C;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* **Configurator**
|
|
11
|
-
*
|
|
12
|
-
* This function provides a configurator which uses TypeScript assertions
|
|
13
|
-
* to narrow the scope of variables. Using type assertions though means we
|
|
14
|
-
* can't also return values and therefore a _fluent_ API style is not possible.
|
|
15
|
-
*
|
|
16
|
-
* Note: this approach does require, that you explicitly use the `IConfigurator`
|
|
17
|
-
* interface exported by this library. Without it you'd getting annoying TS errors
|
|
18
|
-
* on every call to `set()` but adding it is simple enough:
|
|
19
|
-
*
|
|
20
|
-
* ```ts
|
|
21
|
-
* import { Configurator, IConfigurator } from "inferred-types";
|
|
22
|
-
* const config: IConfigurator = Configurator();
|
|
23
|
-
* ```
|
|
24
|
-
*
|
|
25
|
-
* If you want to specify some guideline params which you expect to be set, you
|
|
26
|
-
* can add them like so:
|
|
27
|
-
*
|
|
28
|
-
* ```ts
|
|
29
|
-
* export type IExpected = { foo: number, bar: string };
|
|
30
|
-
* const config: IConfigurator<IExpected> = Configurator<IExpected>();
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* This configuration will ensure that `foo` and `bar` will be seen as optional
|
|
34
|
-
* parameters. If you set them they will stop being optional.
|
|
35
|
-
*/
|
|
36
|
-
export declare function Configurator(): IConfigurator<{}>;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { ExpandRecursively } from "../../types/index";
|
|
2
|
-
export interface IFluentConfigurator<C> {
|
|
3
|
-
/**
|
|
4
|
-
* **set**
|
|
5
|
-
*
|
|
6
|
-
* Sets a new key/value pair to the configurator.
|
|
7
|
-
*
|
|
8
|
-
* @param key the key to put this new configuration item
|
|
9
|
-
* @param value the value of the configuration item
|
|
10
|
-
*/
|
|
11
|
-
set<V, K extends string, KV = {
|
|
12
|
-
[U in K]: V;
|
|
13
|
-
}>(key: K, value: V): IFluentConfigurator<C & KV>;
|
|
14
|
-
done(): ExpandRecursively<C>;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* This function will return a basic _configurator_ API surface which allows
|
|
18
|
-
* you to add name/value pairs to a growing dictionary of strongly typed data.
|
|
19
|
-
*
|
|
20
|
-
* When configuration is complete, you call the `done()` endpoint and the typed
|
|
21
|
-
* configuration will be returned with the API removed.
|
|
22
|
-
*
|
|
23
|
-
* > **Note:** the approach taken here requires that you configure using a **fluent style**
|
|
24
|
-
* exclusively. If you don't the non-fluent properties set will not show up
|
|
25
|
-
* in the typing or the run-time object.
|
|
26
|
-
*/
|
|
27
|
-
export declare function FluentConfigurator<I>(initial?: I): IFluentConfigurator<{}>;
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import * as t from "io-ts";
|
|
2
|
-
import { pascalize } from "native-dash";
|
|
3
|
-
import type { PascalCase } from "~/types";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Defines an `io-ts` based **model**.
|
|
7
|
-
*
|
|
8
|
-
* - `<T>` represents type of the model
|
|
9
|
-
* - `<S>` is the model's literal type
|
|
10
|
-
*
|
|
11
|
-
* Note: unlink the default `io-ts` model/codec, the
|
|
12
|
-
* `name` variable is an explicit type alias and not a string
|
|
13
|
-
* type. This helps to preserve it's type inference even though
|
|
14
|
-
* the model itself does have a runtime definition of it's type.
|
|
15
|
-
*/
|
|
16
|
-
export type IoModel<M, N extends string> = Omit<t.Type<M>, "name"> & { name: N };
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* **Model**
|
|
20
|
-
*
|
|
21
|
-
* A function which returns a `io-ts` based model/codec but with a _literal_ name
|
|
22
|
-
* property, and a `kind` which uniquely distinguishes itself as a "model".
|
|
23
|
-
*
|
|
24
|
-
* **Note:** the name of a model is expected to be in _PascalCase_ and will be converted
|
|
25
|
-
* to it if not passed in correctly.
|
|
26
|
-
*/
|
|
27
|
-
export function IoModel<RP extends t.Props, OP extends t.Props | {}, N extends string>(
|
|
28
|
-
name: N,
|
|
29
|
-
req: RP,
|
|
30
|
-
optional: OP = {} as OP
|
|
31
|
-
) {
|
|
32
|
-
const model = t.intersection([t.type(req), t.partial(optional)], name);
|
|
33
|
-
type T = t.TypeOf<typeof model>;
|
|
34
|
-
return {
|
|
35
|
-
...(model as t.Type<T>),
|
|
36
|
-
name: pascalize(name),
|
|
37
|
-
} as IoModel<T, PascalCase<N>>;
|
|
38
|
-
}
|
package/tests/FluentApi-spec.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import type { Expect, Equal, ExpectExtends, ExpectFalse } from "@type-challenges/utils";
|
|
2
|
-
import { FluentApi, Keys } from "~/types";
|
|
3
|
-
|
|
4
|
-
const escape = {
|
|
5
|
-
hi: () => "hello",
|
|
6
|
-
};
|
|
7
|
-
const fluent = {
|
|
8
|
-
foo: () => ({ ...fluent, ...escape }),
|
|
9
|
-
bar: () => ({ ...fluent, ...escape }),
|
|
10
|
-
};
|
|
11
|
-
const fluentSolo = {
|
|
12
|
-
foo: () => fluentSolo,
|
|
13
|
-
bar: () => fluentSolo,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
describe("FluentApi type", () => {
|
|
17
|
-
it("Fluent API without escape API can be used as FluentAPI", () => {
|
|
18
|
-
const api: FluentApi<typeof fluentSolo> = fluentSolo;
|
|
19
|
-
type Api = typeof api;
|
|
20
|
-
|
|
21
|
-
type cases = [
|
|
22
|
-
// Api extends the type FluentApi
|
|
23
|
-
Expect<ExpectExtends<FluentApi<any, any, any>, Api>>,
|
|
24
|
-
// the fluent API keys are found on API
|
|
25
|
-
Expect<ExpectExtends<Keys<Api>, "foo" | "bar">>,
|
|
26
|
-
// the fluent API returns itself
|
|
27
|
-
Expect<Equal<Api, ReturnType<Api["bar"]>>>,
|
|
28
|
-
Expect<Equal<Api, ReturnType<Api["foo"]>>>
|
|
29
|
-
];
|
|
30
|
-
const cases: cases = [true, true, true, true];
|
|
31
|
-
expect(cases).toBe(cases);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it("Simple fluent API and escape API are combined", () => {
|
|
35
|
-
const api: FluentApi<typeof fluent, typeof escape> = { ...fluent, ...escape };
|
|
36
|
-
type Api = typeof api;
|
|
37
|
-
|
|
38
|
-
type cases = [
|
|
39
|
-
// Api extends the type FluentApi
|
|
40
|
-
Expect<ExpectExtends<FluentApi<any, any, any>, Api>>,
|
|
41
|
-
// the fluent and escape api keys are found on API
|
|
42
|
-
Expect<ExpectExtends<Keys<Api>, "foo" | "bar" | "hi">>,
|
|
43
|
-
// the fluent API returns itself
|
|
44
|
-
Expect<Equal<Api, ReturnType<Api["bar"]>>>,
|
|
45
|
-
Expect<Equal<Api, ReturnType<Api["foo"]>>>,
|
|
46
|
-
// whereas the escape API just returns a string
|
|
47
|
-
Expect<Equal<string, ReturnType<Api["hi"]>>>
|
|
48
|
-
];
|
|
49
|
-
const cases: cases = [true, true, true, true, true];
|
|
50
|
-
expect(cases).toBe(cases);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it("Exclusion masks certain API endpoints", () => {
|
|
54
|
-
const api1: FluentApi<typeof fluent, typeof escape, "hi"> = { ...fluent, ...escape };
|
|
55
|
-
type Api1 = typeof api1;
|
|
56
|
-
const api2: FluentApi<typeof fluent, typeof escape, "foo" | "bar"> = { ...fluent, ...escape };
|
|
57
|
-
type Api2 = typeof api2;
|
|
58
|
-
|
|
59
|
-
type cases = [
|
|
60
|
-
// excluding "hi" hides the escape api but leaves fluent API untouched
|
|
61
|
-
Expect<ExpectExtends<"foo" | "bar", Keys<Api1>>>,
|
|
62
|
-
ExpectFalse<ExpectExtends<"hi", Keys<Api1>>>,
|
|
63
|
-
// similarly hiding "foo" and "bar" leaves the escape API untouched
|
|
64
|
-
Expect<ExpectExtends<"hi", Keys<Api2>>>,
|
|
65
|
-
ExpectFalse<ExpectExtends<"foo" | "bar", Keys<Api2>>>
|
|
66
|
-
];
|
|
67
|
-
|
|
68
|
-
const cases: cases = [true, false, true, false];
|
|
69
|
-
expect(cases).toBe(cases);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it("applying different FluentApi exclusions to the same runtime API modifies the API appropriately", () => {
|
|
73
|
-
const api1: FluentApi<typeof fluent, typeof escape, "hi"> = { ...fluent, ...escape };
|
|
74
|
-
type Api1 = typeof api1;
|
|
75
|
-
const api2: FluentApi<typeof fluent, typeof escape, "foo" | "bar"> = api1 as any;
|
|
76
|
-
type Api2 = typeof api2;
|
|
77
|
-
|
|
78
|
-
type cases = [
|
|
79
|
-
// excluding "hi" hides the escape api but leaves fluent API untouched
|
|
80
|
-
Expect<ExpectExtends<"foo" | "bar", Keys<Api1>>>,
|
|
81
|
-
ExpectFalse<ExpectExtends<"hi", Keys<Api1>>>,
|
|
82
|
-
// similarly hiding "foo" and "bar" leaves the escape API untouched
|
|
83
|
-
Expect<ExpectExtends<"hi", Keys<Api2>>>,
|
|
84
|
-
ExpectFalse<ExpectExtends<"foo" | "bar", Keys<Api2>>>
|
|
85
|
-
];
|
|
86
|
-
|
|
87
|
-
const cases: cases = [true, false, true, false];
|
|
88
|
-
expect(cases).toBe(cases);
|
|
89
|
-
});
|
|
90
|
-
});
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { FluentConfigurator, keys } from "../src";
|
|
2
|
-
import { mySong, Playlist, Song } from "./data/index";
|
|
3
|
-
import { SimpleTable } from "./data/SimpleTable";
|
|
4
|
-
|
|
5
|
-
describe("Configurator => ", () => {
|
|
6
|
-
it("Call to configurator returns API", () => {
|
|
7
|
-
const c = FluentConfigurator();
|
|
8
|
-
expect(typeof c.done).toBe("function");
|
|
9
|
-
expect(typeof c.done()).toBe("object");
|
|
10
|
-
expect(Object.keys(c.done())).toHaveLength(0);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it("Configurator with empty initial state, can have props added using set() and fluent API style", () => {
|
|
14
|
-
const c = FluentConfigurator().set("a", 5).set("b", "foobar").done();
|
|
15
|
-
expect(c).toHaveProperty("a");
|
|
16
|
-
expect(typeof c.a).toBe("number");
|
|
17
|
-
expect(c).toHaveProperty("b");
|
|
18
|
-
expect(typeof c.b).toBe("string");
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it("Configurator with starting state, can have props added using fluent API style", () => {
|
|
22
|
-
const start = { foo: "bar", bar: "baz" };
|
|
23
|
-
const c = FluentConfigurator(start).set("b", "foobar").set("a", 5).done();
|
|
24
|
-
console.log(keys(c));
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
// added state
|
|
28
|
-
expect(c).toHaveProperty("a", 5);
|
|
29
|
-
expect(typeof c.a).toBe("number");
|
|
30
|
-
expect(c).toHaveProperty("b", "foobar");
|
|
31
|
-
expect(typeof c.b).toBe("string");
|
|
32
|
-
|
|
33
|
-
// starting state
|
|
34
|
-
expect(c).toHaveProperty("foo");
|
|
35
|
-
// TODO: need to ensure initial state is available to type system
|
|
36
|
-
// expect(typeof c.foo).toBe("string");
|
|
37
|
-
// expect(c.foo).toBe(start.foo);
|
|
38
|
-
expect(c).toHaveProperty("bar");
|
|
39
|
-
// expect(typeof c.bar).toBe("string");
|
|
40
|
-
// expect(c.bar).toBe(start.bar);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it("Configurator stores Table objects and keeps the typing", () => {
|
|
44
|
-
const c = FluentConfigurator()
|
|
45
|
-
.set("songs", SimpleTable(Song))
|
|
46
|
-
.set("playlists", SimpleTable(Playlist))
|
|
47
|
-
.done();
|
|
48
|
-
// structure
|
|
49
|
-
expect(c).toHaveProperty("songs");
|
|
50
|
-
expect(c.songs).toHaveProperty("is");
|
|
51
|
-
expect(c.songs).toHaveProperty("select");
|
|
52
|
-
expect(c).toHaveProperty("playlists");
|
|
53
|
-
expect(c.playlists).toHaveProperty("is");
|
|
54
|
-
expect(c.playlists).toHaveProperty("select");
|
|
55
|
-
// type guards
|
|
56
|
-
// TODO: get back to working types
|
|
57
|
-
expect((c.songs as any).is(mySong)).toBe(true);
|
|
58
|
-
expect((c.songs as any).is({ foo: false })).toBe(false);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it("set() function is able to set a simple value and have type retained", () => {
|
|
62
|
-
const c = FluentConfigurator()
|
|
63
|
-
.set("songs", SimpleTable(Song))
|
|
64
|
-
.set("playlists", SimpleTable(Playlist))
|
|
65
|
-
.set("foo", 15)
|
|
66
|
-
.set("bar", 1);
|
|
67
|
-
const t = c.done();
|
|
68
|
-
// TODO: get back to working types
|
|
69
|
-
expect((t.songs as any).is(mySong)).toBe(true);
|
|
70
|
-
expect((t.songs as any).is({ foo: false })).toBe(false);
|
|
71
|
-
expect(typeof t.songs).toBe("object");
|
|
72
|
-
expect(typeof t.playlists).toBe("object");
|
|
73
|
-
expect(typeof t.foo).toBe("number");
|
|
74
|
-
expect(typeof t.bar).toBe("number");
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("a partially typed configurator can be configured with set()", () => {
|
|
78
|
-
const c = FluentConfigurator<{ songs: object; playlists: object }>()
|
|
79
|
-
.set("songs", SimpleTable(Song))
|
|
80
|
-
.set("playlists", SimpleTable(Playlist))
|
|
81
|
-
.set("foobar", true)
|
|
82
|
-
.done();
|
|
83
|
-
|
|
84
|
-
expect(c.songs.name).toBe("Song");
|
|
85
|
-
expect(c.playlists.name).toBe("Playlist");
|
|
86
|
-
expect(c.foobar).toBe(true);
|
|
87
|
-
});
|
|
88
|
-
});
|