free-types-core 0.8.5 → 0.9.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/dist/Const.d.ts +1 -0
- package/dist/Type.d.ts +9 -1
- package/dist/apply.d.ts +28 -4
- package/dist/helpers.d.ts +19 -7
- package/package.json +1 -1
package/dist/Const.d.ts
CHANGED
package/dist/Type.d.ts
CHANGED
|
@@ -1,20 +1,28 @@
|
|
|
1
1
|
import { Tuple } from './utils';
|
|
2
2
|
export { Type };
|
|
3
3
|
/** Extend `Type<Input?, Output?>` with an interface to produce a free type, or use it as a type constraint.*/
|
|
4
|
-
declare type Type<Input extends number | unknown[] = unknown[], Output = unknown> = CreateType<{
|
|
4
|
+
declare type Type<Input extends number | unknown[] = unknown[], Output = unknown, Labels extends string[] = []> = CreateType<{
|
|
5
5
|
type: Output;
|
|
6
6
|
constraints: Constraints<Input, Slots<Input>>;
|
|
7
7
|
arguments: unknown[];
|
|
8
|
+
labels: ReverseMap<Labels>;
|
|
8
9
|
}>;
|
|
9
10
|
interface CreateType<T extends {
|
|
10
11
|
type: unknown;
|
|
11
12
|
constraints: unknown[];
|
|
13
|
+
labels: {
|
|
14
|
+
[k: string]: number;
|
|
15
|
+
};
|
|
12
16
|
}> {
|
|
13
17
|
[k: number]: unknown;
|
|
14
18
|
type: T['type'];
|
|
15
19
|
constraints: T['constraints'];
|
|
16
20
|
arguments: unknown[];
|
|
21
|
+
labels: T['labels'];
|
|
17
22
|
}
|
|
23
|
+
declare type ReverseMap<T extends string[]> = {
|
|
24
|
+
[K in keyof T as K extends `${number}` ? T[K] : never]: K extends `${infer I extends number}` ? I : never;
|
|
25
|
+
};
|
|
18
26
|
declare type Slots<T> = T extends Precomputable ? PrecomputedSlots[T & Precomputable] : Tuple<T extends unknown[] ? T['length'] : T extends number ? T : number>;
|
|
19
27
|
declare type Constraints<Input, Slots> = number extends Input ? unknown[] : unknown[] extends Input ? unknown[] : Input extends unknown[] ? Input : Slots;
|
|
20
28
|
declare type PrecomputedSlots = {
|
package/dist/apply.d.ts
CHANGED
|
@@ -1,14 +1,38 @@
|
|
|
1
1
|
import { Type } from './Type';
|
|
2
2
|
import { ArrayKeys, Next } from './utils';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
declare type NamedType = {
|
|
4
|
+
type: unknown;
|
|
5
|
+
constraints: {
|
|
6
|
+
[k: number]: unknown;
|
|
7
|
+
length: number;
|
|
8
|
+
};
|
|
9
|
+
labels: Labels;
|
|
10
|
+
};
|
|
11
|
+
declare type Labels = {
|
|
12
|
+
[k: string]: number;
|
|
13
|
+
};
|
|
14
|
+
export { apply, applyNamed, $apply, Generic };
|
|
15
|
+
/** Apply a free type with its complete arguments list and evaluate the type */
|
|
16
|
+
declare type apply<$T extends Type, Args extends $T['constraints'] = []> = applyPositional<$T, Args>;
|
|
17
|
+
/** Apply a free type with its complete arguments struct and evaluate the type */
|
|
18
|
+
declare type applyNamed<$T extends NamedType, Args extends CheckNamed<$Model>, $Model extends NamedType = $T> = applyPositional<$T, ToPositional<$T, Args>>;
|
|
19
|
+
declare type RecoverLabels<T extends Labels> = {
|
|
20
|
+
[K in keyof T as T[K]]: K;
|
|
21
|
+
};
|
|
22
|
+
declare type ToPositional<$T extends NamedType, Args extends {
|
|
23
|
+
[k: string]: unknown;
|
|
24
|
+
}, Labels extends {
|
|
25
|
+
[k: number]: string;
|
|
26
|
+
} = RecoverLabels<$T['labels']>, I extends number = 0, R extends unknown[] = []> = I extends $T['constraints']['length'] ? R : ToPositional<$T, Args, Labels, Next<I>, [...R, Args[Labels[I]]]>;
|
|
27
|
+
declare type CheckNamed<$T extends NamedType> = {
|
|
28
|
+
[K in keyof $T['labels']]: $T['constraints'][$T['labels'][K]];
|
|
29
|
+
};
|
|
6
30
|
interface $apply<Args extends unknown[] = []> extends Type<[Type]> {
|
|
7
31
|
type: apply<this[0] extends Type ? this[0] : Type, Args>;
|
|
8
32
|
}
|
|
9
33
|
/** Apply a free type `$T` with its type constraints. */
|
|
10
34
|
declare type Generic<$T extends Type> = _apply<$T, $T['constraints']>;
|
|
11
|
-
declare type
|
|
35
|
+
declare type applyPositional<$T extends {
|
|
12
36
|
type: unknown;
|
|
13
37
|
constraints: {
|
|
14
38
|
length: number;
|
package/dist/helpers.d.ts
CHANGED
|
@@ -1,40 +1,52 @@
|
|
|
1
1
|
import { Next, IsUnknown, Prev } from './utils';
|
|
2
2
|
export { At, Checked, Lossy, Optional, A, B, C, D, E };
|
|
3
|
-
declare type Type<N extends number> = {
|
|
4
|
-
constraints: {
|
|
3
|
+
declare type Type<N extends number = number> = {
|
|
4
|
+
constraints: N extends number ? {
|
|
5
5
|
[K in Prev<N>]: unknown;
|
|
6
|
-
};
|
|
6
|
+
} : unknown[];
|
|
7
7
|
arguments: unknown[];
|
|
8
|
+
labels: {
|
|
9
|
+
[k: string]: number;
|
|
10
|
+
};
|
|
8
11
|
};
|
|
9
12
|
declare type Type_<N extends number> = {
|
|
10
13
|
constraints: {
|
|
11
14
|
[K in Prev<N>]?: unknown;
|
|
12
15
|
};
|
|
13
16
|
arguments: unknown[];
|
|
17
|
+
labels: {
|
|
18
|
+
[k: string]: number;
|
|
19
|
+
};
|
|
14
20
|
};
|
|
15
21
|
declare type Default = {
|
|
16
22
|
constraints: [0, 1, 2, 3, 4];
|
|
17
23
|
arguments: unknown[];
|
|
24
|
+
labels: {
|
|
25
|
+
[k: string]: number;
|
|
26
|
+
};
|
|
18
27
|
};
|
|
19
28
|
/** - safely index `this`
|
|
20
29
|
* - optionally take a fallback
|
|
21
30
|
*/
|
|
22
|
-
declare type At<N extends
|
|
31
|
+
declare type At<N extends Check, This extends N extends number ? Type<Next<N>> : Type, Fallback = unknown, Check extends string | number = N extends string ? keyof This['labels'] & string : number> = N extends number ? AtImpl<This, Fallback, N> : AtImpl<This, Fallback, This['labels'][N]>;
|
|
32
|
+
declare type AtImpl<This extends Type, Fallback, I extends number> = IsUnknown<This['arguments'][I]> extends true ? Fallback : This['arguments'][I];
|
|
23
33
|
/** - safely index `this`
|
|
24
34
|
* - defuse the corresponding type constraint with an inline conditional
|
|
25
35
|
* - optionally take a fallback
|
|
26
36
|
*/
|
|
27
|
-
declare type Checked<N extends
|
|
37
|
+
declare type Checked<N extends Check, This extends N extends number ? Type<Next<N>> : Type, Fallback = N extends number ? This['constraints'][N] : N extends string ? This['constraints'][This['labels'][N]] : never, Check extends string | number = N extends string ? keyof This['labels'] & string : number> = N extends number ? CheckedImpl<N, This, Fallback> : CheckedImpl<This['labels'][N], This, Fallback>;
|
|
38
|
+
declare type CheckedImpl<N extends number, This extends Type, Fallback> = This['arguments'][N] extends This['constraints'][N] ? This['arguments'][N] : Fallback;
|
|
28
39
|
/** - safely index `this`
|
|
29
40
|
* - works on optional parameters
|
|
30
41
|
* - defuse the corresponding type constraint with an inline conditional
|
|
31
42
|
* - optionally take a fallback
|
|
32
43
|
*/
|
|
33
|
-
declare type Optional<N extends
|
|
44
|
+
declare type Optional<N extends Check, This extends N extends number ? Type_<Next<N>> : Type, Fallback = N extends number ? Exclude<This['constraints'][N], undefined> : N extends string ? Exclude<This['constraints'][This['labels'][N]], undefined> : never, Check extends string | number = N extends string ? keyof This['labels'] & string : number> = N extends number ? OptionalImpl<N, This, Fallback> : OptionalImpl<This['labels'][N], This, Fallback>;
|
|
45
|
+
declare type OptionalImpl<N extends number, This extends Type, Fallback> = This['arguments'][N] extends Exclude<This['constraints'][N], undefined> ? This['arguments'][N] : Fallback;
|
|
34
46
|
/** - safely index `this`
|
|
35
47
|
* - intersect the corresponding type constraint
|
|
36
48
|
*/
|
|
37
|
-
declare type Lossy<N extends
|
|
49
|
+
declare type Lossy<N extends Check, This extends N extends number ? Type<Next<N>> : Type, Check extends string | number = N extends string ? keyof This['labels'] & string : number> = N extends number ? This['arguments'][N] & This['constraints'][N] : This['arguments'][This['labels'][N]] & This['constraints'][This['labels'][N]];
|
|
38
50
|
/** - safely index `this`
|
|
39
51
|
* - intersect the corresponding type constraint
|
|
40
52
|
* - equals `0` When no argument is supplied
|
package/package.json
CHANGED