free-types-core 0.4.0 → 0.4.2

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.
@@ -0,0 +1,28 @@
1
+ import { Type } from './Type';
2
+ export { Const, $Const };
3
+ /**
4
+ * Turn a type `T` into a constant `Type<❋, T>`, totally ignoring arity and type constraints on its input.
5
+ */
6
+ type Const<T> = {
7
+ type: T;
8
+ constraints: any;
9
+ arguments: any[] & {
10
+ length: any;
11
+ };
12
+ 0: any;
13
+ 1: any;
14
+ 2: any;
15
+ 3: any;
16
+ 4: any;
17
+ 5: any;
18
+ 6: any;
19
+ 7: any;
20
+ 8: any;
21
+ 9: any;
22
+ };
23
+ /**
24
+ * Turn the input into a constant `Type<❋, T>`, totally ignoring arity and type constraints on its input.
25
+ */
26
+ interface $Const extends Type<1> {
27
+ type: Const<this[0]>;
28
+ }
package/dist/Const.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import { Type } from './Type';
2
+ import { Slice, Subtract } from './utils';
3
+ export { Remaining };
4
+ /** Use as a type constraint to match a Type whose state of application is known */
5
+ type Remaining<T extends Type, R extends number> = unknown[] extends T['constraints'] ? Type : Type<Slice<T['constraints'], Subtract<T['constraints']['length'], R>, T['constraints']['length']>, T['type']>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ import { unwrap, Unwrapped, Search } from './unwrap';
2
+ import { apply } from './apply';
3
+ import { TypesMap } from './TypesMap';
4
+ export { Replace };
5
+ /** Replace the inner value of a type with compatible arguments.
6
+ *
7
+ * Can be used to constrain the inner value of a type
8
+ */
9
+ type Replace<T, Args extends U['type']['constraints'], From extends Search = TypesMap, U extends Unwrapped = unwrap<T, From>> = apply<U['type'], Args>;
@@ -0,0 +1 @@
1
+ export {};
package/dist/Type.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ import { Tuple } from './utils';
2
+ export { Type };
3
+ /** Extend `Type<Input?, Output?>` with an interface to produce a free type, or use it as a type constraint.*/
4
+ type Type<Input extends number | unknown[] = unknown[], Output = unknown> = CreateType<{
5
+ type: Output;
6
+ constraints: Constraints<Input, Slots<Input>>;
7
+ arguments: unknown[];
8
+ }>;
9
+ interface CreateType<T extends {
10
+ type: unknown;
11
+ constraints: unknown[];
12
+ }> {
13
+ type: T['type'];
14
+ constraints: T['constraints'];
15
+ arguments: unknown[];
16
+ 0: unknown;
17
+ 1: unknown;
18
+ 2: unknown;
19
+ 3: unknown;
20
+ 4: unknown;
21
+ 5: unknown;
22
+ 6: unknown;
23
+ 7: unknown;
24
+ 8: unknown;
25
+ 9: unknown;
26
+ }
27
+ type Slots<T> = T extends Precomputable ? PrecomputedSlots[T & Precomputable] : Tuple<T extends unknown[] ? T['length'] : T extends number ? T : number>;
28
+ type Constraints<Input, Slots> = number extends Input ? unknown[] : unknown[] extends Input ? unknown[] : Input extends unknown[] ? Input : Slots;
29
+ type PrecomputedSlots = {
30
+ [I in Precomputable]: Tuple<Seq10[I]>;
31
+ };
32
+ type Precomputable = Seq10[number];
33
+ type Seq10 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
package/dist/Type.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,17 @@
1
+ import * as free from './free-types';
2
+ /** A mapping of everyday free types which you can extend with module augmentation. */
3
+ export interface TypesMap {
4
+ Record: free.Record;
5
+ Array: free.Array;
6
+ Tuple: free.Tuple;
7
+ ReadonlyTuple: free.ReadonlyTuple;
8
+ ReadonlyArray: free.ReadonlyArray;
9
+ Set: free.Set;
10
+ ReadonlySet: free.ReadonlySet;
11
+ WeakSet: free.WeakSet;
12
+ Map: free.Map;
13
+ ReadonlyMap: free.ReadonlyMap;
14
+ WeakMap: free.WeakMap;
15
+ Function: free.Function;
16
+ Promise: free.Promise;
17
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,22 @@
1
+ import { Type } from './Type';
2
+ import { ArrayKeys, Next } from './utils';
3
+ export { apply, $apply, Generic };
4
+ /** Apply a free type with all its arguments and evaluate the type */
5
+ type apply<$T extends Type, Args extends $T['constraints'] = []> = applyUnsafe<$T, Args>;
6
+ interface $apply<Args extends unknown[] = []> extends Type<[Type]> {
7
+ type: apply<this[0] extends Type ? this[0] : Type, Args>;
8
+ }
9
+ /** Apply a free type `$T` with its type constraints. */
10
+ type Generic<$T extends Type> = _apply<$T, $T['constraints']>;
11
+ type applyUnsafe<$T extends {
12
+ type: unknown;
13
+ constraints: {
14
+ length: number;
15
+ };
16
+ }, Args extends unknown[]> = number extends $T['constraints']['length'] ? _apply<$T, Args> : _apply<$T, Take<Args, Required<$T['constraints']>['length']>>;
17
+ type _apply<T extends {
18
+ type: unknown;
19
+ }, Args> = (T & Omit<Args, ArrayKeys> & {
20
+ arguments: Args;
21
+ })['type'];
22
+ type Take<T extends unknown[], To extends number, I extends number = 0, R extends unknown[] = []> = I extends To ? R : Take<T, To, Next<I>, [...R, T[I]]>;
package/dist/apply.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -1,82 +1,49 @@
1
- import { Type } from './Type'
2
- import { ToTuple } from './utils'
3
-
4
- export {
5
- $Id as Id,
6
- $Record as Record,
7
- $Array as Array,
8
- $Tuple as Tuple,
9
- $ReadonlyTuple as ReadonlyTuple,
10
- $ReadonlyArray as ReadonlyArray,
11
- $Set as Set,
12
- $ReadonlySet as ReadonlySet,
13
- $WeakSet as WeakSet,
14
- $Map as Map,
15
- $ReadonlyMap as ReadonlyMap,
16
- $WeakMap as WeakMap,
17
- $Function as Function,
18
- $UnaryFunction as UnaryFunction,
19
- $Promise as Promise
20
- }
21
-
1
+ import { Type } from './Type';
2
+ import { ToTuple } from './utils';
3
+ export { $Id as Id, $Record as Record, $Array as Array, $Tuple as Tuple, $ReadonlyTuple as ReadonlyTuple, $ReadonlyArray as ReadonlyArray, $Set as Set, $ReadonlySet as ReadonlySet, $WeakSet as WeakSet, $Map as Map, $ReadonlyMap as ReadonlyMap, $WeakMap as WeakMap, $Function as Function, $UnaryFunction as UnaryFunction, $Promise as Promise };
22
4
  interface $Id extends Type<1> {
23
- type: this[0]
5
+ type: this[0];
24
6
  }
25
-
26
7
  type Key = string | number | symbol;
27
-
28
8
  interface $Record extends Type<[Key, unknown]> {
29
- type: Record<this[0] & Key, this[1]>
9
+ type: Record<this[0] & Key, this[1]>;
30
10
  }
31
-
32
11
  interface $Array extends Type<1> {
33
- type: Array<this[0]>
12
+ type: Array<this[0]>;
34
13
  }
35
-
36
14
  interface $Tuple extends Type {
37
15
  type: ToTuple<this['arguments']>;
38
16
  }
39
-
40
17
  interface $ReadonlyTuple extends Type {
41
18
  type: Readonly<ToTuple<this['arguments']>>;
42
19
  }
43
-
44
20
  interface $ReadonlyArray extends Type<1> {
45
- type: ReadonlyArray<this[0]>
21
+ type: ReadonlyArray<this[0]>;
46
22
  }
47
-
48
23
  interface $Set extends Type<1> {
49
- type: Set<this[0]>
24
+ type: Set<this[0]>;
50
25
  }
51
-
52
26
  interface $ReadonlySet extends Type<1> {
53
- type: ReadonlySet<this[0]>
27
+ type: ReadonlySet<this[0]>;
54
28
  }
55
-
56
29
  interface $WeakSet extends Type<[object]> {
57
- type: WeakSet<this[0] extends object ? this[0] : object>
30
+ type: WeakSet<this[0] extends object ? this[0] : object>;
58
31
  }
59
-
60
32
  interface $Map extends Type<2> {
61
- type: Map<this[0], this[1]>
33
+ type: Map<this[0], this[1]>;
62
34
  }
63
-
64
35
  interface $ReadonlyMap extends Type<2> {
65
- type: ReadonlyMap<this[0], this[1]>
36
+ type: ReadonlyMap<this[0], this[1]>;
66
37
  }
67
-
68
38
  interface $WeakMap extends Type<[object, unknown]> {
69
- type: WeakMap<this[0] extends object ? this[0] : object, this[1]>
39
+ type: WeakMap<this[0] extends object ? this[0] : object, this[1]>;
70
40
  }
71
-
72
41
  interface $Function extends Type<[any[], unknown]> {
73
- type: (...args: this[0] extends any[] ? this[0] : any[]) => this[1]
42
+ type: (...args: this[0] extends any[] ? this[0] : any[]) => this[1];
74
43
  }
75
-
76
44
  interface $UnaryFunction extends Type<2> {
77
- type: (a: unknown extends this[0] ? any : this[0]) => this[1]
45
+ type: (a: unknown extends this[0] ? any : this[0]) => this[1];
78
46
  }
79
-
80
47
  interface $Promise extends Type<1> {
81
- type: Promise<this[0]>
82
- }
48
+ type: Promise<this[0]>;
49
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,62 @@
1
+ import { Next, IsUnknown, Prev } from './utils';
2
+ export { At, Checked, Lossy, Optional, A, B, C, D, E };
3
+ type Type<N extends number> = {
4
+ constraints: {
5
+ [K in Prev<N>]: unknown;
6
+ };
7
+ arguments: unknown[];
8
+ };
9
+ type Type_<N extends number> = {
10
+ constraints: {
11
+ [K in Prev<N>]?: unknown;
12
+ };
13
+ arguments: unknown[];
14
+ };
15
+ type Default = {
16
+ constraints: [0, 1, 2, 3, 4];
17
+ arguments: unknown[];
18
+ };
19
+ /** - safely index `this`
20
+ * - optionally take a fallback
21
+ */
22
+ type At<N extends number, This extends Type<Next<N>>, Fallback = unknown> = IsUnknown<This['arguments'][N]> extends true ? Fallback : This['arguments'][N];
23
+ /** - safely index `this`
24
+ * - defuse the corresponding type constraint with an inline conditional
25
+ * - optionally take a fallback
26
+ */
27
+ type Checked<N extends number, This extends Type<Next<N>>, Fallback = N extends keyof This['constraints'] ? This['constraints'][N] : never> = N extends keyof This['constraints'] ? This['arguments'][N] extends This['constraints'][N] ? This['arguments'][N] : Fallback : never;
28
+ /** - safely index `this`
29
+ * - works on optional parameters
30
+ * - defuse the corresponding type constraint with an inline conditional
31
+ * - optionally take a fallback
32
+ */
33
+ type Optional<N extends number, This extends Type_<Next<N>>, Fallback = N extends keyof This['constraints'] ? Exclude<This['constraints'][N], undefined> : never> = N extends keyof This['constraints'] ? This['arguments'][N] extends Exclude<This['constraints'][N], undefined> ? This['arguments'][N] : Fallback : never;
34
+ /** - safely index `this`
35
+ * - intersect the corresponding type constraint
36
+ */
37
+ type Lossy<N extends number, This extends Type<Next<N>>> = N extends keyof This['constraints'] ? This['arguments'][N] & This['constraints'][N] : never;
38
+ /** - safely index `this`
39
+ * - intersect the corresponding type constraint
40
+ * - equals `0` When no argument is supplied
41
+ */
42
+ type A<This extends Type<1> = Default> = This['arguments'][0] & This['constraints'][0];
43
+ /** - safely index `this`
44
+ * - intersect the corresponding type constraint
45
+ * - equals `1` When no argument is supplied
46
+ */
47
+ type B<This extends Type<2> = Default> = This['arguments'][1] & This['constraints'][1];
48
+ /** - safely index `this`
49
+ * - intersect the corresponding type constraint
50
+ * - equals `2` When no argument is supplied
51
+ */
52
+ type C<This extends Type<3> = Default> = This['arguments'][2] & This['constraints'][2];
53
+ /** - safely index `this`
54
+ * - intersect the corresponding type constraint
55
+ * - equals `3` When no argument is supplied
56
+ */
57
+ type D<This extends Type<4> = Default> = This['arguments'][3] & This['constraints'][3];
58
+ /** - safely index `this`
59
+ * - intersect the corresponding type constraint
60
+ * - equals `4` When no argument is supplied
61
+ */
62
+ type E<This extends Type<5> = Default> = This['arguments'][4] & This['constraints'][4];
@@ -0,0 +1 @@
1
+ export {};
package/dist/hkt.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { Type } from "./Type";
2
+ export { HKT, Contra };
3
+ /** Interfaces extending this type are able to use `hkt` internally. */
4
+ type HKT = {
5
+ HKT: [any, ...any[]];
6
+ };
7
+ /** Expect a free type with contravariant arguments */
8
+ type Contra<$T extends Type, $U extends Type> = $U['constraints'] extends $T['constraints'] ? $T['type'] extends $U['type'] ? Type : Type<$T['constraints'], $U['type']> : {
9
+ 'Contravariance issue': {
10
+ 'the input': $U['constraints'];
11
+ 'is not assignable to': $T['constraints'];
12
+ };
13
+ };
package/dist/hkt.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -9,4 +9,4 @@ export * from './unwrap';
9
9
  export * from './Replace';
10
10
  export * from './hkt';
11
11
  export * as free from './free-types';
12
- export { Slice, ToTuple, Head, Tail, Last, Init, IsUnknown, IsOptional } from './utils'
12
+ export { Slice, ToTuple, Head, Tail, Last, Init, IsUnknown, IsOptional } from './utils';
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ export * from './apply';
2
+ export * from './partial';
3
+ export * from './Remaining';
4
+ export * from './Type';
5
+ export * from './Const';
6
+ export * from './helpers';
7
+ export * from './inferArgs';
8
+ export * from './unwrap';
9
+ export * from './Replace';
10
+ export * from './hkt';
11
+ export * as free from './free-types';
@@ -0,0 +1,29 @@
1
+ import { Type } from './Type';
2
+ import { apply } from './apply';
3
+ export { inferArgs };
4
+ type inferArgs<T, $T extends Type> = T extends apply<$T, [
5
+ infer A,
6
+ infer B,
7
+ infer C,
8
+ infer D,
9
+ infer E,
10
+ infer F,
11
+ infer G,
12
+ infer H,
13
+ infer I,
14
+ infer J
15
+ ]> ? Extract<[A, B, C, D, E, F, G, H, I, J]>[$T['constraints']['length']] : never;
16
+ interface Extract<T extends unknown[]> {
17
+ [k: number]: unknown[];
18
+ 0: [];
19
+ 1: [T[0]];
20
+ 2: [T[0], T[1]];
21
+ 3: [T[0], T[1], T[2]];
22
+ 4: [T[0], T[1], T[2], T[3]];
23
+ 5: [T[0], T[1], T[2], T[3], T[4]];
24
+ 6: [T[0], T[1], T[2], T[3], T[4], T[5]];
25
+ 7: [T[0], T[1], T[2], T[3], T[4], T[5], T[6]];
26
+ 8: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7]];
27
+ 9: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], T[8]];
28
+ 10: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], T[8], T[9]];
29
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,19 @@
1
+ import { Type } from './Type';
2
+ import { apply } from './apply';
3
+ import { Slice, Subtract, ToTuple } from './utils';
4
+ export { partial, partialRight, PartialRight, $partial };
5
+ /** Return a new type based upon `$T`, with `Args` already applied, expecting the remaining arguments */
6
+ type partial<$T extends Type, Args extends Partial<$Model['constraints']>, $Model extends Type = $T> = $T extends $Model ? Args extends unknown[] ? _partial<$T, Args> : never : never;
7
+ /** A free version of `partial` */
8
+ interface $partial<$T extends Type> extends Type<1> {
9
+ type: _partial<$T, [this['arguments'][0]]>;
10
+ }
11
+ type _partial<$T extends Type, Args extends unknown[]> = PartialType<$T, Args, Slice<$T['constraints'], Args['length'], Required<$T['constraints']>['length']>, 'left'>;
12
+ /** Return a new type based upon `$T`, with `Args` already applied to the rightmost parameters of `$T`, and expecting the remaining arguments */
13
+ type partialRight<$T extends Type, Args extends PartialRight<$Model['constraints']>, $Model extends Type = $T> = $T extends $Model ? PartialType<$T, Args, Slice<$T['constraints'], 0, Subtract<Required<$T['constraints']>['length'], Args['length']>>, 'right'> : never;
14
+ type PartialRight<T extends unknown[], R = never> = T extends [unknown, ...(infer Rest)] ? PartialRight<Rest, T | R> : [] | R;
15
+ interface PartialType<$T extends Type, Applied extends unknown[], Constraints extends unknown[], Direction> extends Type {
16
+ type: apply<$T, Direction extends 'left' ? [...Applied, ...ToTuple<this['arguments']>] : [...ToTuple<this['arguments']>, ...Applied]>;
17
+ constraints: Constraints;
18
+ arguments: unknown[];
19
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,34 @@
1
+ import { ArrayKeys, Eq } from './utils';
2
+ import { Type } from './Type';
3
+ import { inferArgs } from './inferArgs';
4
+ import { Generic, apply } from './apply';
5
+ import { TypesMap } from './TypesMap';
6
+ import { Tuple, ReadonlyTuple, Array, ReadonlyArray } from './free-types';
7
+ type Search = Type | SearchList;
8
+ type SearchList = Type[] | Record<string, Type> | Interface;
9
+ type Interface = {
10
+ [k: string]: any;
11
+ } & {
12
+ [Symbol.toStringTag]?: never;
13
+ };
14
+ export { unwrap, Unwrapped, Search };
15
+ /** Decompose a type into its constituents */
16
+ type unwrap<T, From extends Search = TypesMap> = T extends readonly unknown[] ? unwrapLists<T> : _unwrap<T, From extends Type ? [From] : From>;
17
+ type unwrapLists<T> = T extends unknown[] ? T extends [unknown, ...unknown[]] ? Unwrapped<'Tuple', Tuple, T> : Unwrapped<'Array', Array, [T[0]]> : T extends readonly [unknown, ...unknown[]] ? Unwrapped<'ReadonlyTuple', ReadonlyTuple, Mutable<T>> : Unwrapped<'ReadonlyArray', ReadonlyArray, [(T & unknown[])[0]]>;
18
+ type Mutable<T> = {
19
+ -readonly [K in keyof T]: K extends ArrayKeys ? T[K] : T[K];
20
+ };
21
+ type _unwrap<T, From extends SearchList, R = {
22
+ [K in keyof From as K extends ArrayKeys ? never : K extends string ? K : never]: From[K] extends Type ? Branch<T, From[K], K>[T extends Generic<From[K]> ? 'match' : 'miss'] : never;
23
+ }> = R[keyof R];
24
+ interface Branch<T, $T extends Type, K> {
25
+ match: Disambiguate<T, $T, inferArgs<T, $T>, K & string>;
26
+ miss: never;
27
+ }
28
+ type Disambiguate<T, $T extends Type, Args extends unknown[], K extends string> = Eq<T, apply<$T, Args>> extends true ? Unwrapped<K, $T, Args> : never;
29
+ /** The result of unwrapping a type with `unwrap` */
30
+ type Unwrapped<URI extends string = string, T extends Type = Type, A = unknown[]> = {
31
+ URI: URI;
32
+ type: T;
33
+ args: A;
34
+ };
package/dist/unwrap.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,29 @@
1
+ export { Natural, Prev, Next, Subtract };
2
+ export { ArrayKeys, Slice, ToTuple, Tuple, Head, Tail, Last, Init };
3
+ export { Extends, Eq, IsUnknown, IsAny, IsOptional };
4
+ type Natural = [0, ..._Next];
5
+ type Prev<I extends number> = number extends I ? number : number & _Prev[I];
6
+ type _Prev = [never, ...Natural];
7
+ type Next<I extends number> = number extends I ? number : number & _Next[I];
8
+ type _Next = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64];
9
+ type Subtract<A extends number, B extends number> = number extends A ? number : number extends B ? number : _Subtract<A, B>;
10
+ type _Subtract<A extends number, B extends number, _A = Prev<A>, _B = Prev<B>> = B extends 0 ? A : [_A] extends [never] ? never : [_B] extends never ? never : _Subtract<_A & number, _B & number>;
11
+ type ArrayKeys = Exclude<keyof [], never>;
12
+ type Slice<T extends unknown[], From extends number, To extends number = Required<T>['length'], I extends number = From, R extends unknown[] = []> = I extends To ? R : Slice<T, From, To, Next<I>, IsOptional<T, I> extends true ? [...R, T[I]?] : [...R, T[I]]>;
13
+ type IsOptional<T extends unknown[], I extends number> = [
14
+ {
15
+ [K in T['length'] as K]: K extends I ? never : unknown;
16
+ }[I]
17
+ ] extends [never] ? true : false;
18
+ type ToTuple<T extends readonly unknown[], I extends number = 0, R extends unknown[] = []> = any[] extends T ? T[0][] : I extends T['length'] ? R : ToTuple<T, Next<I>, [...R, T[I]]>;
19
+ type Tuple<L extends number, T = unknown, R extends unknown[] = []> = number extends L ? T[] : L extends R['length'] ? R : Tuple<L, T, [T, ...R]>;
20
+ type Head<T extends [unknown, ...unknown[]]> = T[0];
21
+ type Tail<T extends [unknown, ...unknown[]]> = T extends [unknown, ...infer R] ? R : never;
22
+ type Last<T extends unknown[]> = T[Prev<T['length']>];
23
+ type Init<T extends [unknown, ...unknown[]]> = T extends [...infer R, unknown] ? R : never;
24
+ type IsUnknown<T> = unknown extends T ? IsAny<T> extends false ? true : false : false;
25
+ type IsAny<T> = Extends<T | anything, T & anything>;
26
+ type Extends<A, B> = [A] extends [B] ? true : false;
27
+ type Eq<A, B> = [A] extends [B] ? [B] extends [A] ? true : false : false;
28
+ declare const thing: unique symbol;
29
+ type anything = typeof thing;
package/dist/utils.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "free-types-core",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "A type-level library enabling the creation and the manipulation of type constructors which are detached from their type parameters.",
5
5
  "keywords:": [
6
6
  "higher kinded type",
@@ -9,10 +9,14 @@
9
9
  ],
10
10
  "exports": {
11
11
  ".": {
12
- "import": "./index.ts"
12
+ "import": "./dist/index.js"
13
13
  }
14
14
  },
15
- "types": "./index.ts",
15
+ "main": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "files": [
18
+ "dist/*"
19
+ ],
16
20
  "author": "Geoffrey Gourdet",
17
21
  "repository": {
18
22
  "type": "git",
@@ -1,3 +0,0 @@
1
- {
2
- "typescript.tsdk": "node_modules\\typescript\\lib"
3
- }
package/Const.ts DELETED
@@ -1,27 +0,0 @@
1
- import { Type } from './Type'
2
-
3
- export { Const, $Const };
4
-
5
- /**
6
- * Turn a type `T` into a constant `Type<❋, T>`, totally ignoring arity and type constraints on its input.
7
- */
8
- type Const<T> = {
9
- type: T
10
- constraints: any
11
- arguments: any[] & { length: any },
12
- 0: any
13
- 1: any
14
- 2: any
15
- 3: any
16
- 4: any
17
- 5: any
18
- 6: any
19
- 7: any
20
- 8: any
21
- 9: any
22
- }
23
-
24
- /**
25
- * Turn the input into a constant `Type<❋, T>`, totally ignoring arity and type constraints on its input.
26
- */
27
- interface $Const extends Type<1> { type: Const<this[0]> }
package/Remaining.ts DELETED
@@ -1,15 +0,0 @@
1
- import { Type } from './Type';
2
- import { Slice, Subtract } from './utils';
3
-
4
- export { Remaining };
5
-
6
- /** Use as a type constraint to match a Type whose state of application is known */
7
- type Remaining<
8
- T extends Type,
9
- R extends number,
10
- > = unknown[] extends T['constraints'] ? Type : Type<Slice<
11
- T['constraints'],
12
- Subtract<T['constraints']['length'], R>,
13
- T['constraints']['length']>,
14
- T['type']
15
- >;
package/Replace.ts DELETED
@@ -1,17 +0,0 @@
1
- import { unwrap, Unwrapped, Search } from './unwrap'
2
- import { apply } from './apply';
3
-
4
- import { TypesMap } from './TypesMap';
5
-
6
- export { Replace }
7
-
8
- /** Replace the inner value of a type with compatible arguments.
9
- *
10
- * Can be used to constrain the inner value of a type
11
- */
12
- type Replace<
13
- T,
14
- Args extends U['type']['constraints'],
15
- From extends Search = TypesMap,
16
- U extends Unwrapped = unwrap<T, From>
17
- > = apply<U['type'], Args>
package/Type.ts DELETED
@@ -1,42 +0,0 @@
1
- import { Tuple } from './utils';
2
-
3
- export { Type };
4
-
5
- /** Extend `Type<Input?, Output?>` with an interface to produce a free type, or use it as a type constraint.*/
6
-
7
- type Type<Input extends number | unknown[] = unknown[], Output = unknown> =
8
- CreateType<{
9
- type: Output
10
- constraints: Constraints<Input, Slots<Input>>
11
- arguments: unknown[]
12
- }>
13
-
14
- interface CreateType<T extends { type: unknown, constraints: unknown[] }> {
15
- type: T['type']
16
- constraints: T['constraints']
17
- arguments: unknown[]
18
- 0: unknown
19
- 1: unknown
20
- 2: unknown
21
- 3: unknown
22
- 4: unknown
23
- 5: unknown
24
- 6: unknown
25
- 7: unknown
26
- 8: unknown
27
- 9: unknown
28
- }
29
-
30
- type Slots<T> = T extends Precomputable
31
- ? PrecomputedSlots[T & Precomputable]
32
- : Tuple<T extends unknown[] ? T['length'] : T extends number ? T : number>;
33
-
34
- type Constraints<Input, Slots> =
35
- number extends Input ? unknown[]
36
- : unknown[] extends Input ? unknown[]
37
- : Input extends unknown[] ? Input
38
- : Slots
39
-
40
- type PrecomputedSlots = { [I in Precomputable]: Tuple<Seq10[I]> }
41
- type Precomputable = Seq10[number];
42
- type Seq10 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
package/TypesMap.ts DELETED
@@ -1,19 +0,0 @@
1
- import * as free from './free-types';
2
-
3
- /** A mapping of everyday free types which you can extend with module augmentation. */
4
- export interface TypesMap {
5
- Record: free.Record,
6
- Array: free.Array,
7
- Tuple: free.Tuple,
8
- ReadonlyTuple: free.ReadonlyTuple,
9
- ReadonlyArray: free.ReadonlyArray,
10
- Set: free.Set,
11
- ReadonlySet: free.ReadonlySet,
12
- WeakSet: free.WeakSet,
13
- Map: free.Map,
14
- ReadonlyMap: free.ReadonlyMap,
15
- WeakMap: free.WeakMap,
16
- Function: free.Function,
17
- // UnaryFunction is missing on purpose
18
- Promise: free.Promise
19
- }
package/apply.ts DELETED
@@ -1,32 +0,0 @@
1
- import { Type } from './Type';
2
- import { ArrayKeys, Next } from './utils';
3
-
4
- export { apply, $apply, Generic }
5
-
6
- /** Apply a free type with all its arguments and evaluate the type */
7
- type apply<$T extends Type, Args extends $T['constraints'] = []> =
8
- applyUnsafe<$T, Args>;
9
-
10
- interface $apply<Args extends unknown[] = []> extends Type<[Type]> {
11
- type: apply<this[0] extends Type ? this[0] : Type, Args>
12
- }
13
-
14
- /** Apply a free type `$T` with its type constraints. */
15
- type Generic<$T extends Type> = _apply<$T, $T['constraints']>;
16
-
17
- type applyUnsafe<
18
- $T extends { type: unknown, constraints: {length: number }},
19
- Args extends unknown[],
20
- > = number extends $T['constraints']['length']
21
- ? _apply<$T, Args>
22
- : _apply<$T, Take<Args, Required<$T['constraints']>['length']>>;
23
-
24
- type _apply<T extends { type: unknown }, Args> =
25
- (T & Omit<Args, ArrayKeys> & { arguments: Args })['type'];
26
-
27
- type Take<
28
- T extends unknown[],
29
- To extends number,
30
- I extends number = 0,
31
- R extends unknown[] = [],
32
- > = I extends To ? R : Take<T, To, Next<I>, [...R, T[I]]>
package/helpers.ts DELETED
@@ -1,102 +0,0 @@
1
- import { Next, IsUnknown, Prev } from './utils'
2
-
3
- export { At, Checked, Lossy, Optional, A, B, C, D, E }
4
-
5
- // Fake `Type` for user friendly error messages
6
- type Type<N extends number> = {
7
- constraints: { [K in Prev<N>]: unknown; }
8
- arguments: unknown[]
9
- }
10
-
11
- type Type_ <N extends number> = {
12
- constraints: { [K in Prev<N>]?: unknown; }
13
- arguments: unknown[]
14
- }
15
- type Default = {
16
- constraints: [0,1,2,3,4]
17
- arguments: unknown[]
18
- }
19
-
20
- /** - safely index `this`
21
- * - optionally take a fallback
22
- */
23
- type At<
24
- N extends number,
25
- This extends Type<Next<N>>,
26
- Fallback = unknown
27
- > = IsUnknown<This['arguments'][N]> extends true ? Fallback
28
- : This['arguments'][N]
29
-
30
- /** - safely index `this`
31
- * - defuse the corresponding type constraint with an inline conditional
32
- * - optionally take a fallback
33
- */
34
- type Checked<
35
- N extends number,
36
- This extends Type<Next<N>>,
37
- Fallback = N extends keyof This['constraints'] ? This['constraints'][N] : never
38
- > = N extends keyof This['constraints']
39
- ? This['arguments'][N] extends This['constraints'][N]
40
- ? This['arguments'][N]
41
- : Fallback
42
- : never;
43
-
44
- /** - safely index `this`
45
- * - works on optional parameters
46
- * - defuse the corresponding type constraint with an inline conditional
47
- * - optionally take a fallback
48
- */
49
- type Optional<
50
- N extends number,
51
- This extends Type_<Next<N>>,
52
- Fallback = N extends keyof This['constraints'] ? Exclude<This['constraints'][N], undefined> : never
53
- > = N extends keyof This['constraints']
54
- ? This['arguments'][N] extends Exclude<This['constraints'][N], undefined>
55
- ? This['arguments'][N]
56
- : Fallback
57
- : never;
58
-
59
- /** - safely index `this`
60
- * - intersect the corresponding type constraint
61
- */
62
- type Lossy<
63
- N extends number,
64
- This extends Type<Next<N>>
65
- > = N extends keyof This['constraints']
66
- ? This['arguments'][N] & This['constraints'][N]
67
- : never;
68
-
69
- /** - safely index `this`
70
- * - intersect the corresponding type constraint
71
- * - equals `0` When no argument is supplied
72
- */
73
- type A<This extends Type<1> = Default> =
74
- This['arguments'][0] & This['constraints'][0];
75
-
76
- /** - safely index `this`
77
- * - intersect the corresponding type constraint
78
- * - equals `1` When no argument is supplied
79
- */
80
- type B<This extends Type<2> = Default> =
81
- This['arguments'][1] & This['constraints'][1];
82
-
83
- /** - safely index `this`
84
- * - intersect the corresponding type constraint
85
- * - equals `2` When no argument is supplied
86
- */
87
- type C<This extends Type<3> = Default> =
88
- This['arguments'][2] & This['constraints'][2];
89
-
90
- /** - safely index `this`
91
- * - intersect the corresponding type constraint
92
- * - equals `3` When no argument is supplied
93
- */
94
- type D<This extends Type<4> = Default> =
95
- This['arguments'][3] & This['constraints'][3];
96
-
97
- /** - safely index `this`
98
- * - intersect the corresponding type constraint
99
- * - equals `4` When no argument is supplied
100
- */
101
- type E<This extends Type<5> = Default> =
102
- This['arguments'][4] & This['constraints'][4];
package/hkt.ts DELETED
@@ -1,18 +0,0 @@
1
- import { Type } from "./Type";
2
-
3
- export { HKT, Contra }
4
-
5
- /** Interfaces extending this type are able to use `hkt` internally. */
6
- type HKT = { HKT: [any, ...any[]] };
7
-
8
- /** Expect a free type with contravariant arguments */
9
- type Contra<$T extends Type, $U extends Type> =
10
- $U['constraints'] extends $T['constraints']
11
- ? $T['type'] extends $U['type']
12
- ? Type
13
- : Type<$T['constraints'], $U['type']>
14
- : {
15
- 'Contravariance issue': {
16
- 'the input': $U['constraints'], 'is not assignable to': $T['constraints']
17
- }
18
- };
package/inferArgs.ts DELETED
@@ -1,26 +0,0 @@
1
- import { Type } from './Type';
2
- import { apply } from './apply';
3
-
4
- export { inferArgs };
5
-
6
- type inferArgs<T, $T extends Type> =
7
- T extends apply<$T, [
8
- infer A, infer B, infer C, infer D, infer E,
9
- infer F, infer G, infer H, infer I, infer J
10
- ]> ? Extract<[A, B, C, D, E, F, G, H, I, J]>[$T['constraints']['length']]
11
- : never
12
-
13
- interface Extract<T extends unknown[]> {
14
- [k: number]: unknown[]
15
- 0: []
16
- 1: [T[0]]
17
- 2: [T[0], T[1]]
18
- 3: [T[0], T[1], T[2]]
19
- 4: [T[0], T[1], T[2], T[3]]
20
- 5: [T[0], T[1], T[2], T[3], T[4]]
21
- 6: [T[0], T[1], T[2], T[3], T[4], T[5]]
22
- 7: [T[0], T[1], T[2], T[3], T[4], T[5], T[6]]
23
- 8: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7]]
24
- 9: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], T[8]]
25
- 10: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], T[8], T[9]]
26
- }
package/partial.ts DELETED
@@ -1,60 +0,0 @@
1
- import { Type } from './Type';
2
- import { apply } from './apply';
3
- import { Slice, Subtract, ToTuple } from './utils';
4
-
5
- export { partial, partialRight, PartialRight, $partial }
6
-
7
- /** Return a new type based upon `$T`, with `Args` already applied, expecting the remaining arguments */
8
- type partial <
9
- $T extends Type,
10
- Args extends Partial<$Model['constraints']>,
11
- $Model extends Type = $T
12
- > = $T extends $Model ? Args extends unknown[]
13
- ? _partial<$T, Args>
14
- : never : never
15
-
16
- /** A free version of `partial` */
17
- interface $partial<$T extends Type> extends Type<1> {
18
- type: _partial<$T, [this['arguments'][0]]>
19
- }
20
-
21
- type _partial <$T extends Type, Args extends unknown[]> =
22
- PartialType<
23
- $T,
24
- Args,
25
- Slice<$T['constraints'],
26
- Args['length'],
27
- Required<$T['constraints']>['length']>,
28
- 'left'
29
- >
30
-
31
- /** Return a new type based upon `$T`, with `Args` already applied to the rightmost parameters of `$T`, and expecting the remaining arguments */
32
- type partialRight<
33
- $T extends Type,
34
- Args extends PartialRight<$Model['constraints']>,
35
- $Model extends Type = $T
36
- > =
37
- $T extends $Model ? PartialType<
38
- $T,
39
- Args,
40
- Slice<$T['constraints'], 0, Subtract<Required<$T['constraints']>['length'], Args['length']>>,
41
- 'right'
42
- > : never
43
-
44
- type PartialRight<T extends unknown[], R = never> =
45
- T extends [unknown, ...(infer Rest)]
46
- ? PartialRight<Rest, T | R>
47
- : [] | R
48
-
49
- interface PartialType<
50
- $T extends Type,
51
- Applied extends unknown[],
52
- Constraints extends unknown[],
53
- Direction
54
- > extends Type {
55
- type: apply<$T,
56
- Direction extends 'left' ? [...Applied, ...ToTuple<this['arguments']>] : [...ToTuple<this['arguments']>, ...Applied]
57
- >
58
- constraints: Constraints
59
- arguments: unknown[]
60
- }
package/tsconfig.json DELETED
@@ -1,16 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "rootDir": "./",
4
- "noEmit": true,
5
- "noErrorTruncation": true,
6
- "moduleResolution": "node",
7
- "module": "ES2020",
8
- "target": "ES2020",
9
- "strictFunctionTypes": true,
10
- "noImplicitAny": true,
11
- "strict": true
12
- },
13
- "include": [
14
- "./index.ts"
15
- ]
16
- }
package/unwrap.ts DELETED
@@ -1,55 +0,0 @@
1
- import { ArrayKeys, Eq } from './utils'
2
- import { Type } from './Type';
3
- import { inferArgs} from './inferArgs';
4
- import { Generic, apply } from './apply';
5
- import { TypesMap } from './TypesMap';
6
- import { Tuple, ReadonlyTuple, Array, ReadonlyArray } from './free-types';
7
-
8
- type Search = Type | SearchList;
9
- type SearchList = Type[] | Record<string, Type> | Interface;
10
- type Interface = {[k: string]: any} & { [Symbol.toStringTag]?: never }
11
-
12
- export { unwrap, Unwrapped, Search }
13
-
14
- /** Decompose a type into its constituents */
15
-
16
- type unwrap<T, From extends Search = TypesMap> =
17
- T extends readonly unknown[] ? unwrapLists<T>
18
- : _unwrap<T, From extends Type ? [From] : From>
19
-
20
- type unwrapLists<T> =
21
- T extends unknown[]
22
- ? T extends [unknown, ...unknown[]]
23
- ? Unwrapped<'Tuple', Tuple, T>
24
- : Unwrapped<'Array', Array, [T[0]]>
25
- : T extends readonly [unknown, ...unknown[]]
26
- ? Unwrapped<'ReadonlyTuple', ReadonlyTuple, Mutable<T>>
27
- : Unwrapped<'ReadonlyArray', ReadonlyArray, [(T & unknown[])[0]]>
28
-
29
- type Mutable<T> = {
30
- -readonly[K in keyof T]: K extends ArrayKeys ? T[K] : T[K]
31
- };
32
-
33
- type _unwrap<T, From extends SearchList, R = {
34
- [K in keyof From as K extends ArrayKeys ? never : K extends string ? K : never]:
35
- From[K] extends Type ? Branch<T, From[K], K>[
36
- T extends Generic<From[K]> ? 'match' : 'miss'
37
- ] : never
38
- }> = R[keyof R];
39
-
40
- interface Branch<T, $T extends Type, K> {
41
- match: Disambiguate<T, $T, inferArgs<T, $T>, K & string>
42
- miss: never
43
- }
44
-
45
- type Disambiguate<T, $T extends Type, Args extends unknown[], K extends string> =
46
- Eq<T, apply<$T, Args>> extends true
47
- ? Unwrapped<K, $T, Args>
48
- : never
49
-
50
- /** The result of unwrapping a type with `unwrap` */
51
- type Unwrapped<
52
- URI extends string = string,
53
- T extends Type = Type,
54
- A = unknown[]
55
- > = { URI: URI, type: T, args: A };
package/utils.ts DELETED
@@ -1,87 +0,0 @@
1
- export { Natural, Prev, Next, Subtract }
2
-
3
- export { ArrayKeys, Slice, ToTuple, Tuple, Head, Tail, Last, Init }
4
-
5
- export { Extends, Eq, IsUnknown, IsAny, IsOptional }
6
-
7
- // Numbers
8
-
9
- type Natural = [0, ..._Next];
10
-
11
- type Prev<I extends number> =
12
- number extends I ? number
13
- : number & _Prev[I];
14
-
15
- type _Prev = [never, ...Natural];
16
-
17
- type Next<I extends number> =
18
- number extends I ? number
19
- : number & _Next[I];
20
-
21
- type _Next = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64]
22
-
23
- type Subtract<A extends number, B extends number> =
24
- number extends A ? number : number extends B ? number
25
- : _Subtract<A, B>
26
-
27
- type _Subtract<A extends number, B extends number, _A = Prev<A>, _B = Prev<B>> =
28
- B extends 0 ? A : [_A] extends [never] ? never : [_B] extends never ? never
29
- : _Subtract<_A & number, _B & number>;
30
-
31
- // Tuples
32
-
33
- type ArrayKeys = Exclude<keyof [], never>;
34
-
35
- type Slice<
36
- T extends unknown[],
37
- From extends number,
38
- To extends number = Required<T>['length'],
39
- I extends number = From,
40
- R extends unknown[] = [],
41
- > =
42
- I extends To ? R
43
- : Slice<T, From, To, Next<I>,
44
- IsOptional<T, I> extends true
45
- ? [...R, T[I]?]
46
- : [...R, T[I]]
47
- >
48
-
49
- type IsOptional<T extends unknown[], I extends number> = [{
50
- [K in T['length'] as K]: K extends I ? never : unknown
51
- }[I]] extends [never] ? true : false;
52
-
53
- type ToTuple<
54
- T extends readonly unknown[],
55
- I extends number = 0,
56
- R extends unknown[] = []
57
- > = any[] extends T ? T[0][]
58
- : I extends T['length'] ? R
59
- : ToTuple<T, Next<I>, [...R, T[I]]>;
60
-
61
- type Tuple <L extends number, T = unknown, R extends unknown[] = []> =
62
- number extends L ? T[]
63
- : L extends R['length'] ? R
64
- : Tuple<L, T, [T, ...R]>;
65
-
66
- type Head<T extends [unknown, ...unknown[]]> = T[0];
67
-
68
- type Tail<T extends [unknown, ...unknown[]]> =
69
- T extends [unknown, ...infer R] ? R : never;
70
-
71
- type Last<T extends unknown[]> = T[Prev<T['length']>]
72
-
73
- type Init<T extends [unknown, ...unknown[]]> =
74
- T extends [...infer R, unknown] ? R : never;
75
-
76
-
77
- // Logic
78
-
79
- type IsUnknown<T> = unknown extends T ? IsAny<T> extends false ? true : false : false;
80
-
81
- type IsAny<T> = Extends<T | anything, T & anything>
82
-
83
- type Extends<A, B> = [A] extends [B] ? true : false;
84
- type Eq<A, B> = [A] extends [B] ? [B] extends [A] ? true : false : false;
85
-
86
- declare const thing: unique symbol;
87
- type anything = typeof thing;