forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintraExpressionInferences.js
385 lines (336 loc) · 9.09 KB
/
intraExpressionInferences.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//// [intraExpressionInferences.ts]
// Repros from #47599
declare function callIt<T>(obj: {
produce: (n: number) => T,
consume: (x: T) => void
}): void;
callIt({
produce: () => 0,
consume: n => n.toFixed()
});
callIt({
produce: _a => 0,
consume: n => n.toFixed(),
});
callIt({
produce() {
return 0;
},
consume: n => n.toFixed()
});
declare function callItT<T>(obj: [(n: number) => T, (x: T) => void]): void;
callItT([() => 0, n => n.toFixed()]);
callItT([_a => 0, n => n.toFixed()]);
// Repro from #25092
interface MyInterface<T> {
retrieveGeneric: (parameter: string) => T,
operateWithGeneric: (generic: T) => string
}
const inferTypeFn = <T>(generic: MyInterface<T>) => generic;
const myGeneric = inferTypeFn({
retrieveGeneric: parameter => 5,
operateWithGeneric: generic => generic.toFixed()
});
// Repro #38623
function make<M>(o: { mutations: M, action: (m: M) => void }) { }
make({
mutations: {
foo() { }
},
action: (a) => { a.foo() }
});
// Repro from #38845
declare function foo<A>(options: { a: A, b: (a: A) => void }): void;
foo({
a: () => { return 42 },
b(a) {},
});
foo({
a: function () { return 42 },
b(a) {},
});
foo({
a() { return 42 },
b(a) {},
});
// Repro from #38872
type Chain<R1, R2> = {
a(): R1,
b(a: R1): R2;
c(b: R2): void;
};
function test<R1, R2>(foo: Chain<R1, R2>) {}
test({
a: () => 0,
b: (a) => 'a',
c: (b) => {
const x: string = b;
}
});
test({
a: () => 0,
b: (a) => a,
c: (b) => {
const x: number = b;
}
});
// Repro from #41712
class Wrapper<T = any> {
public value?: T;
}
type WrappedMap = Record<string, Wrapper>;
type Unwrap<D extends WrappedMap> = {
[K in keyof D]: D[K] extends Wrapper<infer T> ? T : never;
};
type MappingComponent<I extends WrappedMap, O extends WrappedMap> = {
setup(): { inputs: I; outputs: O };
map?: (inputs: Unwrap<I>) => Unwrap<O>;
};
declare function createMappingComponent<I extends WrappedMap, O extends WrappedMap>(def: MappingComponent<I, O>): void;
createMappingComponent({
setup() {
return {
inputs: {
num: new Wrapper<number>(),
str: new Wrapper<string>()
},
outputs: {
bool: new Wrapper<boolean>(),
str: new Wrapper<string>()
}
};
},
map(inputs) {
return {
bool: inputs.nonexistent,
str: inputs.num, // Causes error
}
}
});
// Repro from #48279
function simplified<T>(props: { generator: () => T, receiver: (t: T) => any }) {}
function whatIWant<T>(props: { generator: (bob: any) => T, receiver: (t: T) => any }) {}
function nonObject<T>(generator: (bob: any) => T, receiver: (t: T) => any) {}
simplified({ generator: () => 123, receiver: (t) => console.log(t + 2) })
whatIWant({ generator: (bob) => bob ? 1 : 2, receiver: (t) => console.log(t + 2) })
nonObject((bob) => bob ? 1 : 2, (t) => console.log(t + 2))
// Repro from #48466
interface Opts<TParams, TDone, TMapped> {
fetch: (params: TParams, foo: number) => TDone,
map: (data: TDone) => TMapped
}
function example<TParams, TDone, TMapped>(options: Opts<TParams, TDone, TMapped>) {
return (params: TParams) => {
const data = options.fetch(params, 123)
return options.map(data)
}
}
interface Params {
one: number
two: string
}
example({
fetch: (params: Params) => 123,
map: (number) => String(number)
});
example({
fetch: (params: Params, foo: number) => 123,
map: (number) => String(number)
});
example({
fetch: (params: Params, foo) => 123,
map: (number) => String(number)
});
// Repro from #45255
declare const branch:
<T, U extends T>(_: { test: T, if: (t: T) => t is U, then: (u: U) => void }) => void
declare const x: "a" | "b"
branch({
test: x,
if: (t): t is "a" => t === "a",
then: u => {
let test1: "a" = u
}
})
//// [intraExpressionInferences.js]
"use strict";
// Repros from #47599
callIt({
produce: function () { return 0; },
consume: function (n) { return n.toFixed(); }
});
callIt({
produce: function (_a) { return 0; },
consume: function (n) { return n.toFixed(); }
});
callIt({
produce: function () {
return 0;
},
consume: function (n) { return n.toFixed(); }
});
callItT([function () { return 0; }, function (n) { return n.toFixed(); }]);
callItT([function (_a) { return 0; }, function (n) { return n.toFixed(); }]);
var inferTypeFn = function (generic) { return generic; };
var myGeneric = inferTypeFn({
retrieveGeneric: function (parameter) { return 5; },
operateWithGeneric: function (generic) { return generic.toFixed(); }
});
// Repro #38623
function make(o) { }
make({
mutations: {
foo: function () { }
},
action: function (a) { a.foo(); }
});
foo({
a: function () { return 42; },
b: function (a) { }
});
foo({
a: function () { return 42; },
b: function (a) { }
});
foo({
a: function () { return 42; },
b: function (a) { }
});
function test(foo) { }
test({
a: function () { return 0; },
b: function (a) { return 'a'; },
c: function (b) {
var x = b;
}
});
test({
a: function () { return 0; },
b: function (a) { return a; },
c: function (b) {
var x = b;
}
});
// Repro from #41712
var Wrapper = /** @class */ (function () {
function Wrapper() {
}
return Wrapper;
}());
createMappingComponent({
setup: function () {
return {
inputs: {
num: new Wrapper(),
str: new Wrapper()
},
outputs: {
bool: new Wrapper(),
str: new Wrapper()
}
};
},
map: function (inputs) {
return {
bool: inputs.nonexistent,
str: inputs.num
};
}
});
// Repro from #48279
function simplified(props) { }
function whatIWant(props) { }
function nonObject(generator, receiver) { }
simplified({ generator: function () { return 123; }, receiver: function (t) { return console.log(t + 2); } });
whatIWant({ generator: function (bob) { return bob ? 1 : 2; }, receiver: function (t) { return console.log(t + 2); } });
nonObject(function (bob) { return bob ? 1 : 2; }, function (t) { return console.log(t + 2); });
function example(options) {
return function (params) {
var data = options.fetch(params, 123);
return options.map(data);
};
}
example({
fetch: function (params) { return 123; },
map: function (number) { return String(number); }
});
example({
fetch: function (params, foo) { return 123; },
map: function (number) { return String(number); }
});
example({
fetch: function (params, foo) { return 123; },
map: function (number) { return String(number); }
});
branch({
test: x,
"if": function (t) { return t === "a"; },
then: function (u) {
var test1 = u;
}
});
//// [intraExpressionInferences.d.ts]
declare function callIt<T>(obj: {
produce: (n: number) => T;
consume: (x: T) => void;
}): void;
declare function callItT<T>(obj: [(n: number) => T, (x: T) => void]): void;
interface MyInterface<T> {
retrieveGeneric: (parameter: string) => T;
operateWithGeneric: (generic: T) => string;
}
declare const inferTypeFn: <T>(generic: MyInterface<T>) => MyInterface<T>;
declare const myGeneric: MyInterface<number>;
declare function make<M>(o: {
mutations: M;
action: (m: M) => void;
}): void;
declare function foo<A>(options: {
a: A;
b: (a: A) => void;
}): void;
declare type Chain<R1, R2> = {
a(): R1;
b(a: R1): R2;
c(b: R2): void;
};
declare function test<R1, R2>(foo: Chain<R1, R2>): void;
declare class Wrapper<T = any> {
value?: T;
}
declare type WrappedMap = Record<string, Wrapper>;
declare type Unwrap<D extends WrappedMap> = {
[K in keyof D]: D[K] extends Wrapper<infer T> ? T : never;
};
declare type MappingComponent<I extends WrappedMap, O extends WrappedMap> = {
setup(): {
inputs: I;
outputs: O;
};
map?: (inputs: Unwrap<I>) => Unwrap<O>;
};
declare function createMappingComponent<I extends WrappedMap, O extends WrappedMap>(def: MappingComponent<I, O>): void;
declare function simplified<T>(props: {
generator: () => T;
receiver: (t: T) => any;
}): void;
declare function whatIWant<T>(props: {
generator: (bob: any) => T;
receiver: (t: T) => any;
}): void;
declare function nonObject<T>(generator: (bob: any) => T, receiver: (t: T) => any): void;
interface Opts<TParams, TDone, TMapped> {
fetch: (params: TParams, foo: number) => TDone;
map: (data: TDone) => TMapped;
}
declare function example<TParams, TDone, TMapped>(options: Opts<TParams, TDone, TMapped>): (params: TParams) => TMapped;
interface Params {
one: number;
two: string;
}
declare const branch: <T, U extends T>(_: {
test: T;
if: (t: T) => t is U;
then: (u: U) => void;
}) => void;
declare const x: "a" | "b";