Skip to content

Commit 199414d

Browse files
committed
Use this: microsoft/TypeScript#25947 (comment) trick to optimize typechecking
1 parent c6395b8 commit 199414d

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed

expensiveOptimized.ts

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
interface Iterable<C> {}
2+
3+
const unrelated = Symbol()
4+
type unrelated = typeof unrelated
5+
interface Collections<K,V> {
6+
[unrelated]: V[]
7+
}
8+
9+
interface ZipConcatMixin<T,SelfIdentifier extends keyof Collections<any,any>> {
10+
zip<U>(other: Collection<any, U>): Collections<number, [T,U]>[SelfIdentifier];
11+
zip<U,V>(other: Collection<any, U>, other2: Collection<any,V>): Collections<number, [T,U,V]>[SelfIdentifier];
12+
//zip<U,V,M>(other: Collection<any, U>, other2: Collection<any,V>, other3: Collection<any,M>): Collections<[T,U,V,M]>[SelfIdentifier];
13+
zip(...collections: Array<Collection<any, any>>): Collections<number,any>[SelfIdentifier];
14+
15+
concat<C>(...valuesOrCollections: Array<Iterable<C> | C>): Collections<number,T | C>[SelfIdentifier];
16+
}
17+
18+
interface MapFilterMixin<K,V,SelfIdentifier extends keyof Collections<any,any>> {
19+
map<M>(
20+
mapper: (value: V, key: K, iter: this) => M,
21+
context?: any
22+
): Collections<K,M>[SelfIdentifier];
23+
// flatMap<M>(
24+
// mapper: (value: V, key: K, iter: this) => Iterable<M>,
25+
// context?: any
26+
// ): Collections<K,M>[SelfIdentifier];
27+
filter<F extends V>(
28+
predicate: (value: V, index: K, iter: this) => value is F,
29+
context?: any
30+
): Collections<K,F>[SelfIdentifier];
31+
filter(
32+
predicate: (value: V, index: K, iter: this) => any,
33+
context?: any
34+
): this;
35+
}
36+
37+
export interface List<T> extends CollectionIndexed<T, ListSelf> {
38+
}
39+
const ListSelf = Symbol()
40+
type ListSelf = typeof ListSelf
41+
interface Collections<K,V> {
42+
[ListSelf]: List<V>
43+
}
44+
45+
export interface SeqIndexed<T> extends CollectionIndexed<T, SeqIndexedSelf> {
46+
toSeq(): this
47+
48+
zipAll(...collections: Array<Collection<any, any>>): SeqIndexed<any>;
49+
}
50+
const SeqIndexedSelf = Symbol()
51+
type SeqIndexedSelf = typeof SeqIndexedSelf
52+
interface Collections<K,V> {
53+
[SeqIndexedSelf]: SeqIndexed<V>
54+
}
55+
56+
export interface CollectionIndexed<T, SelfIdentifier extends keyof Collections<any, any> = CollectionIndexedSelf> extends Collection<number, T, SelfIdentifier>, ZipConcatMixin<T, SelfIdentifier> {
57+
interpose(separator: T): this;
58+
}
59+
const CollectionIndexedSelf = Symbol()
60+
type CollectionIndexedSelf = typeof CollectionIndexedSelf
61+
interface Collections<K,V> {
62+
[CollectionIndexedSelf]: CollectionIndexed<V>
63+
}
64+
65+
export interface Collection<K, V, SelfIdentifier extends keyof Collections<any,any> = CollectionSelf> extends MapFilterMixin<K,V,SelfIdentifier> {
66+
67+
// Conversion to JavaScript types
68+
69+
toJS(): Array<any> | { [key: string]: any };
70+
toJSON(): Array<V> | { [key: string]: V };
71+
72+
// Conversion to Collections
73+
74+
toMap(): Collection<K, V>;
75+
toOrderedMap(): Collection<K, V>;
76+
toSet(): Collection<V,V>;
77+
toOrderedSet(): Collection<V,V>;
78+
toList(): List<V>;
79+
toStack(): Collection<V,V>;
80+
81+
// Conversion to Seq
82+
83+
toSeq(): Collection<K, V>;
84+
toKeyedSeq(): Collection<K, V>;
85+
toIndexedSeq(): SeqIndexed<V>;
86+
87+
// Sequence algorithms
88+
89+
// map<M>(
90+
// mapper: (value: V, key: K, iter: this) => M,
91+
// context?: any
92+
// ): Collection<K, M>;
93+
// filter<F extends V>(
94+
// predicate: (value: V, key: K, iter: this) => value is F,
95+
// context?: any
96+
// ): Collection<K, F>;
97+
// filter(
98+
// predicate: (value: V, key: K, iter: this) => any,
99+
// context?: any
100+
// ): this;
101+
filterNot(
102+
predicate: (value: V, key: K, iter: this) => boolean,
103+
context?: any
104+
): this;
105+
sort(comparator?: (valueA: V, valueB: V) => number): this;
106+
sortBy<C>(
107+
comparatorValueMapper: (value: V, key: K, iter: this) => C,
108+
comparator?: (valueA: C, valueB: C) => number
109+
): this;
110+
groupBy<G>(
111+
grouper: (value: V, key: K, iter: this) => G,
112+
context?: any
113+
): /*Map*/Collection<G, /*this*/Collection<K, V>>;
114+
115+
// Padding
116+
takeUntil0(predicate: (iter: this) => boolean): this;
117+
takeUntil1(predicate: (iter: this) => boolean): this;
118+
takeUntil2(predicate: (iter: this) => boolean): this;
119+
takeUntil3(predicate: (iter: this) => boolean): this;
120+
takeUntil4(predicate: (iter: this) => boolean): this;
121+
takeUntil5(predicate: (iter: this) => boolean): this;
122+
takeUntil6(predicate: (iter: this) => boolean): this;
123+
ttakeUntil0(predicate: (iter: this) => boolean): this;
124+
ttakeUntil1(predicate: (iter: this) => boolean): this;
125+
ttakeUntil2(predicate: (iter: this) => boolean): this;
126+
ttakeUntil3(predicate: (iter: this) => boolean): this;
127+
ttakeUntil4(predicate: (iter: this) => boolean): this;
128+
ttakeUntil5(predicate: (iter: this) => boolean): this;
129+
ttakeUntil6(predicate: (iter: this) => boolean): this;
130+
tttakeUntil0(predicate: (iter: this) => boolean): this;
131+
tttakeUntil1(predicate: (iter: this) => boolean): this;
132+
tttakeUntil2(predicate: (iter: this) => boolean): this;
133+
tttakeUntil3(predicate: (iter: this) => boolean): this;
134+
tttakeUntil4(predicate: (iter: this) => boolean): this;
135+
tttakeUntil5(predicate: (iter: this) => boolean): this;
136+
tttakeUntil6(predicate: (iter: this) => boolean): this;
137+
ttttakeUntil0(predicate: (iter: this) => boolean): this;
138+
ttttakeUntil1(predicate: (iter: this) => boolean): this;
139+
ttttakeUntil2(predicate: (iter: this) => boolean): this;
140+
ttttakeUntil3(predicate: (iter: this) => boolean): this;
141+
ttttakeUntil4(predicate: (iter: this) => boolean): this;
142+
ttttakeUntil5(predicate: (iter: this) => boolean): this;
143+
ttttakeUntil6(predicate: (iter: this) => boolean): this;
144+
145+
tttttakeUntil0(predicate: (iter: this) => boolean): this;
146+
tttttakeUntil1(predicate: (iter: this) => boolean): this;
147+
tttttakeUntil2(predicate: (iter: this) => boolean): this;
148+
tttttakeUntil3(predicate: (iter: this) => boolean): this;
149+
tttttakeUntil4(predicate: (iter: this) => boolean): this;
150+
tttttakeUntil5(predicate: (iter: this) => boolean): this;
151+
tttttakeUntil6(predicate: (iter: this) => boolean): this;
152+
}
153+
const CollectionSelf = Symbol()
154+
type CollectionSelf = typeof CollectionSelf
155+
interface Collections<K,V> {
156+
[CollectionSelf]: Collection<K,V>
157+
}
158+
159+
function a<T> () {
160+
const a: SeqIndexed<T> = 0 as any
161+
const b: CollectionIndexed<T> = a
162+
163+
const c: CollectionIndexed<T, SeqIndexedSelf> = 0 as any
164+
c.zip(0 as any as Collection<any, T>)
165+
const d: CollectionIndexed<T, CollectionIndexedSelf> = c
166+
d.zip(0 as any as Collection<any, T>)
167+
168+
return b
169+
}
170+
171+
const b: SeqIndexed<(number | string)> = 0 as any
172+
const c = b.filter((v): v is number => typeof v !== "string")

0 commit comments

Comments
 (0)