/
eager.test-d.ts
110 lines (94 loc) 路 3.01 KB
/
eager.test-d.ts
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { expectType } from 'tsd';
import { ArrayPath, FieldPathValues, Path, PathValue } from '../../types';
import { _, Depth3Type } from '../__fixtures__';
/** {@link Path} */ {
/** it should evaluate to never for an empty object */ {
const actual = _ as Path<{}>;
expectType<never>(actual);
}
/** it should evaluate to all paths of an object */ {
const actual = _ as Path<{ foo: { bar: string; baz: string } }>;
expectType<'foo' | 'foo.bar' | 'foo.baz'>(actual);
}
/** it should include paths through tuples */ {
const actual = _ as Path<{ foo: [string, number] }>;
expectType<'foo' | 'foo.0' | 'foo.1'>(actual);
}
/** it should include paths through arrays */ {
const actual = _ as Path<{ foo: string[] }>;
expectType<'foo' | `foo.${number}`>(actual);
}
/** it should be able to avoid self-referencing/recursion, not crashing on self-referencing types. */ {
type Foo = { foo: Foo };
const actual = _ as Path<Foo>;
expectType<'foo'>(actual);
}
/** it should not erroneously match subtypes as traversed */ {
type Foo =
| {
foo?: Foo;
bar?: {
baz: 1;
};
}
| {};
const actual = _ as Path<Foo>;
expectType<'foo' | 'bar' | 'bar.baz'>(actual);
}
}
/** {@link ArrayPath} */ {
/** it should evaluate to all paths pointing to a non-primitive array */ {
const actual = _ as ArrayPath<{
foo: Array<{ bar: string[]; baz: string[] }>;
}>;
expectType<'foo'>(actual);
}
/** it should include paths through tuples */ {
const actual = _ as ArrayPath<{ foo: [object[], object[]] }>;
expectType<'foo' | 'foo.0' | 'foo.1'>(actual);
}
/** it should include paths through arrays */ {
const actual = _ as ArrayPath<{ foo: string[][][] }>;
expectType<'foo' | `foo.${number}`>(actual);
}
/** it should be able to avoid self-referencing/recursion, not crashing on self-referencing types. */ {
type Foo = { foo: Foo[] };
const actual = _ as ArrayPath<Foo>;
expectType<'foo'>(actual);
}
/** it should not erroneously match subtypes as traversed */ {
type Foo =
| {
bar?: {
baz?: 1;
fooArr?: Foo[];
};
}
| {};
const actual = _ as ArrayPath<Foo>;
expectType<'bar.fooArr'>(actual);
}
}
/** {@link PathValue} */ {
/** it should traverse an object */ {
const actual = _ as PathValue<Depth3Type<number>, 'foo.foo.value'>;
expectType<number>(actual);
}
/** it should traverse a tuple */ {
const actual = _ as PathValue<Depth3Type<boolean>, 'bar.0.value'>;
expectType<boolean>(actual);
}
/** it should traverse an array */ {
const actual = _ as PathValue<Depth3Type<boolean>, 'baz.42.value'>;
expectType<boolean>(actual);
}
}
/** {@link FieldPathValues} */ {
/** it should resolve all paths */ {
const actual = _ as FieldPathValues<
Depth3Type<string>,
['foo.foo.value', 'bar.0.value', 'baz.42.value']
>;
expectType<[string, string, string]>(actual);
}
}