@cjser/ts-pattern 5.9.0-cjser.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.
- package/LICENSE +21 -0
- package/README.md +1776 -0
- package/dist/errors.d.cts +8 -0
- package/dist/errors.d.ts +8 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/index.umd.js +2 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/internals/helpers.d.cts +12 -0
- package/dist/internals/helpers.d.ts +12 -0
- package/dist/internals/symbols.d.cts +24 -0
- package/dist/internals/symbols.d.ts +24 -0
- package/dist/is-matching.d.cts +40 -0
- package/dist/is-matching.d.ts +40 -0
- package/dist/match.d.cts +19 -0
- package/dist/match.d.ts +19 -0
- package/dist/patterns.d.cts +345 -0
- package/dist/patterns.d.ts +345 -0
- package/dist/types/BuildMany.d.cts +22 -0
- package/dist/types/BuildMany.d.ts +22 -0
- package/dist/types/DeepExclude.d.cts +2 -0
- package/dist/types/DeepExclude.d.ts +2 -0
- package/dist/types/DistributeUnions.d.cts +111 -0
- package/dist/types/DistributeUnions.d.ts +111 -0
- package/dist/types/ExtractPreciseValue.d.cts +27 -0
- package/dist/types/ExtractPreciseValue.d.ts +27 -0
- package/dist/types/FindSelected.d.cts +78 -0
- package/dist/types/FindSelected.d.ts +78 -0
- package/dist/types/InvertPattern.d.cts +104 -0
- package/dist/types/InvertPattern.d.ts +104 -0
- package/dist/types/IsMatching.d.cts +13 -0
- package/dist/types/IsMatching.d.ts +13 -0
- package/dist/types/Match.d.cts +171 -0
- package/dist/types/Match.d.ts +171 -0
- package/dist/types/Pattern.d.cts +401 -0
- package/dist/types/Pattern.d.ts +401 -0
- package/dist/types/helpers.d.cts +80 -0
- package/dist/types/helpers.d.ts +80 -0
- package/dist/types/index.d.cts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist-cjser/index.cjs +319 -0
- package/package.json +135 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Iterator, IsOptionalKeysOf, UpdateAt, ValueOf } from './helpers.cjs';
|
|
2
|
+
export type BuildMany<data, xs extends readonly any[]> = xs extends any ? BuildOne<data, xs> : never;
|
|
3
|
+
type BuildOne<data, xs extends readonly any[]> = xs extends [
|
|
4
|
+
[
|
|
5
|
+
infer value,
|
|
6
|
+
infer path
|
|
7
|
+
],
|
|
8
|
+
...infer tail
|
|
9
|
+
] ? BuildOne<SetDeep<data, value, path>, tail> : data;
|
|
10
|
+
export type SetDeep<data, value, path> = path extends readonly [
|
|
11
|
+
infer head,
|
|
12
|
+
...infer tail
|
|
13
|
+
] ? data extends readonly any[] ? data extends readonly [any, ...any] ? head extends number ? UpdateAt<data, Iterator<head>, SetDeep<data[head], value, tail>> : never : SetDeep<ValueOf<data>, value, tail>[] : data extends Set<infer a> ? Set<SetDeep<a, value, tail>> : data extends Map<infer k, infer v> ? Map<k, SetDeep<v, value, tail>> : head extends keyof data ? [
|
|
14
|
+
IsOptionalKeysOf<data, head>,
|
|
15
|
+
tail,
|
|
16
|
+
undefined
|
|
17
|
+
] extends [true, [], value] ? {
|
|
18
|
+
[k in keyof data]: k extends head ? value : data[k];
|
|
19
|
+
} : {
|
|
20
|
+
[k in keyof data]-?: k extends head ? SetDeep<data[head], value, tail> : data[k];
|
|
21
|
+
} : data : value;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Iterator, IsOptionalKeysOf, UpdateAt, ValueOf } from './helpers.js';
|
|
2
|
+
export type BuildMany<data, xs extends readonly any[]> = xs extends any ? BuildOne<data, xs> : never;
|
|
3
|
+
type BuildOne<data, xs extends readonly any[]> = xs extends [
|
|
4
|
+
[
|
|
5
|
+
infer value,
|
|
6
|
+
infer path
|
|
7
|
+
],
|
|
8
|
+
...infer tail
|
|
9
|
+
] ? BuildOne<SetDeep<data, value, path>, tail> : data;
|
|
10
|
+
export type SetDeep<data, value, path> = path extends readonly [
|
|
11
|
+
infer head,
|
|
12
|
+
...infer tail
|
|
13
|
+
] ? data extends readonly any[] ? data extends readonly [any, ...any] ? head extends number ? UpdateAt<data, Iterator<head>, SetDeep<data[head], value, tail>> : never : SetDeep<ValueOf<data>, value, tail>[] : data extends Set<infer a> ? Set<SetDeep<a, value, tail>> : data extends Map<infer k, infer v> ? Map<k, SetDeep<v, value, tail>> : head extends keyof data ? [
|
|
14
|
+
IsOptionalKeysOf<data, head>,
|
|
15
|
+
tail,
|
|
16
|
+
undefined
|
|
17
|
+
] extends [true, [], value] ? {
|
|
18
|
+
[k in keyof data]: k extends head ? value : data[k];
|
|
19
|
+
} : {
|
|
20
|
+
[k in keyof data]-?: k extends head ? SetDeep<data[head], value, tail> : data[k];
|
|
21
|
+
} : data : value;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { BuildMany } from './BuildMany.cjs';
|
|
2
|
+
import type { IsAny, Values, Flatten, IsUnion, IsPlainObject, Length, UnionToTuple, IsReadonlyArray, ValueOf, MaybeAddReadonly, IsStrictArray } from './helpers.cjs';
|
|
3
|
+
import { IsMatching } from './IsMatching.cjs';
|
|
4
|
+
/**
|
|
5
|
+
* DistributeMatchingUnions takes two arguments:
|
|
6
|
+
* - a data structure of type `a` containing unions
|
|
7
|
+
* - a pattern `p`, matching this data structure
|
|
8
|
+
* and turns it into a union of all possible
|
|
9
|
+
* combination of each unions contained in `a` that matches `p`.
|
|
10
|
+
*
|
|
11
|
+
* It does this in 3 main steps:
|
|
12
|
+
* - 1. Find all unions contained in the data structure, that matches `p`
|
|
13
|
+
* with `FindUnions<a, p>`. It returns a tree of [union, path] pairs.
|
|
14
|
+
* - 2. this tree is passed to the `Distribute` type level function,
|
|
15
|
+
* Which turns it into a union of list of `[singleValue, path]` pairs.
|
|
16
|
+
* Each list correspond to one of the possible combination of the unions
|
|
17
|
+
* found in `a`.
|
|
18
|
+
* - 3. build a data structure with the same shape as `a` for each combination
|
|
19
|
+
* and return the union of these data structures.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* type t1 = DistributeMatchingUnions<['a' | 'b', 1 | 2], ['a', 1]>;
|
|
23
|
+
* // => ['a', 1] | ['a', 2] | ['b', 1] | ['b', 2]
|
|
24
|
+
*
|
|
25
|
+
* type t2 = DistributeMatchingUnions<['a' | 'b', 1 | 2], ['a', unknown]>;
|
|
26
|
+
* // => ['a', 1 | 2] | ['b', 1 | 2]
|
|
27
|
+
*/
|
|
28
|
+
export type DistributeMatchingUnions<a, p> = IsAny<a> extends true ? any : BuildMany<a, Distribute<FindUnionsMany<a, p>>>;
|
|
29
|
+
export type FindUnionsMany<a, p, path extends PropertyKey[] = []> = UnionToTuple<(p extends any ? IsMatching<a, p> extends true ? FindUnions<a, p, path> : [] : never) extends readonly (infer T)[] ? T : never>;
|
|
30
|
+
/**
|
|
31
|
+
* The reason we don't look further down the tree with lists,
|
|
32
|
+
* Set and Maps is that they can be heterogeneous,
|
|
33
|
+
* so matching on a A[] for a in input of (A|B)[]
|
|
34
|
+
* doesn't rule anything out. You can still have
|
|
35
|
+
* a (A|B)[] afterward. The same logic goes for Set and Maps.
|
|
36
|
+
*
|
|
37
|
+
* Kinds are types of types.
|
|
38
|
+
*
|
|
39
|
+
* kind UnionConfig = {
|
|
40
|
+
* cases: Union<{
|
|
41
|
+
* value: b,
|
|
42
|
+
* subUnions: UnionConfig[]
|
|
43
|
+
* }>,
|
|
44
|
+
* path: string[]
|
|
45
|
+
* }
|
|
46
|
+
* FindUnions :: Pattern a p => a -> p -> UnionConfig[]
|
|
47
|
+
*/
|
|
48
|
+
export type FindUnions<a, p, path extends PropertyKey[] = []> = unknown extends p ? [] : IsAny<p> extends true ? [] : Length<path> extends 5 ? [] : IsUnion<a> extends true ? [
|
|
49
|
+
{
|
|
50
|
+
cases: a extends any ? {
|
|
51
|
+
value: a;
|
|
52
|
+
subUnions: FindUnionsMany<a, p, path>;
|
|
53
|
+
} : never;
|
|
54
|
+
path: path;
|
|
55
|
+
}
|
|
56
|
+
] : [a, p] extends [readonly any[], readonly any[]] ? [a, p] extends [
|
|
57
|
+
readonly [infer a1, infer a2, infer a3, infer a4, infer a5],
|
|
58
|
+
readonly [infer p1, infer p2, infer p3, infer p4, infer p5]
|
|
59
|
+
] ? [
|
|
60
|
+
...FindUnions<a1, p1, [...path, 0]>,
|
|
61
|
+
...FindUnions<a2, p2, [...path, 1]>,
|
|
62
|
+
...FindUnions<a3, p3, [...path, 2]>,
|
|
63
|
+
...FindUnions<a4, p4, [...path, 3]>,
|
|
64
|
+
...FindUnions<a5, p5, [...path, 4]>
|
|
65
|
+
] : [a, p] extends [
|
|
66
|
+
readonly [infer a1, infer a2, infer a3, infer a4],
|
|
67
|
+
readonly [infer p1, infer p2, infer p3, infer p4]
|
|
68
|
+
] ? [
|
|
69
|
+
...FindUnions<a1, p1, [...path, 0]>,
|
|
70
|
+
...FindUnions<a2, p2, [...path, 1]>,
|
|
71
|
+
...FindUnions<a3, p3, [...path, 2]>,
|
|
72
|
+
...FindUnions<a4, p4, [...path, 3]>
|
|
73
|
+
] : [a, p] extends [
|
|
74
|
+
readonly [infer a1, infer a2, infer a3],
|
|
75
|
+
readonly [infer p1, infer p2, infer p3]
|
|
76
|
+
] ? [
|
|
77
|
+
...FindUnions<a1, p1, [...path, 0]>,
|
|
78
|
+
...FindUnions<a2, p2, [...path, 1]>,
|
|
79
|
+
...FindUnions<a3, p3, [...path, 2]>
|
|
80
|
+
] : [a, p] extends [
|
|
81
|
+
readonly [infer a1, infer a2],
|
|
82
|
+
readonly [infer p1, infer p2]
|
|
83
|
+
] ? [...FindUnions<a1, p1, [...path, 0]>, ...FindUnions<a2, p2, [...path, 1]>] : [a, p] extends [readonly [infer a1], readonly [infer p1]] ? FindUnions<a1, p1, [...path, 0]> : p extends readonly [] | readonly [any, ...any] | readonly [...any, any] ? IsStrictArray<Extract<a, readonly any[]>> extends false ? [] : [
|
|
84
|
+
ArrayToVariadicUnion<a, p> extends infer aUnion ? {
|
|
85
|
+
cases: aUnion extends any ? {
|
|
86
|
+
value: aUnion;
|
|
87
|
+
subUnions: [];
|
|
88
|
+
} : never;
|
|
89
|
+
path: path;
|
|
90
|
+
} : never
|
|
91
|
+
] : [] : a extends Set<any> ? [] : a extends Map<any, any> ? [] : [IsPlainObject<a>, IsPlainObject<p>] extends [true, true] ? Flatten<Values<{
|
|
92
|
+
[k in keyof a & keyof p]: FindUnions<a[k], p[k], [...path, k]>;
|
|
93
|
+
}>> : [];
|
|
94
|
+
export type ArrayToVariadicUnion<input, excluded> = MaybeAddReadonly<(input extends readonly [any, ...any] | readonly [...any, any] ? never : []) | (excluded extends readonly [...any, any] ? [...Extract<input, readonly any[]>, ValueOf<input>] : [ValueOf<input>, ...Extract<input, readonly any[]>]), IsReadonlyArray<input>>;
|
|
95
|
+
export type Distribute<unions extends readonly any[]> = unions extends readonly [
|
|
96
|
+
{
|
|
97
|
+
cases: infer cases;
|
|
98
|
+
path: infer path;
|
|
99
|
+
},
|
|
100
|
+
...infer tail
|
|
101
|
+
] ? cases extends {
|
|
102
|
+
value: infer value;
|
|
103
|
+
subUnions: infer subUnions;
|
|
104
|
+
} ? [
|
|
105
|
+
[
|
|
106
|
+
value,
|
|
107
|
+
path
|
|
108
|
+
],
|
|
109
|
+
...Distribute<Extract<subUnions, readonly any[]>>,
|
|
110
|
+
...Distribute<tail>
|
|
111
|
+
] : never : [];
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { BuildMany } from './BuildMany.js';
|
|
2
|
+
import type { IsAny, Values, Flatten, IsUnion, IsPlainObject, Length, UnionToTuple, IsReadonlyArray, ValueOf, MaybeAddReadonly, IsStrictArray } from './helpers.js';
|
|
3
|
+
import { IsMatching } from './IsMatching.js';
|
|
4
|
+
/**
|
|
5
|
+
* DistributeMatchingUnions takes two arguments:
|
|
6
|
+
* - a data structure of type `a` containing unions
|
|
7
|
+
* - a pattern `p`, matching this data structure
|
|
8
|
+
* and turns it into a union of all possible
|
|
9
|
+
* combination of each unions contained in `a` that matches `p`.
|
|
10
|
+
*
|
|
11
|
+
* It does this in 3 main steps:
|
|
12
|
+
* - 1. Find all unions contained in the data structure, that matches `p`
|
|
13
|
+
* with `FindUnions<a, p>`. It returns a tree of [union, path] pairs.
|
|
14
|
+
* - 2. this tree is passed to the `Distribute` type level function,
|
|
15
|
+
* Which turns it into a union of list of `[singleValue, path]` pairs.
|
|
16
|
+
* Each list correspond to one of the possible combination of the unions
|
|
17
|
+
* found in `a`.
|
|
18
|
+
* - 3. build a data structure with the same shape as `a` for each combination
|
|
19
|
+
* and return the union of these data structures.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* type t1 = DistributeMatchingUnions<['a' | 'b', 1 | 2], ['a', 1]>;
|
|
23
|
+
* // => ['a', 1] | ['a', 2] | ['b', 1] | ['b', 2]
|
|
24
|
+
*
|
|
25
|
+
* type t2 = DistributeMatchingUnions<['a' | 'b', 1 | 2], ['a', unknown]>;
|
|
26
|
+
* // => ['a', 1 | 2] | ['b', 1 | 2]
|
|
27
|
+
*/
|
|
28
|
+
export type DistributeMatchingUnions<a, p> = IsAny<a> extends true ? any : BuildMany<a, Distribute<FindUnionsMany<a, p>>>;
|
|
29
|
+
export type FindUnionsMany<a, p, path extends PropertyKey[] = []> = UnionToTuple<(p extends any ? IsMatching<a, p> extends true ? FindUnions<a, p, path> : [] : never) extends readonly (infer T)[] ? T : never>;
|
|
30
|
+
/**
|
|
31
|
+
* The reason we don't look further down the tree with lists,
|
|
32
|
+
* Set and Maps is that they can be heterogeneous,
|
|
33
|
+
* so matching on a A[] for a in input of (A|B)[]
|
|
34
|
+
* doesn't rule anything out. You can still have
|
|
35
|
+
* a (A|B)[] afterward. The same logic goes for Set and Maps.
|
|
36
|
+
*
|
|
37
|
+
* Kinds are types of types.
|
|
38
|
+
*
|
|
39
|
+
* kind UnionConfig = {
|
|
40
|
+
* cases: Union<{
|
|
41
|
+
* value: b,
|
|
42
|
+
* subUnions: UnionConfig[]
|
|
43
|
+
* }>,
|
|
44
|
+
* path: string[]
|
|
45
|
+
* }
|
|
46
|
+
* FindUnions :: Pattern a p => a -> p -> UnionConfig[]
|
|
47
|
+
*/
|
|
48
|
+
export type FindUnions<a, p, path extends PropertyKey[] = []> = unknown extends p ? [] : IsAny<p> extends true ? [] : Length<path> extends 5 ? [] : IsUnion<a> extends true ? [
|
|
49
|
+
{
|
|
50
|
+
cases: a extends any ? {
|
|
51
|
+
value: a;
|
|
52
|
+
subUnions: FindUnionsMany<a, p, path>;
|
|
53
|
+
} : never;
|
|
54
|
+
path: path;
|
|
55
|
+
}
|
|
56
|
+
] : [a, p] extends [readonly any[], readonly any[]] ? [a, p] extends [
|
|
57
|
+
readonly [infer a1, infer a2, infer a3, infer a4, infer a5],
|
|
58
|
+
readonly [infer p1, infer p2, infer p3, infer p4, infer p5]
|
|
59
|
+
] ? [
|
|
60
|
+
...FindUnions<a1, p1, [...path, 0]>,
|
|
61
|
+
...FindUnions<a2, p2, [...path, 1]>,
|
|
62
|
+
...FindUnions<a3, p3, [...path, 2]>,
|
|
63
|
+
...FindUnions<a4, p4, [...path, 3]>,
|
|
64
|
+
...FindUnions<a5, p5, [...path, 4]>
|
|
65
|
+
] : [a, p] extends [
|
|
66
|
+
readonly [infer a1, infer a2, infer a3, infer a4],
|
|
67
|
+
readonly [infer p1, infer p2, infer p3, infer p4]
|
|
68
|
+
] ? [
|
|
69
|
+
...FindUnions<a1, p1, [...path, 0]>,
|
|
70
|
+
...FindUnions<a2, p2, [...path, 1]>,
|
|
71
|
+
...FindUnions<a3, p3, [...path, 2]>,
|
|
72
|
+
...FindUnions<a4, p4, [...path, 3]>
|
|
73
|
+
] : [a, p] extends [
|
|
74
|
+
readonly [infer a1, infer a2, infer a3],
|
|
75
|
+
readonly [infer p1, infer p2, infer p3]
|
|
76
|
+
] ? [
|
|
77
|
+
...FindUnions<a1, p1, [...path, 0]>,
|
|
78
|
+
...FindUnions<a2, p2, [...path, 1]>,
|
|
79
|
+
...FindUnions<a3, p3, [...path, 2]>
|
|
80
|
+
] : [a, p] extends [
|
|
81
|
+
readonly [infer a1, infer a2],
|
|
82
|
+
readonly [infer p1, infer p2]
|
|
83
|
+
] ? [...FindUnions<a1, p1, [...path, 0]>, ...FindUnions<a2, p2, [...path, 1]>] : [a, p] extends [readonly [infer a1], readonly [infer p1]] ? FindUnions<a1, p1, [...path, 0]> : p extends readonly [] | readonly [any, ...any] | readonly [...any, any] ? IsStrictArray<Extract<a, readonly any[]>> extends false ? [] : [
|
|
84
|
+
ArrayToVariadicUnion<a, p> extends infer aUnion ? {
|
|
85
|
+
cases: aUnion extends any ? {
|
|
86
|
+
value: aUnion;
|
|
87
|
+
subUnions: [];
|
|
88
|
+
} : never;
|
|
89
|
+
path: path;
|
|
90
|
+
} : never
|
|
91
|
+
] : [] : a extends Set<any> ? [] : a extends Map<any, any> ? [] : [IsPlainObject<a>, IsPlainObject<p>] extends [true, true] ? Flatten<Values<{
|
|
92
|
+
[k in keyof a & keyof p]: FindUnions<a[k], p[k], [...path, k]>;
|
|
93
|
+
}>> : [];
|
|
94
|
+
export type ArrayToVariadicUnion<input, excluded> = MaybeAddReadonly<(input extends readonly [any, ...any] | readonly [...any, any] ? never : []) | (excluded extends readonly [...any, any] ? [...Extract<input, readonly any[]>, ValueOf<input>] : [ValueOf<input>, ...Extract<input, readonly any[]>]), IsReadonlyArray<input>>;
|
|
95
|
+
export type Distribute<unions extends readonly any[]> = unions extends readonly [
|
|
96
|
+
{
|
|
97
|
+
cases: infer cases;
|
|
98
|
+
path: infer path;
|
|
99
|
+
},
|
|
100
|
+
...infer tail
|
|
101
|
+
] ? cases extends {
|
|
102
|
+
value: infer value;
|
|
103
|
+
subUnions: infer subUnions;
|
|
104
|
+
} ? [
|
|
105
|
+
[
|
|
106
|
+
value,
|
|
107
|
+
path
|
|
108
|
+
],
|
|
109
|
+
...Distribute<Extract<subUnions, readonly any[]>>,
|
|
110
|
+
...Distribute<tail>
|
|
111
|
+
] : never : [];
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Override } from './Pattern.cjs';
|
|
2
|
+
import type { BuiltInObjects, Compute, Contains, IsPlainObject, IsReadonlyArray, LeastUpperBound, MaybeAddReadonly, ValueOf } from './helpers.cjs';
|
|
3
|
+
export type ExtractPreciseValue<a, b> = b extends Override<infer b1> ? b1 : unknown extends b ? a : 0 extends 1 & b ? a : 0 extends 1 & a ? b : b extends readonly any[] ? ExtractPreciseArrayValue<a, b, IsReadonlyArray<a>> : b extends Map<infer bk, infer bv> ? a extends Map<infer ak, infer av> ? Map<ExtractPreciseValue<ak, bk>, ExtractPreciseValue<av, bv>> : LeastUpperBound<a, b> : b extends Set<infer bv> ? a extends Set<infer av> ? Set<ExtractPreciseValue<av, bv>> : LeastUpperBound<a, b> : IsPlainObject<b, BuiltInObjects | Error> extends true ? a extends object ? a extends b ? a : b extends a ? Contains<b, never> extends true ? never : Contains<Omit<b, keyof a>, {}> extends true ? never : [
|
|
4
|
+
Exclude<keyof a, keyof b>
|
|
5
|
+
] extends [never] ? b : Compute<b & Omit<a, keyof b>> : [keyof a & keyof b] extends [never] ? never : Compute<{
|
|
6
|
+
[k in keyof a as k extends keyof b ? never : k]: a[k];
|
|
7
|
+
} & {
|
|
8
|
+
[k in keyof b]: k extends keyof a ? ExtractPreciseValue<a[k], b[k]> : b[k];
|
|
9
|
+
}> extends infer result ? Contains<Pick<result, keyof result & keyof b>, never> extends true ? never : result : never : LeastUpperBound<a, b> : LeastUpperBound<a, b>;
|
|
10
|
+
type ExtractPreciseArrayValue<a, b, isReadonly extends boolean, startOutput extends any[] = [], endOutput extends any[] = []> = a extends readonly (infer aItem)[] ? b extends readonly [] ? MaybeAddReadonly<[...startOutput, ...endOutput], isReadonly> : b extends readonly [infer b1, ...infer bRest] ? a extends readonly [infer a1, ...infer aRest] ? ExtractPreciseValue<a1, b1> extends infer currentValue ? [currentValue] extends [never] ? never : ExtractPreciseArrayValue<aRest, bRest, isReadonly, [
|
|
11
|
+
...startOutput,
|
|
12
|
+
currentValue
|
|
13
|
+
], endOutput> : never : ExtractPreciseValue<aItem, b1> extends infer currentValue ? [currentValue] extends [never] ? never : ExtractPreciseArrayValue<aItem[], bRest, isReadonly, [
|
|
14
|
+
...startOutput,
|
|
15
|
+
currentValue
|
|
16
|
+
], endOutput> : never : b extends readonly [...infer bInit, infer b1] ? a extends readonly [...infer aInit, infer a1] ? ExtractPreciseValue<a1, b1> extends infer currentValue ? [currentValue] extends [never] ? never : ExtractPreciseArrayValue<aInit, bInit, isReadonly, startOutput, [
|
|
17
|
+
...endOutput,
|
|
18
|
+
currentValue
|
|
19
|
+
]> : never : ExtractPreciseValue<aItem, b1> extends infer currentValue ? [currentValue] extends [never] ? never : ExtractPreciseArrayValue<aItem[], bInit, isReadonly, startOutput, [
|
|
20
|
+
...endOutput,
|
|
21
|
+
currentValue
|
|
22
|
+
]> : never : ExtractPreciseValue<aItem, ValueOf<b>> extends infer currentValue ? [currentValue] extends [never] ? never : MaybeAddReadonly<[
|
|
23
|
+
...startOutput,
|
|
24
|
+
...currentValue[],
|
|
25
|
+
...endOutput
|
|
26
|
+
], isReadonly> : never : LeastUpperBound<a, b>;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Override } from './Pattern.js';
|
|
2
|
+
import type { BuiltInObjects, Compute, Contains, IsPlainObject, IsReadonlyArray, LeastUpperBound, MaybeAddReadonly, ValueOf } from './helpers.js';
|
|
3
|
+
export type ExtractPreciseValue<a, b> = b extends Override<infer b1> ? b1 : unknown extends b ? a : 0 extends 1 & b ? a : 0 extends 1 & a ? b : b extends readonly any[] ? ExtractPreciseArrayValue<a, b, IsReadonlyArray<a>> : b extends Map<infer bk, infer bv> ? a extends Map<infer ak, infer av> ? Map<ExtractPreciseValue<ak, bk>, ExtractPreciseValue<av, bv>> : LeastUpperBound<a, b> : b extends Set<infer bv> ? a extends Set<infer av> ? Set<ExtractPreciseValue<av, bv>> : LeastUpperBound<a, b> : IsPlainObject<b, BuiltInObjects | Error> extends true ? a extends object ? a extends b ? a : b extends a ? Contains<b, never> extends true ? never : Contains<Omit<b, keyof a>, {}> extends true ? never : [
|
|
4
|
+
Exclude<keyof a, keyof b>
|
|
5
|
+
] extends [never] ? b : Compute<b & Omit<a, keyof b>> : [keyof a & keyof b] extends [never] ? never : Compute<{
|
|
6
|
+
[k in keyof a as k extends keyof b ? never : k]: a[k];
|
|
7
|
+
} & {
|
|
8
|
+
[k in keyof b]: k extends keyof a ? ExtractPreciseValue<a[k], b[k]> : b[k];
|
|
9
|
+
}> extends infer result ? Contains<Pick<result, keyof result & keyof b>, never> extends true ? never : result : never : LeastUpperBound<a, b> : LeastUpperBound<a, b>;
|
|
10
|
+
type ExtractPreciseArrayValue<a, b, isReadonly extends boolean, startOutput extends any[] = [], endOutput extends any[] = []> = a extends readonly (infer aItem)[] ? b extends readonly [] ? MaybeAddReadonly<[...startOutput, ...endOutput], isReadonly> : b extends readonly [infer b1, ...infer bRest] ? a extends readonly [infer a1, ...infer aRest] ? ExtractPreciseValue<a1, b1> extends infer currentValue ? [currentValue] extends [never] ? never : ExtractPreciseArrayValue<aRest, bRest, isReadonly, [
|
|
11
|
+
...startOutput,
|
|
12
|
+
currentValue
|
|
13
|
+
], endOutput> : never : ExtractPreciseValue<aItem, b1> extends infer currentValue ? [currentValue] extends [never] ? never : ExtractPreciseArrayValue<aItem[], bRest, isReadonly, [
|
|
14
|
+
...startOutput,
|
|
15
|
+
currentValue
|
|
16
|
+
], endOutput> : never : b extends readonly [...infer bInit, infer b1] ? a extends readonly [...infer aInit, infer a1] ? ExtractPreciseValue<a1, b1> extends infer currentValue ? [currentValue] extends [never] ? never : ExtractPreciseArrayValue<aInit, bInit, isReadonly, startOutput, [
|
|
17
|
+
...endOutput,
|
|
18
|
+
currentValue
|
|
19
|
+
]> : never : ExtractPreciseValue<aItem, b1> extends infer currentValue ? [currentValue] extends [never] ? never : ExtractPreciseArrayValue<aItem[], bInit, isReadonly, startOutput, [
|
|
20
|
+
...endOutput,
|
|
21
|
+
currentValue
|
|
22
|
+
]> : never : ExtractPreciseValue<aItem, ValueOf<b>> extends infer currentValue ? [currentValue] extends [never] ? never : MaybeAddReadonly<[
|
|
23
|
+
...startOutput,
|
|
24
|
+
...currentValue[],
|
|
25
|
+
...endOutput
|
|
26
|
+
], isReadonly> : never : LeastUpperBound<a, b>;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type * as symbols from '../internals/symbols.cjs';
|
|
2
|
+
import type { AnyMatcher, Matcher, Pattern } from './Pattern.cjs';
|
|
3
|
+
import type { Equal, Primitives, ValueOf, MergeUnion, IsUnion } from './helpers.cjs';
|
|
4
|
+
type SelectionsRecord = Record<string, [unknown, unknown[]]>;
|
|
5
|
+
export type None = {
|
|
6
|
+
type: 'none';
|
|
7
|
+
};
|
|
8
|
+
export type Some<key extends string> = {
|
|
9
|
+
type: 'some';
|
|
10
|
+
key: key;
|
|
11
|
+
};
|
|
12
|
+
export type SelectionType = None | Some<string>;
|
|
13
|
+
type MapOptional<selections> = {
|
|
14
|
+
[k in keyof selections]: selections[k] extends [infer v, infer subpath] ? [v | undefined, subpath] : never;
|
|
15
|
+
};
|
|
16
|
+
type MapList<selections> = {
|
|
17
|
+
[k in keyof selections]: selections[k] extends [infer v, infer subpath] ? [v[], subpath] : never;
|
|
18
|
+
};
|
|
19
|
+
type ReduceFindSelectionUnion<i, ps extends readonly any[], output = never> = ps extends readonly [infer head, ...infer tail] ? ReduceFindSelectionUnion<i, tail, output | FindSelectionUnion<i, head>> : output;
|
|
20
|
+
type FindSelectionUnionInArray<i, p, path extends any[] = [], output = never> = i extends readonly (infer iItem)[] ? p extends readonly [] ? output : p extends readonly [infer p1, ...infer pRest] ? i extends readonly [infer i1, ...infer iRest] ? FindSelectionUnionInArray<iRest, pRest, [
|
|
21
|
+
...path,
|
|
22
|
+
p['length']
|
|
23
|
+
], output | FindSelectionUnion<i1, p1, [...path, p['length']]>> : FindSelectionUnionInArray<iItem[], pRest, [
|
|
24
|
+
...path,
|
|
25
|
+
p['length']
|
|
26
|
+
], output | FindSelectionUnion<iItem, p1, [...path, p['length']]>> : p extends readonly [...infer pInit, infer p1] ? i extends readonly [...infer iInit, infer i1] ? FindSelectionUnionInArray<iInit, pInit, [
|
|
27
|
+
...path,
|
|
28
|
+
p['length']
|
|
29
|
+
], output | FindSelectionUnion<i1, p1, [...path, p['length']]>> : FindSelectionUnionInArray<iItem[], pInit, [
|
|
30
|
+
...path,
|
|
31
|
+
p['length']
|
|
32
|
+
], output | FindSelectionUnion<iItem, p1, [...path, p['length']]>> : p extends readonly [...(readonly (infer pRest & AnyMatcher)[])] ? output | FindSelectionUnion<i, pRest, [...path, p['length']]> : output | FindSelectionUnion<iItem, ValueOf<p>, [
|
|
33
|
+
...path,
|
|
34
|
+
Extract<p, readonly any[]>['length']
|
|
35
|
+
]> : output;
|
|
36
|
+
export type FindSelectionUnion<i, p, path extends any[] = []> = 0 extends 1 & i ? never : 0 extends 1 & p ? never : p extends Primitives ? never : p extends Matcher<any, infer pattern, infer matcherType, infer sel> ? {
|
|
37
|
+
select: sel extends Some<infer k> ? {
|
|
38
|
+
[kk in k]: [i, path];
|
|
39
|
+
} | FindSelectionUnion<i, pattern, path> : never;
|
|
40
|
+
array: i extends readonly (infer iItem)[] ? MapList<FindSelectionUnion<iItem, pattern>> : never;
|
|
41
|
+
record: [i, pattern] extends [
|
|
42
|
+
Record<infer k, infer v>,
|
|
43
|
+
[
|
|
44
|
+
infer pkey,
|
|
45
|
+
infer pvalue
|
|
46
|
+
]
|
|
47
|
+
] ? MapList<FindSelectionUnion<k, pkey, path>> | MapList<FindSelectionUnion<v, pvalue, path>> : never;
|
|
48
|
+
map: [i, pattern] extends [
|
|
49
|
+
Map<infer k, infer v>,
|
|
50
|
+
[
|
|
51
|
+
infer pkey,
|
|
52
|
+
infer pvalue
|
|
53
|
+
]
|
|
54
|
+
] ? MapList<FindSelectionUnion<k, pkey, path>> | MapList<FindSelectionUnion<v, pvalue, path>> : never;
|
|
55
|
+
set: i extends Set<infer v> ? MapList<FindSelectionUnion<v, pattern, path>> : never;
|
|
56
|
+
optional: MapOptional<FindSelectionUnion<i, pattern>>;
|
|
57
|
+
or: MapOptional<ReduceFindSelectionUnion<i, Extract<pattern, readonly any[]>>>;
|
|
58
|
+
and: ReduceFindSelectionUnion<i, Extract<pattern, readonly any[]>>;
|
|
59
|
+
not: never;
|
|
60
|
+
default: sel extends Some<infer k> ? {
|
|
61
|
+
[kk in k]: [i, path];
|
|
62
|
+
} : never;
|
|
63
|
+
custom: never;
|
|
64
|
+
}[matcherType] : p extends readonly any[] ? FindSelectionUnionInArray<i, p> : p extends {} ? i extends {} ? {
|
|
65
|
+
[k in keyof p]: k extends keyof i ? FindSelectionUnion<i[k], p[k], [...path, k]> : never;
|
|
66
|
+
}[keyof p] : never : never;
|
|
67
|
+
export type SeveralAnonymousSelectError<a = 'You can only use a single anonymous selection (with `select()`) in your pattern. If you need to select multiple values, give them names with `select(<name>)` instead'> = {
|
|
68
|
+
__error: never;
|
|
69
|
+
} & a;
|
|
70
|
+
export type MixedNamedAndAnonymousSelectError<a = 'Mixing named selections (`select("name")`) and anonymous selections (`select()`) is forbiden. Please, only use named selections.'> = {
|
|
71
|
+
__error: never;
|
|
72
|
+
} & a;
|
|
73
|
+
export type SelectionToArgs<selections extends SelectionsRecord> = symbols.anonymousSelectKey extends keyof selections ? IsUnion<selections[symbols.anonymousSelectKey][1]> extends true ? SeveralAnonymousSelectError : keyof selections extends symbols.anonymousSelectKey ? selections[symbols.anonymousSelectKey][0] : MixedNamedAndAnonymousSelectError : {
|
|
74
|
+
[k in keyof selections]: selections[k][0];
|
|
75
|
+
};
|
|
76
|
+
export type Selections<i, p> = FindSelectionUnion<i, p> extends infer u ? [u] extends [never] ? i : SelectionToArgs<Extract<MergeUnion<u>, SelectionsRecord>> : i;
|
|
77
|
+
export type FindSelected<i, p> = Equal<p, Pattern<i>> extends true ? i : Selections<i, p>;
|
|
78
|
+
export {};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type * as symbols from '../internals/symbols.js';
|
|
2
|
+
import type { AnyMatcher, Matcher, Pattern } from './Pattern.js';
|
|
3
|
+
import type { Equal, Primitives, ValueOf, MergeUnion, IsUnion } from './helpers.js';
|
|
4
|
+
type SelectionsRecord = Record<string, [unknown, unknown[]]>;
|
|
5
|
+
export type None = {
|
|
6
|
+
type: 'none';
|
|
7
|
+
};
|
|
8
|
+
export type Some<key extends string> = {
|
|
9
|
+
type: 'some';
|
|
10
|
+
key: key;
|
|
11
|
+
};
|
|
12
|
+
export type SelectionType = None | Some<string>;
|
|
13
|
+
type MapOptional<selections> = {
|
|
14
|
+
[k in keyof selections]: selections[k] extends [infer v, infer subpath] ? [v | undefined, subpath] : never;
|
|
15
|
+
};
|
|
16
|
+
type MapList<selections> = {
|
|
17
|
+
[k in keyof selections]: selections[k] extends [infer v, infer subpath] ? [v[], subpath] : never;
|
|
18
|
+
};
|
|
19
|
+
type ReduceFindSelectionUnion<i, ps extends readonly any[], output = never> = ps extends readonly [infer head, ...infer tail] ? ReduceFindSelectionUnion<i, tail, output | FindSelectionUnion<i, head>> : output;
|
|
20
|
+
type FindSelectionUnionInArray<i, p, path extends any[] = [], output = never> = i extends readonly (infer iItem)[] ? p extends readonly [] ? output : p extends readonly [infer p1, ...infer pRest] ? i extends readonly [infer i1, ...infer iRest] ? FindSelectionUnionInArray<iRest, pRest, [
|
|
21
|
+
...path,
|
|
22
|
+
p['length']
|
|
23
|
+
], output | FindSelectionUnion<i1, p1, [...path, p['length']]>> : FindSelectionUnionInArray<iItem[], pRest, [
|
|
24
|
+
...path,
|
|
25
|
+
p['length']
|
|
26
|
+
], output | FindSelectionUnion<iItem, p1, [...path, p['length']]>> : p extends readonly [...infer pInit, infer p1] ? i extends readonly [...infer iInit, infer i1] ? FindSelectionUnionInArray<iInit, pInit, [
|
|
27
|
+
...path,
|
|
28
|
+
p['length']
|
|
29
|
+
], output | FindSelectionUnion<i1, p1, [...path, p['length']]>> : FindSelectionUnionInArray<iItem[], pInit, [
|
|
30
|
+
...path,
|
|
31
|
+
p['length']
|
|
32
|
+
], output | FindSelectionUnion<iItem, p1, [...path, p['length']]>> : p extends readonly [...(readonly (infer pRest & AnyMatcher)[])] ? output | FindSelectionUnion<i, pRest, [...path, p['length']]> : output | FindSelectionUnion<iItem, ValueOf<p>, [
|
|
33
|
+
...path,
|
|
34
|
+
Extract<p, readonly any[]>['length']
|
|
35
|
+
]> : output;
|
|
36
|
+
export type FindSelectionUnion<i, p, path extends any[] = []> = 0 extends 1 & i ? never : 0 extends 1 & p ? never : p extends Primitives ? never : p extends Matcher<any, infer pattern, infer matcherType, infer sel> ? {
|
|
37
|
+
select: sel extends Some<infer k> ? {
|
|
38
|
+
[kk in k]: [i, path];
|
|
39
|
+
} | FindSelectionUnion<i, pattern, path> : never;
|
|
40
|
+
array: i extends readonly (infer iItem)[] ? MapList<FindSelectionUnion<iItem, pattern>> : never;
|
|
41
|
+
record: [i, pattern] extends [
|
|
42
|
+
Record<infer k, infer v>,
|
|
43
|
+
[
|
|
44
|
+
infer pkey,
|
|
45
|
+
infer pvalue
|
|
46
|
+
]
|
|
47
|
+
] ? MapList<FindSelectionUnion<k, pkey, path>> | MapList<FindSelectionUnion<v, pvalue, path>> : never;
|
|
48
|
+
map: [i, pattern] extends [
|
|
49
|
+
Map<infer k, infer v>,
|
|
50
|
+
[
|
|
51
|
+
infer pkey,
|
|
52
|
+
infer pvalue
|
|
53
|
+
]
|
|
54
|
+
] ? MapList<FindSelectionUnion<k, pkey, path>> | MapList<FindSelectionUnion<v, pvalue, path>> : never;
|
|
55
|
+
set: i extends Set<infer v> ? MapList<FindSelectionUnion<v, pattern, path>> : never;
|
|
56
|
+
optional: MapOptional<FindSelectionUnion<i, pattern>>;
|
|
57
|
+
or: MapOptional<ReduceFindSelectionUnion<i, Extract<pattern, readonly any[]>>>;
|
|
58
|
+
and: ReduceFindSelectionUnion<i, Extract<pattern, readonly any[]>>;
|
|
59
|
+
not: never;
|
|
60
|
+
default: sel extends Some<infer k> ? {
|
|
61
|
+
[kk in k]: [i, path];
|
|
62
|
+
} : never;
|
|
63
|
+
custom: never;
|
|
64
|
+
}[matcherType] : p extends readonly any[] ? FindSelectionUnionInArray<i, p> : p extends {} ? i extends {} ? {
|
|
65
|
+
[k in keyof p]: k extends keyof i ? FindSelectionUnion<i[k], p[k], [...path, k]> : never;
|
|
66
|
+
}[keyof p] : never : never;
|
|
67
|
+
export type SeveralAnonymousSelectError<a = 'You can only use a single anonymous selection (with `select()`) in your pattern. If you need to select multiple values, give them names with `select(<name>)` instead'> = {
|
|
68
|
+
__error: never;
|
|
69
|
+
} & a;
|
|
70
|
+
export type MixedNamedAndAnonymousSelectError<a = 'Mixing named selections (`select("name")`) and anonymous selections (`select()`) is forbiden. Please, only use named selections.'> = {
|
|
71
|
+
__error: never;
|
|
72
|
+
} & a;
|
|
73
|
+
export type SelectionToArgs<selections extends SelectionsRecord> = symbols.anonymousSelectKey extends keyof selections ? IsUnion<selections[symbols.anonymousSelectKey][1]> extends true ? SeveralAnonymousSelectError : keyof selections extends symbols.anonymousSelectKey ? selections[symbols.anonymousSelectKey][0] : MixedNamedAndAnonymousSelectError : {
|
|
74
|
+
[k in keyof selections]: selections[k][0];
|
|
75
|
+
};
|
|
76
|
+
export type Selections<i, p> = FindSelectionUnion<i, p> extends infer u ? [u] extends [never] ? i : SelectionToArgs<Extract<MergeUnion<u>, SelectionsRecord>> : i;
|
|
77
|
+
export type FindSelected<i, p> = Equal<p, Pattern<i>> extends true ? i : Selections<i, p>;
|
|
78
|
+
export {};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { DeepExclude } from './DeepExclude.cjs';
|
|
2
|
+
import { IsPlainObject, Primitives, IsLiteral, ValueOf, Compute, Equal, Extends, Not, All, NonLiteralPrimitive, MaybeAddReadonly, IsReadonlyArray, MapKey, MapValue, SetValue, ExtractPlainObject, GetKey, Call, Fn, ReadonlyArrayValue, WithDefault, RecordKey, RecordValue } from './helpers.cjs';
|
|
3
|
+
import type { Matcher, Pattern, Override, AnyMatcher } from './Pattern.cjs';
|
|
4
|
+
type OptionalKeys<p> = ValueOf<{
|
|
5
|
+
[k in keyof p]: 0 extends 1 & p[k] ? never : p[k] extends Matcher<any, any, infer matcherType> ? matcherType extends 'optional' ? k : never : never;
|
|
6
|
+
}>;
|
|
7
|
+
type ReduceUnion<tuple extends readonly any[], i, output = never> = tuple extends readonly [infer p, ...infer tail] ? ReduceUnion<tail, i, output | InvertPatternInternal<p, i>> : output;
|
|
8
|
+
type ReduceIntersection<tuple extends readonly any[], i, output = unknown> = tuple extends readonly [infer p, ...infer tail] ? ReduceIntersection<tail, i, output & InvertPatternInternal<p, i>> : output;
|
|
9
|
+
type InvertArrayPattern<p, i, startOutput extends any[] = [], endOutput extends any[] = []> = i extends readonly (infer ii)[] ? p extends readonly [] ? [...startOutput, ...endOutput] : p extends readonly [infer p1, ...infer pRest] ? i extends readonly [infer i1, ...infer iRest] ? InvertArrayPattern<pRest, iRest, [
|
|
10
|
+
...startOutput,
|
|
11
|
+
InvertPatternInternal<p1, i1>
|
|
12
|
+
], endOutput> : InvertArrayPattern<pRest, ii[], [
|
|
13
|
+
...startOutput,
|
|
14
|
+
InvertPatternInternal<p1, ii>
|
|
15
|
+
], endOutput> : p extends readonly [...infer pInit, infer p1] ? i extends readonly [...infer iInit, infer i1] ? InvertArrayPattern<pInit, iInit, startOutput, [
|
|
16
|
+
...endOutput,
|
|
17
|
+
InvertPatternInternal<p1, i1>
|
|
18
|
+
]> : InvertArrayPattern<pInit, ii[], startOutput, [
|
|
19
|
+
...endOutput,
|
|
20
|
+
InvertPatternInternal<p1, ii>
|
|
21
|
+
]> : p extends readonly [...(readonly (infer pRest & AnyMatcher)[])] ? [
|
|
22
|
+
...startOutput,
|
|
23
|
+
...Extract<InvertPatternInternal<pRest, i>, readonly any[]>,
|
|
24
|
+
...endOutput
|
|
25
|
+
] : [...startOutput, ...InvertPatternInternal<ValueOf<p>, ii>[], ...endOutput] : never;
|
|
26
|
+
/**
|
|
27
|
+
* ### InvertPatternInternal
|
|
28
|
+
* Since patterns have special wildcard values, we need a way
|
|
29
|
+
* to transform a pattern into the type of value it represents
|
|
30
|
+
*/
|
|
31
|
+
export type InvertPattern<p, input> = Equal<Pattern<input>, p> extends true ? never : InvertPatternInternal<p, input>;
|
|
32
|
+
type InvertPatternInternal<p, input> = 0 extends 1 & p ? never : p extends Matcher<infer _input, infer subpattern, infer matcherType, any, infer narrowedOrFn> ? {
|
|
33
|
+
not: DeepExclude<input, InvertPatternInternal<subpattern, input>>;
|
|
34
|
+
select: InvertPatternInternal<subpattern, input>;
|
|
35
|
+
array: InvertPatternInternal<subpattern, ReadonlyArrayValue<input>>[];
|
|
36
|
+
record: subpattern extends [infer pk, infer pv] ? Record<Extract<InvertPatternInternal<pk, RecordKey<input>>, PropertyKey>, InvertPatternInternal<pv, RecordValue<input>>> : never;
|
|
37
|
+
map: subpattern extends [infer pk, infer pv] ? Map<InvertPatternInternal<pk, MapKey<Extract<input, Map<any, any>>>>, InvertPatternInternal<pv, MapValue<Extract<input, Map<any, any>>>>> : never;
|
|
38
|
+
set: Set<InvertPatternInternal<subpattern, SetValue<Extract<input, Set<any>>>>>;
|
|
39
|
+
optional: InvertPatternInternal<subpattern, Exclude<input, undefined>> | undefined;
|
|
40
|
+
and: ReduceIntersection<Extract<subpattern, readonly any[]>, input>;
|
|
41
|
+
or: ReduceUnion<Extract<subpattern, readonly any[]>, input>;
|
|
42
|
+
default: [subpattern] extends [never] ? input : subpattern;
|
|
43
|
+
custom: Override<narrowedOrFn extends Fn ? Call<narrowedOrFn, input> : narrowedOrFn>;
|
|
44
|
+
}[matcherType] : p extends Primitives ? p : p extends readonly any[] ? InvertArrayPattern<p, WithDefault<Extract<input, readonly any[]>, unknown[]>> : IsPlainObject<p> extends true ? OptionalKeys<p> extends infer optKeys ? [optKeys] extends [never] ? {
|
|
45
|
+
[k in Exclude<keyof p, optKeys>]: InvertPatternInternal<p[k], WithDefault<GetKey<ExtractPlainObject<input>, k>, unknown>>;
|
|
46
|
+
} : Compute<{
|
|
47
|
+
[k in Exclude<keyof p, optKeys>]: InvertPatternInternal<p[k], WithDefault<GetKey<ExtractPlainObject<input>, k>, unknown>>;
|
|
48
|
+
} & {
|
|
49
|
+
[k in Extract<optKeys, keyof p>]?: InvertPatternInternal<p[k], WithDefault<GetKey<ExtractPlainObject<input>, k>, unknown>>;
|
|
50
|
+
}> : never : p;
|
|
51
|
+
export type ReduceIntersectionForExclude<tuple extends readonly any[], i, output = unknown> = tuple extends readonly [infer p, ...infer tail] ? ReduceIntersectionForExclude<tail, i, output & InvertPatternForExcludeInternal<p, i, unknown>> : output;
|
|
52
|
+
export type ReduceUnionForExclude<tuple extends readonly any[], i, output = never> = tuple extends readonly [infer p, ...infer tail] ? ReduceUnionForExclude<tail, i, output | InvertPatternForExcludeInternal<p, i, never>> : output;
|
|
53
|
+
type ExcludeIfExists<a, b> = [
|
|
54
|
+
b
|
|
55
|
+
] extends [never] ? never : unknown extends a ? never : All<[
|
|
56
|
+
Extends<a, NonLiteralPrimitive>,
|
|
57
|
+
Not<IsLiteral<a>>,
|
|
58
|
+
IsLiteral<b>
|
|
59
|
+
]> extends true ? never : DeepExclude<a, b>;
|
|
60
|
+
type InvertArrayPatternForExclude<p, i, empty, isReadonly extends boolean, startOutput extends any[] = [], endOutput extends any[] = []> = i extends readonly (infer ii)[] ? p extends readonly [] ? MaybeAddReadonly<[...startOutput, ...endOutput], isReadonly> : p extends readonly [infer p1, ...infer pRest] ? i extends readonly [infer i1, ...infer iRest] ? InvertArrayPatternForExclude<pRest, iRest, empty, isReadonly, [
|
|
61
|
+
...startOutput,
|
|
62
|
+
InvertPatternForExcludeInternal<p1, i1, empty>
|
|
63
|
+
], endOutput> : InvertArrayPatternForExclude<pRest, ii[], empty, isReadonly, [
|
|
64
|
+
...startOutput,
|
|
65
|
+
InvertPatternForExcludeInternal<p1, ii, empty>
|
|
66
|
+
], endOutput> : p extends readonly [...infer pInit, infer p1] ? i extends readonly [...infer iInit, infer i1] ? InvertArrayPatternForExclude<pInit, iInit, empty, isReadonly, startOutput, [
|
|
67
|
+
...endOutput,
|
|
68
|
+
InvertPatternForExcludeInternal<p1, i1, empty>
|
|
69
|
+
]> : InvertArrayPatternForExclude<pInit, ii[], empty, isReadonly, startOutput, [
|
|
70
|
+
...endOutput,
|
|
71
|
+
InvertPatternForExcludeInternal<p1, ii, empty>
|
|
72
|
+
]> : p extends readonly [...(readonly (infer pRest & AnyMatcher)[])] ? MaybeAddReadonly<[
|
|
73
|
+
...startOutput,
|
|
74
|
+
...Extract<InvertPatternForExcludeInternal<pRest, i, empty>, readonly any[]>,
|
|
75
|
+
...endOutput
|
|
76
|
+
], isReadonly> : MaybeAddReadonly<[
|
|
77
|
+
...startOutput,
|
|
78
|
+
...InvertPatternForExcludeInternal<ValueOf<p>, ii, empty>[],
|
|
79
|
+
...endOutput
|
|
80
|
+
], isReadonly> : empty;
|
|
81
|
+
/**
|
|
82
|
+
* ### InvertPatternForExclude
|
|
83
|
+
*/
|
|
84
|
+
export type InvertPatternForExclude<p, i> = Equal<Pattern<i>, p> extends true ? never : InvertPatternForExcludeInternal<p, i>;
|
|
85
|
+
type InvertPatternForExcludeInternal<p, i, empty = never> = unknown extends p ? i : [p] extends [Primitives] ? IsLiteral<p> extends true ? p : IsLiteral<i> extends true ? p : empty : p extends Matcher<infer matchableInput, infer subpattern, infer matcherType, any, infer excluded> ? {
|
|
86
|
+
select: InvertPatternForExcludeInternal<subpattern, i, empty>;
|
|
87
|
+
array: i extends readonly (infer ii)[] ? MaybeAddReadonly<InvertPatternForExcludeInternal<subpattern, ii, empty>[], IsReadonlyArray<i>> : empty;
|
|
88
|
+
record: subpattern extends [infer pk, infer pv] ? Record<Extract<InvertPatternForExcludeInternal<pk, RecordKey<i>, empty>, PropertyKey>, InvertPatternForExcludeInternal<pv, RecordValue<i>, empty>> : empty;
|
|
89
|
+
map: subpattern extends [infer pk, infer pv] ? i extends Map<infer ik, infer iv> ? Map<InvertPatternForExcludeInternal<pk, ik, empty>, InvertPatternForExcludeInternal<pv, iv, empty>> : empty : empty;
|
|
90
|
+
set: i extends Set<infer iv> ? Set<InvertPatternForExcludeInternal<subpattern, iv, empty>> : empty;
|
|
91
|
+
optional: InvertPatternForExcludeInternal<subpattern, i, empty> | undefined;
|
|
92
|
+
and: ReduceIntersectionForExclude<Extract<subpattern, readonly any[]>, i>;
|
|
93
|
+
or: ReduceUnionForExclude<Extract<subpattern, readonly any[]>, i>;
|
|
94
|
+
not: ExcludeIfExists<unknown extends matchableInput ? i : matchableInput, InvertPatternForExcludeInternal<subpattern, i>>;
|
|
95
|
+
default: excluded;
|
|
96
|
+
custom: excluded extends infer narrowedOrFn extends Fn ? Call<narrowedOrFn, i> : excluded;
|
|
97
|
+
}[matcherType] : p extends readonly any[] ? Extract<i, readonly any[]> extends infer arrayInput ? InvertArrayPatternForExclude<p, arrayInput, empty, IsReadonlyArray<arrayInput>> : never : IsPlainObject<p> extends true ? Equal<{}, p> extends true ? {} : i extends object ? [keyof p & keyof i] extends [never] ? empty : OptionalKeys<p> extends infer optKeys ? [optKeys] extends [never] ? {
|
|
98
|
+
readonly [k in keyof p]: k extends keyof i ? InvertPatternForExcludeInternal<p[k], i[k], empty> : InvertPatternInternal<p[k], unknown>;
|
|
99
|
+
} : Compute<{
|
|
100
|
+
readonly [k in Exclude<keyof p, optKeys>]: k extends keyof i ? InvertPatternForExcludeInternal<p[k], i[k], empty> : InvertPatternInternal<p[k], unknown>;
|
|
101
|
+
} & {
|
|
102
|
+
readonly [k in Extract<optKeys, keyof p>]?: k extends keyof i ? InvertPatternForExcludeInternal<p[k], i[k], empty> : InvertPatternInternal<p[k], unknown>;
|
|
103
|
+
}> : empty : empty : empty;
|
|
104
|
+
export {};
|