free-types-core 0.2.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/.vscode/settings.json +3 -0
- package/Const.ts +27 -0
- package/Licence +5 -0
- package/README.md +3 -0
- package/Remaining.ts +15 -0
- package/Replace.ts +17 -0
- package/Type.ts +42 -0
- package/TypesMap.ts +19 -0
- package/apply.ts +25 -0
- package/free-types.ts +82 -0
- package/helpers.ts +74 -0
- package/hkt.ts +18 -0
- package/index.ts +12 -0
- package/inferArgs.ts +26 -0
- package/package.json +30 -0
- package/partial.ts +60 -0
- package/tsconfig.json +16 -0
- package/unwrap.ts +55 -0
- package/utils.ts +79 -0
package/Const.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
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/Licence
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
Copyright 2022 Geoffrey Gourdet
|
|
2
|
+
|
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
|
|
4
|
+
|
|
5
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
package/Remaining.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Type } from './Type';
|
|
2
|
+
import { ArrayKeys, Slice } 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, Slice<Args, 0, $T['constraints']['length']>>;
|
|
23
|
+
|
|
24
|
+
type _apply<T extends { type: unknown }, Args> =
|
|
25
|
+
(T & Omit<Args, ArrayKeys> & { arguments: Args })['type'];
|
package/free-types.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
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
|
+
|
|
22
|
+
interface $Id extends Type<1> {
|
|
23
|
+
type: this[0]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type Key = string | number | symbol;
|
|
27
|
+
|
|
28
|
+
interface $Record extends Type<[Key, unknown]> {
|
|
29
|
+
type: Record<this[0] & Key, this[1]>
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface $Array extends Type<1> {
|
|
33
|
+
type: Array<this[0]>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface $Tuple extends Type {
|
|
37
|
+
type: ToTuple<this['arguments']>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface $ReadonlyTuple extends Type {
|
|
41
|
+
type: Readonly<ToTuple<this['arguments']>>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface $ReadonlyArray extends Type<1> {
|
|
45
|
+
type: ReadonlyArray<this[0]>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface $Set extends Type<1> {
|
|
49
|
+
type: Set<this[0]>
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface $ReadonlySet extends Type<1> {
|
|
53
|
+
type: ReadonlySet<this[0]>
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface $WeakSet extends Type<[object]> {
|
|
57
|
+
type: WeakSet<this[0] extends object ? this[0] : object>
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interface $Map extends Type<2> {
|
|
61
|
+
type: Map<this[0], this[1]>
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface $ReadonlyMap extends Type<2> {
|
|
65
|
+
type: ReadonlyMap<this[0], this[1]>
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
interface $WeakMap extends Type<[object, unknown]> {
|
|
69
|
+
type: WeakMap<this[0] extends object ? this[0] : object, this[1]>
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
interface $Function extends Type<[any[], unknown]> {
|
|
73
|
+
type: (...args: this[0] extends any[] ? this[0] : any[]) => this[1]
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
interface $UnaryFunction extends Type<2> {
|
|
77
|
+
type: (a: unknown extends this[0] ? any : this[0]) => this[1]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface $Promise extends Type<1> {
|
|
81
|
+
type: Promise<this[0]>
|
|
82
|
+
}
|
package/helpers.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Next, IsUnknown, Prev } from './utils'
|
|
2
|
+
|
|
3
|
+
export { At, Checked, Lossy, 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 Default = {
|
|
12
|
+
constraints: [0,1,2,3,4]
|
|
13
|
+
arguments: unknown[]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** safely index `this`
|
|
17
|
+
*/
|
|
18
|
+
type At<
|
|
19
|
+
N extends number,
|
|
20
|
+
This extends Type<Next<N>>,
|
|
21
|
+
Fallback = unknown
|
|
22
|
+
> = IsUnknown<This['arguments'][N]> extends true ? Fallback
|
|
23
|
+
: This['arguments'][N]
|
|
24
|
+
|
|
25
|
+
/** safely index `this` and defuse the corresponding type constraint with an inline conditional
|
|
26
|
+
*/
|
|
27
|
+
type Checked<
|
|
28
|
+
N extends number,
|
|
29
|
+
This extends Type<Next<N>>,
|
|
30
|
+
Fallback = N extends keyof This['constraints'] ? This['constraints'][N] : never
|
|
31
|
+
> = N extends keyof This['constraints']
|
|
32
|
+
? This['arguments'][N] extends This['constraints'][N]
|
|
33
|
+
? This['arguments'][N]
|
|
34
|
+
: Fallback
|
|
35
|
+
: never;
|
|
36
|
+
|
|
37
|
+
/** safely index `this` and intersect the corresponding type constraint
|
|
38
|
+
*/
|
|
39
|
+
type Lossy<
|
|
40
|
+
N extends number,
|
|
41
|
+
This extends Type<Next<N>>
|
|
42
|
+
> = N extends keyof This['constraints']
|
|
43
|
+
? This['arguments'][N] & This['constraints'][N]
|
|
44
|
+
: never;
|
|
45
|
+
|
|
46
|
+
/** safely index `this` and intersect the corresponding type constraint
|
|
47
|
+
* equals `0` When no argument is supplied
|
|
48
|
+
*/
|
|
49
|
+
type A<This extends Type<1> = Default> =
|
|
50
|
+
This['arguments'][0] & This['constraints'][0];
|
|
51
|
+
|
|
52
|
+
/** safely index `this` and intersect the corresponding type constraint
|
|
53
|
+
* equals `1` When no argument is supplied
|
|
54
|
+
*/
|
|
55
|
+
type B<This extends Type<2> = Default> =
|
|
56
|
+
This['arguments'][1] & This['constraints'][1];
|
|
57
|
+
|
|
58
|
+
/** safely index `this` and intersect the corresponding type constraint
|
|
59
|
+
* equals `2` When no argument is supplied
|
|
60
|
+
*/
|
|
61
|
+
type C<This extends Type<3> = Default> =
|
|
62
|
+
This['arguments'][2] & This['constraints'][2];
|
|
63
|
+
|
|
64
|
+
/** safely index `this` and intersect the corresponding type constraint
|
|
65
|
+
* equals `3` When no argument is supplied
|
|
66
|
+
*/
|
|
67
|
+
type D<This extends Type<4> = Default> =
|
|
68
|
+
This['arguments'][3] & This['constraints'][3];
|
|
69
|
+
|
|
70
|
+
/** safely index `this` and intersect the corresponding type constraint
|
|
71
|
+
* equals `4` When no argument is supplied
|
|
72
|
+
*/
|
|
73
|
+
type E<This extends Type<5> = Default> =
|
|
74
|
+
This['arguments'][4] & This['constraints'][4];
|
package/hkt.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
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/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
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';
|
|
12
|
+
export { Slice, ToTuple, Head, Tail, Last, Init, IsUnknown } from './utils'
|
package/inferArgs.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
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/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "free-types-core",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "A type-level library enabling the creation and the manipulation of type constructors which are detached from their type parameters.",
|
|
5
|
+
"keywords:": [
|
|
6
|
+
"higher kinded type",
|
|
7
|
+
"type constructor",
|
|
8
|
+
"typescript"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./index.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"types": "./index.ts",
|
|
16
|
+
"author": "Geoffrey Gourdet",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/geoffreytools/free-types-core"
|
|
20
|
+
},
|
|
21
|
+
"license": "ICS",
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"typescript": "^4.7.2"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc",
|
|
27
|
+
"watch": "tsc --watch",
|
|
28
|
+
"trace": "tsc --generateTrace traceDir"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/partial.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
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
|
+
$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<$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
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export { Natural, Prev, Next, Subtract }
|
|
2
|
+
|
|
3
|
+
export { ArrayKeys, Slice, ToTuple, Tuple, Head, Tail, Last, Init }
|
|
4
|
+
|
|
5
|
+
export { Extends, Eq, IsUnknown, IsAny }
|
|
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 = T['length'],
|
|
39
|
+
I extends number = From,
|
|
40
|
+
R extends unknown[] = [],
|
|
41
|
+
> =
|
|
42
|
+
I extends To ? R
|
|
43
|
+
: Slice<T, From, To, Next<I>, [...R, T[I]]>;
|
|
44
|
+
|
|
45
|
+
type ToTuple<
|
|
46
|
+
T extends readonly unknown[],
|
|
47
|
+
I extends number = 0,
|
|
48
|
+
R extends unknown[] = []
|
|
49
|
+
> = any[] extends T ? T[0][]
|
|
50
|
+
: I extends T['length'] ? R
|
|
51
|
+
: ToTuple<T, Next<I>, [...R, T[I]]>;
|
|
52
|
+
|
|
53
|
+
type Tuple <L extends number, T = unknown, R extends unknown[] = []> =
|
|
54
|
+
number extends L ? T[]
|
|
55
|
+
: L extends R['length'] ? R
|
|
56
|
+
: Tuple<L, T, [T, ...R]>;
|
|
57
|
+
|
|
58
|
+
type Head<T extends [unknown, ...unknown[]]> = T[0];
|
|
59
|
+
|
|
60
|
+
type Tail<T extends [unknown, ...unknown[]]> =
|
|
61
|
+
T extends [unknown, ...infer R] ? R : never;
|
|
62
|
+
|
|
63
|
+
type Last<T extends unknown[]> = T[Prev<T['length']>]
|
|
64
|
+
|
|
65
|
+
type Init<T extends [unknown, ...unknown[]]> =
|
|
66
|
+
T extends [...infer R, unknown] ? R : never;
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
// Logic
|
|
70
|
+
|
|
71
|
+
type IsUnknown<T> = unknown extends T ? IsAny<T> extends false ? true : false : false;
|
|
72
|
+
|
|
73
|
+
type IsAny<T> = Extends<T | anything, T & anything>
|
|
74
|
+
|
|
75
|
+
type Extends<A, B> = [A] extends [B] ? true : false;
|
|
76
|
+
type Eq<A, B> = [A] extends [B] ? [B] extends [A] ? true : false : false;
|
|
77
|
+
|
|
78
|
+
declare const thing: unique symbol;
|
|
79
|
+
type anything = typeof thing;
|