Skip to content

Commit 34ed45d

Browse files
committed
Adding more tests
1 parent 0056760 commit 34ed45d

File tree

4 files changed

+93
-65
lines changed

4 files changed

+93
-65
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
tests/cases/conformance/expressions/functionCalls/typeArgumentInferenceWithObjectLiteral.ts(35,10): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
2+
Type argument candidate 'E1' is not a valid type argument because it is not a supertype of candidate 'E2'.
3+
4+
5+
==== tests/cases/conformance/expressions/functionCalls/typeArgumentInferenceWithObjectLiteral.ts (1 errors) ====
6+
interface Computed<T> {
7+
read(): T;
8+
write(value: T);
9+
}
10+
11+
function foo<T>(x: Computed<T>) { }
12+
13+
var s: string;
14+
15+
// Calls below should infer string for T and then assign that type to the value parameter
16+
foo({
17+
read: () => s,
18+
write: value => s = value
19+
});
20+
foo({
21+
write: value => s = value,
22+
read: () => s
23+
});
24+
25+
enum E1 { X }
26+
enum E2 { X }
27+
28+
// Check that we infer from both a.r and b before fixing T in a.w
29+
30+
declare function f1<T, U>(a: { w: (x: T) => U; r: () => T; }, b: T): U;
31+
32+
var v1: number;
33+
var v1 = f1({ w: x => x, r: () => 0 }, 0);
34+
var v1 = f1({ w: x => x, r: () => 0 }, E1.X);
35+
var v1 = f1({ w: x => x, r: () => E1.X }, 0);
36+
37+
var v2: E1;
38+
var v2 = f1({ w: x => x, r: () => E1.X }, E1.X);
39+
40+
var v3 = f1({ w: x => x, r: () => E1.X }, E2.X); // Error
41+
~~
42+
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
43+
!!! error TS2453: Type argument candidate 'E1' is not a valid type argument because it is not a supertype of candidate 'E2'.
44+

tests/baselines/reference/typeArgumentInferenceWithObjectLiteral.js

+32
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ foo({
1717
write: value => s = value,
1818
read: () => s
1919
});
20+
21+
enum E1 { X }
22+
enum E2 { X }
23+
24+
// Check that we infer from both a.r and b before fixing T in a.w
25+
26+
declare function f1<T, U>(a: { w: (x: T) => U; r: () => T; }, b: T): U;
27+
28+
var v1: number;
29+
var v1 = f1({ w: x => x, r: () => 0 }, 0);
30+
var v1 = f1({ w: x => x, r: () => 0 }, E1.X);
31+
var v1 = f1({ w: x => x, r: () => E1.X }, 0);
32+
33+
var v2: E1;
34+
var v2 = f1({ w: x => x, r: () => E1.X }, E1.X);
35+
36+
var v3 = f1({ w: x => x, r: () => E1.X }, E2.X); // Error
2037

2138

2239
//// [typeArgumentInferenceWithObjectLiteral.js]
@@ -32,3 +49,18 @@ foo({
3249
write: function (value) { return s = value; },
3350
read: function () { return s; }
3451
});
52+
var E1;
53+
(function (E1) {
54+
E1[E1["X"] = 0] = "X";
55+
})(E1 || (E1 = {}));
56+
var E2;
57+
(function (E2) {
58+
E2[E2["X"] = 0] = "X";
59+
})(E2 || (E2 = {}));
60+
var v1;
61+
var v1 = f1({ w: function (x) { return x; }, r: function () { return 0; } }, 0);
62+
var v1 = f1({ w: function (x) { return x; }, r: function () { return 0; } }, 0 /* X */);
63+
var v1 = f1({ w: function (x) { return x; }, r: function () { return 0 /* X */; } }, 0);
64+
var v2;
65+
var v2 = f1({ w: function (x) { return x; }, r: function () { return 0 /* X */; } }, 0 /* X */);
66+
var v3 = f1({ w: function (x) { return x; }, r: function () { return 0 /* X */; } }, 0 /* X */); // Error

tests/baselines/reference/typeArgumentInferenceWithObjectLiteral.types

-65
This file was deleted.

tests/cases/conformance/expressions/functionCalls/typeArgumentInferenceWithObjectLiteral.ts

+17
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,20 @@ foo({
1616
write: value => s = value,
1717
read: () => s
1818
});
19+
20+
enum E1 { X }
21+
enum E2 { X }
22+
23+
// Check that we infer from both a.r and b before fixing T in a.w
24+
25+
declare function f1<T, U>(a: { w: (x: T) => U; r: () => T; }, b: T): U;
26+
27+
var v1: number;
28+
var v1 = f1({ w: x => x, r: () => 0 }, 0);
29+
var v1 = f1({ w: x => x, r: () => 0 }, E1.X);
30+
var v1 = f1({ w: x => x, r: () => E1.X }, 0);
31+
32+
var v2: E1;
33+
var v2 = f1({ w: x => x, r: () => E1.X }, E1.X);
34+
35+
var v3 = f1({ w: x => x, r: () => E1.X }, E2.X); // Error

0 commit comments

Comments
 (0)