Skip to content

Commit d94027b

Browse files
author
Yui T
committed
Add conformance tests for arugment expression, getSetAccessor, arrayLiteralExpression
1 parent 905375d commit d94027b

13 files changed

+353
-38
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
tests/cases/conformance/expressions/contextualTyping/argumentExpressionContextualTyping.ts(16,5): error TS2345: Argument of type '(string | number | boolean)[]' is not assignable to parameter of type '[string, number, boolean]'.
2+
Property '0' is missing in type '(string | number | boolean)[]'.
3+
tests/cases/conformance/expressions/contextualTyping/argumentExpressionContextualTyping.ts(17,5): error TS2345: Argument of type '(string | number | boolean)[]' is not assignable to parameter of type '[string, number, boolean]'.
4+
tests/cases/conformance/expressions/contextualTyping/argumentExpressionContextualTyping.ts(18,5): error TS2345: Argument of type '{ x: (string | number)[]; y: { c: boolean; d: string; e: number; }; }' is not assignable to parameter of type '{ x: [any, any]; y: { c: any; d: any; e: any; }; }'.
5+
Types of property 'x' are incompatible.
6+
Type '(string | number)[]' is not assignable to type '[any, any]'.
7+
Property '0' is missing in type '(string | number)[]'.
8+
9+
10+
==== tests/cases/conformance/expressions/contextualTyping/argumentExpressionContextualTyping.ts (3 errors) ====
11+
// In a typed function call, argument expressions are contextually typed by their corresponding parameter types.
12+
function foo({x: [a, b], y: {c, d, e}}) { }
13+
function bar({x: [a, b = 10], y: {c, d, e = { f:1 }}}) { }
14+
function baz(x: [string, number, boolean]) { }
15+
16+
var o = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
17+
var o1: { x: [string, number], y: { c: boolean, d: string, e: number } } = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
18+
foo(o1); // Not error since x has contextual type of tuple namely [string, number]
19+
foo({ x: ["string", 1], y: { c: true, d: "world", e: 3 } }); // Not error
20+
21+
var array = ["string", 1, true];
22+
var tuple: [string, number, boolean] = ["string", 1, true];
23+
baz(tuple);
24+
baz(["string", 1, true]);
25+
26+
baz(array); // Error
27+
~~~~~
28+
!!! error TS2345: Argument of type '(string | number | boolean)[]' is not assignable to parameter of type '[string, number, boolean]'.
29+
!!! error TS2345: Property '0' is missing in type '(string | number | boolean)[]'.
30+
baz(["string", 1, true, ...array]); // Error
31+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32+
!!! error TS2345: Argument of type '(string | number | boolean)[]' is not assignable to parameter of type '[string, number, boolean]'.
33+
foo(o); // Error because x has an array type namely (string|number)[]
34+
~
35+
!!! error TS2345: Argument of type '{ x: (string | number)[]; y: { c: boolean; d: string; e: number; }; }' is not assignable to parameter of type '{ x: [any, any]; y: { c: any; d: any; e: any; }; }'.
36+
!!! error TS2345: Types of property 'x' are incompatible.
37+
!!! error TS2345: Type '(string | number)[]' is not assignable to type '[any, any]'.
38+
!!! error TS2345: Property '0' is missing in type '(string | number)[]'.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [argumentExpressionContextualTyping.ts]
2+
// In a typed function call, argument expressions are contextually typed by their corresponding parameter types.
3+
function foo({x: [a, b], y: {c, d, e}}) { }
4+
function bar({x: [a, b = 10], y: {c, d, e = { f:1 }}}) { }
5+
function baz(x: [string, number, boolean]) { }
6+
7+
var o = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
8+
var o1: { x: [string, number], y: { c: boolean, d: string, e: number } } = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
9+
foo(o1); // Not error since x has contextual type of tuple namely [string, number]
10+
foo({ x: ["string", 1], y: { c: true, d: "world", e: 3 } }); // Not error
11+
12+
var array = ["string", 1, true];
13+
var tuple: [string, number, boolean] = ["string", 1, true];
14+
baz(tuple);
15+
baz(["string", 1, true]);
16+
17+
baz(array); // Error
18+
baz(["string", 1, true, ...array]); // Error
19+
foo(o); // Error because x has an array type namely (string|number)[]
20+
21+
//// [argumentExpressionContextualTyping.js]
22+
// In a typed function call, argument expressions are contextually typed by their corresponding parameter types.
23+
function foo(_a) {
24+
var _b = _a.x, a = _b[0], b = _b[1], _c = _a.y, c = _c.c, d = _c.d, e = _c.e;
25+
}
26+
function bar(_a) {
27+
var _b = _a.x, a = _b[0], _c = _b[1], b = _c === void 0 ? 10 : _c, _d = _a.y, c = _d.c, d = _d.d, _e = _d.e, e = _e === void 0 ? { f: 1 } : _e;
28+
}
29+
function baz(x) { }
30+
var o = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
31+
var o1 = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
32+
foo(o1); // Not error since x has contextual type of tuple namely [string, number]
33+
foo({ x: ["string", 1], y: { c: true, d: "world", e: 3 } }); // Not error
34+
var array = ["string", 1, true];
35+
var tuple = ["string", 1, true];
36+
baz(tuple);
37+
baz(["string", 1, true]);
38+
baz(array); // Error
39+
baz(["string", 1, true].concat(array)); // Error
40+
foo(o); // Error because x has an array type namely (string|number)[]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
tests/cases/conformance/expressions/contextualTyping/arrayLiteralExpressionContextualTyping.ts(8,5): error TS2322: Type '[number, number, number, string]' is not assignable to type '[number, number, number]'.
2+
Types of property 'pop' are incompatible.
3+
Type '() => string | number' is not assignable to type '() => number'.
4+
Type 'string | number' is not assignable to type 'number'.
5+
Type 'string' is not assignable to type 'number'.
6+
tests/cases/conformance/expressions/contextualTyping/arrayLiteralExpressionContextualTyping.ts(14,5): error TS2322: Type 'number[]' is not assignable to type '[number, number, number]'.
7+
Property '0' is missing in type 'number[]'.
8+
9+
10+
==== tests/cases/conformance/expressions/contextualTyping/arrayLiteralExpressionContextualTyping.ts (2 errors) ====
11+
// In a contextually typed array literal expression containing no spread elements, an element expression at index N is contextually typed by
12+
// the type of the property with the numeric name N in the contextual type, if any, or otherwise
13+
// the numeric index type of the contextual type, if any.
14+
var array = [1, 2, 3];
15+
var array1 = [true, 2, 3]; // Contextual type by the numeric index type of the contextual type
16+
var tup: [number, number, number] = [1, 2, 3, 4];
17+
var tup1: [number|string, number|string, number|string] = [1, 2, 3, "string"];
18+
var tup2: [number, number, number] = [1, 2, 3, "string"]; // Error
19+
~~~~
20+
!!! error TS2322: Type '[number, number, number, string]' is not assignable to type '[number, number, number]'.
21+
!!! error TS2322: Types of property 'pop' are incompatible.
22+
!!! error TS2322: Type '() => string | number' is not assignable to type '() => number'.
23+
!!! error TS2322: Type 'string | number' is not assignable to type 'number'.
24+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
25+
26+
// In a contextually typed array literal expression containing one or more spread elements,
27+
// an element expression at index N is contextually typed by the numeric index type of the contextual type, if any.
28+
var spr = [1, 2, 3, ...array];
29+
var spr1 = [1, 2, 3, ...tup];
30+
var spr2:[number, number, number] = [1, 2, 3, ...tup]; // Error
31+
~~~~
32+
!!! error TS2322: Type 'number[]' is not assignable to type '[number, number, number]'.
33+
!!! error TS2322: Property '0' is missing in type 'number[]'.
34+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [arrayLiteralExpressionContextualTyping.ts]
2+
// In a contextually typed array literal expression containing no spread elements, an element expression at index N is contextually typed by
3+
// the type of the property with the numeric name N in the contextual type, if any, or otherwise
4+
// the numeric index type of the contextual type, if any.
5+
var array = [1, 2, 3];
6+
var array1 = [true, 2, 3]; // Contextual type by the numeric index type of the contextual type
7+
var tup: [number, number, number] = [1, 2, 3, 4];
8+
var tup1: [number|string, number|string, number|string] = [1, 2, 3, "string"];
9+
var tup2: [number, number, number] = [1, 2, 3, "string"]; // Error
10+
11+
// In a contextually typed array literal expression containing one or more spread elements,
12+
// an element expression at index N is contextually typed by the numeric index type of the contextual type, if any.
13+
var spr = [1, 2, 3, ...array];
14+
var spr1 = [1, 2, 3, ...tup];
15+
var spr2:[number, number, number] = [1, 2, 3, ...tup]; // Error
16+
17+
18+
//// [arrayLiteralExpressionContextualTyping.js]
19+
// In a contextually typed array literal expression containing no spread elements, an element expression at index N is contextually typed by
20+
// the type of the property with the numeric name N in the contextual type, if any, or otherwise
21+
// the numeric index type of the contextual type, if any.
22+
var array = [1, 2, 3];
23+
var array1 = [true, 2, 3]; // Contextual type by the numeric index type of the contextual type
24+
var tup = [1, 2, 3, 4];
25+
var tup1 = [1, 2, 3, "string"];
26+
var tup2 = [1, 2, 3, "string"]; // Error
27+
// In a contextually typed array literal expression containing one or more spread elements,
28+
// an element expression at index N is contextually typed by the numeric index type of the contextual type, if any.
29+
var spr = [1, 2, 3].concat(array);
30+
var spr1 = [1, 2, 3].concat(tup);
31+
var spr2 = [1, 2, 3].concat(tup); // Error
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
tests/cases/conformance/expressions/contextualTyping/getSetAccessorContextualTyping.ts(8,16): error TS2322: Type 'string' is not assignable to type 'number'.
2+
3+
4+
==== tests/cases/conformance/expressions/contextualTyping/getSetAccessorContextualTyping.ts (1 errors) ====
5+
// In the body of a get accessor with no return type annotation,
6+
// if a matching set accessor exists and that set accessor has a parameter type annotation,
7+
// return expressions are contextually typed by the type given in the set accessor's parameter type annotation.
8+
9+
class C {
10+
set X(x: number) { }
11+
get X() {
12+
return "string"; // Error; get contextual type by set accessor parameter type annotation
13+
~~~~~~~~
14+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
15+
}
16+
17+
set Y(y) { }
18+
get Y() {
19+
return true;
20+
}
21+
22+
set W(w) { }
23+
get W(): boolean {
24+
return true;
25+
}
26+
27+
set Z(z: number) { }
28+
get Z() {
29+
return 1;
30+
}
31+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//// [getSetAccessorContextualTyping.ts]
2+
// In the body of a get accessor with no return type annotation,
3+
// if a matching set accessor exists and that set accessor has a parameter type annotation,
4+
// return expressions are contextually typed by the type given in the set accessor's parameter type annotation.
5+
6+
class C {
7+
set X(x: number) { }
8+
get X() {
9+
return "string"; // Error; get contextual type by set accessor parameter type annotation
10+
}
11+
12+
set Y(y) { }
13+
get Y() {
14+
return true;
15+
}
16+
17+
set W(w) { }
18+
get W(): boolean {
19+
return true;
20+
}
21+
22+
set Z(z: number) { }
23+
get Z() {
24+
return 1;
25+
}
26+
}
27+
28+
//// [getSetAccessorContextualTyping.js]
29+
// In the body of a get accessor with no return type annotation,
30+
// if a matching set accessor exists and that set accessor has a parameter type annotation,
31+
// return expressions are contextually typed by the type given in the set accessor's parameter type annotation.
32+
var C = (function () {
33+
function C() {
34+
}
35+
Object.defineProperty(C.prototype, "X", {
36+
get: function () {
37+
return "string"; // Error; get contextual type by set accessor parameter type annotation
38+
},
39+
set: function (x) { },
40+
enumerable: true,
41+
configurable: true
42+
});
43+
Object.defineProperty(C.prototype, "Y", {
44+
get: function () {
45+
return true;
46+
},
47+
set: function (y) { },
48+
enumerable: true,
49+
configurable: true
50+
});
51+
Object.defineProperty(C.prototype, "W", {
52+
get: function () {
53+
return true;
54+
},
55+
set: function (w) { },
56+
enumerable: true,
57+
configurable: true
58+
});
59+
Object.defineProperty(C.prototype, "Z", {
60+
get: function () {
61+
return 1;
62+
},
63+
set: function (z) { },
64+
enumerable: true,
65+
configurable: true
66+
});
67+
return C;
68+
})();

tests/baselines/reference/objectLiteralContextualTyping.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//// [objectLiteralContextualTyping.ts]
2-
// Tests related to #1774
2+
// In a contextually typed object literal, each property value expression is contextually typed by
3+
// the type of the property with a matching name in the contextual type, if any, or otherwise
4+
// for a numerically named property, the numeric index type of the contextual type, if any, or otherwise
5+
// the string index type of the contextual type, if any.
36

47
interface Item {
58
name: string;
@@ -28,7 +31,10 @@ var b: {};
2831

2932

3033
//// [objectLiteralContextualTyping.js]
31-
// Tests related to #1774
34+
// In a contextually typed object literal, each property value expression is contextually typed by
35+
// the type of the property with a matching name in the contextual type, if any, or otherwise
36+
// for a numerically named property, the numeric index type of the contextual type, if any, or otherwise
37+
// the string index type of the contextual type, if any.
3238
var x = foo({ name: "Sprocket" });
3339
var x;
3440
var y = foo({ name: "Sprocket", description: "Bumpy wheel" });

0 commit comments

Comments
 (0)