free-types-core 0.2.0 → 0.4.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/apply.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Type } from './Type';
2
- import { ArrayKeys, Slice } from './utils';
2
+ import { ArrayKeys, Next } from './utils';
3
3
 
4
4
  export { apply, $apply, Generic }
5
5
 
@@ -19,7 +19,14 @@ type applyUnsafe<
19
19
  Args extends unknown[],
20
20
  > = number extends $T['constraints']['length']
21
21
  ? _apply<$T, Args>
22
- : _apply<$T, Slice<Args, 0, $T['constraints']['length']>>;
22
+ : _apply<$T, Take<Args, Required<$T['constraints']>['length']>>;
23
23
 
24
24
  type _apply<T extends { type: unknown }, Args> =
25
- (T & Omit<Args, ArrayKeys> & { arguments: Args })['type'];
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 CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Next, IsUnknown, Prev } from './utils'
2
2
 
3
- export { At, Checked, Lossy, A, B, C, D, E }
3
+ export { At, Checked, Lossy, Optional, A, B, C, D, E }
4
4
 
5
5
  // Fake `Type` for user friendly error messages
6
6
  type Type<N extends number> = {
@@ -8,12 +8,17 @@ type Type<N extends number> = {
8
8
  arguments: unknown[]
9
9
  }
10
10
 
11
+ type Type_ <N extends number> = {
12
+ constraints: { [K in Prev<N>]?: unknown; }
13
+ arguments: unknown[]
14
+ }
11
15
  type Default = {
12
16
  constraints: [0,1,2,3,4]
13
17
  arguments: unknown[]
14
18
  }
15
19
 
16
- /** safely index `this`
20
+ /** - safely index `this`
21
+ * - optionally take a fallback
17
22
  */
18
23
  type At<
19
24
  N extends number,
@@ -22,7 +27,9 @@ type At<
22
27
  > = IsUnknown<This['arguments'][N]> extends true ? Fallback
23
28
  : This['arguments'][N]
24
29
 
25
- /** safely index `this` and defuse the corresponding type constraint with an inline conditional
30
+ /** - safely index `this`
31
+ * - defuse the corresponding type constraint with an inline conditional
32
+ * - optionally take a fallback
26
33
  */
27
34
  type Checked<
28
35
  N extends number,
@@ -33,8 +40,24 @@ type Checked<
33
40
  ? This['arguments'][N]
34
41
  : Fallback
35
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;
36
58
 
37
- /** safely index `this` and intersect the corresponding type constraint
59
+ /** - safely index `this`
60
+ * - intersect the corresponding type constraint
38
61
  */
39
62
  type Lossy<
40
63
  N extends number,
@@ -43,32 +66,37 @@ type Lossy<
43
66
  ? This['arguments'][N] & This['constraints'][N]
44
67
  : never;
45
68
 
46
- /** safely index `this` and intersect the corresponding type constraint
47
- * equals `0` When no argument is supplied
69
+ /** - safely index `this`
70
+ * - intersect the corresponding type constraint
71
+ * - equals `0` When no argument is supplied
48
72
  */
49
73
  type A<This extends Type<1> = Default> =
50
74
  This['arguments'][0] & This['constraints'][0];
51
75
 
52
- /** safely index `this` and intersect the corresponding type constraint
53
- * equals `1` When no argument is supplied
76
+ /** - safely index `this`
77
+ * - intersect the corresponding type constraint
78
+ * - equals `1` When no argument is supplied
54
79
  */
55
80
  type B<This extends Type<2> = Default> =
56
81
  This['arguments'][1] & This['constraints'][1];
57
82
 
58
- /** safely index `this` and intersect the corresponding type constraint
59
- * equals `2` When no argument is supplied
83
+ /** - safely index `this`
84
+ * - intersect the corresponding type constraint
85
+ * - equals `2` When no argument is supplied
60
86
  */
61
87
  type C<This extends Type<3> = Default> =
62
88
  This['arguments'][2] & This['constraints'][2];
63
89
 
64
- /** safely index `this` and intersect the corresponding type constraint
65
- * equals `3` When no argument is supplied
90
+ /** - safely index `this`
91
+ * - intersect the corresponding type constraint
92
+ * - equals `3` When no argument is supplied
66
93
  */
67
94
  type D<This extends Type<4> = Default> =
68
95
  This['arguments'][3] & This['constraints'][3];
69
96
 
70
- /** safely index `this` and intersect the corresponding type constraint
71
- * equals `4` When no argument is supplied
97
+ /** - safely index `this`
98
+ * - intersect the corresponding type constraint
99
+ * - equals `4` When no argument is supplied
72
100
  */
73
101
  type E<This extends Type<5> = Default> =
74
102
  This['arguments'][4] & This['constraints'][4];
package/index.ts CHANGED
@@ -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 } from './utils'
12
+ export { Slice, ToTuple, Head, Tail, Last, Init, IsUnknown, IsOptional } from './utils'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "free-types-core",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
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",
package/partial.ts CHANGED
@@ -24,7 +24,7 @@ type _partial <$T extends Type, Args extends unknown[]> =
24
24
  Args,
25
25
  Slice<$T['constraints'],
26
26
  Args['length'],
27
- $T['constraints']['length']>,
27
+ Required<$T['constraints']>['length']>,
28
28
  'left'
29
29
  >
30
30
 
@@ -37,7 +37,7 @@ type partialRight<
37
37
  $T extends $Model ? PartialType<
38
38
  $T,
39
39
  Args,
40
- Slice<$T['constraints'], 0, Subtract<$T['constraints']['length'], Args['length']>>,
40
+ Slice<$T['constraints'], 0, Subtract<Required<$T['constraints']>['length'], Args['length']>>,
41
41
  'right'
42
42
  > : never
43
43
 
package/utils.ts CHANGED
@@ -2,7 +2,7 @@ export { Natural, Prev, Next, Subtract }
2
2
 
3
3
  export { ArrayKeys, Slice, ToTuple, Tuple, Head, Tail, Last, Init }
4
4
 
5
- export { Extends, Eq, IsUnknown, IsAny }
5
+ export { Extends, Eq, IsUnknown, IsAny, IsOptional }
6
6
 
7
7
  // Numbers
8
8
 
@@ -35,12 +35,20 @@ type ArrayKeys = Exclude<keyof [], never>;
35
35
  type Slice<
36
36
  T extends unknown[],
37
37
  From extends number,
38
- To extends number = T['length'],
38
+ To extends number = Required<T>['length'],
39
39
  I extends number = From,
40
40
  R extends unknown[] = [],
41
41
  > =
42
42
  I extends To ? R
43
- : Slice<T, From, To, Next<I>, [...R, T[I]]>;
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;
44
52
 
45
53
  type ToTuple<
46
54
  T extends readonly unknown[],