Skip to content

Conformance test for 4.19 Contextually Typed Expressions #2814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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]'.
Property '0' is missing in type '(string | number | boolean)[]'.
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]'.
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; }; }'.
Types of property 'x' are incompatible.
Type '(string | number)[]' is not assignable to type '[any, any]'.
Property '0' is missing in type '(string | number)[]'.


==== tests/cases/conformance/expressions/contextualTyping/argumentExpressionContextualTyping.ts (3 errors) ====
// In a typed function call, argument expressions are contextually typed by their corresponding parameter types.
function foo({x: [a, b], y: {c, d, e}}) { }
function bar({x: [a, b = 10], y: {c, d, e = { f:1 }}}) { }
function baz(x: [string, number, boolean]) { }

var o = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
var o1: { x: [string, number], y: { c: boolean, d: string, e: number } } = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
foo(o1); // Not error since x has contextual type of tuple namely [string, number]
foo({ x: ["string", 1], y: { c: true, d: "world", e: 3 } }); // Not error

var array = ["string", 1, true];
var tuple: [string, number, boolean] = ["string", 1, true];
baz(tuple);
baz(["string", 1, true]);

baz(array); // Error
~~~~~
!!! error TS2345: Argument of type '(string | number | boolean)[]' is not assignable to parameter of type '[string, number, boolean]'.
!!! error TS2345: Property '0' is missing in type '(string | number | boolean)[]'.
baz(["string", 1, true, ...array]); // Error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '(string | number | boolean)[]' is not assignable to parameter of type '[string, number, boolean]'.
foo(o); // Error because x has an array type namely (string|number)[]
~
!!! 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; }; }'.
!!! error TS2345: Types of property 'x' are incompatible.
!!! error TS2345: Type '(string | number)[]' is not assignable to type '[any, any]'.
!!! error TS2345: Property '0' is missing in type '(string | number)[]'.
40 changes: 40 additions & 0 deletions tests/baselines/reference/argumentExpressionContextualTyping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//// [argumentExpressionContextualTyping.ts]
// In a typed function call, argument expressions are contextually typed by their corresponding parameter types.
function foo({x: [a, b], y: {c, d, e}}) { }
function bar({x: [a, b = 10], y: {c, d, e = { f:1 }}}) { }
function baz(x: [string, number, boolean]) { }

var o = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
var o1: { x: [string, number], y: { c: boolean, d: string, e: number } } = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
foo(o1); // Not error since x has contextual type of tuple namely [string, number]
foo({ x: ["string", 1], y: { c: true, d: "world", e: 3 } }); // Not error

var array = ["string", 1, true];
var tuple: [string, number, boolean] = ["string", 1, true];
baz(tuple);
baz(["string", 1, true]);

baz(array); // Error
baz(["string", 1, true, ...array]); // Error
foo(o); // Error because x has an array type namely (string|number)[]

//// [argumentExpressionContextualTyping.js]
// In a typed function call, argument expressions are contextually typed by their corresponding parameter types.
function foo(_a) {
var _b = _a.x, a = _b[0], b = _b[1], _c = _a.y, c = _c.c, d = _c.d, e = _c.e;
}
function bar(_a) {
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;
}
function baz(x) { }
var o = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
var o1 = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
foo(o1); // Not error since x has contextual type of tuple namely [string, number]
foo({ x: ["string", 1], y: { c: true, d: "world", e: 3 } }); // Not error
var array = ["string", 1, true];
var tuple = ["string", 1, true];
baz(tuple);
baz(["string", 1, true]);
baz(array); // Error
baz(["string", 1, true].concat(array)); // Error
foo(o); // Error because x has an array type namely (string|number)[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
tests/cases/conformance/expressions/contextualTyping/arrayLiteralExpressionContextualTyping.ts(8,5): error TS2322: Type '[number, number, number, string]' is not assignable to type '[number, number, number]'.
Types of property 'pop' are incompatible.
Type '() => string | number' is not assignable to type '() => number'.
Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/expressions/contextualTyping/arrayLiteralExpressionContextualTyping.ts(14,5): error TS2322: Type 'number[]' is not assignable to type '[number, number, number]'.
Property '0' is missing in type 'number[]'.


==== tests/cases/conformance/expressions/contextualTyping/arrayLiteralExpressionContextualTyping.ts (2 errors) ====
// In a contextually typed array literal expression containing no spread elements, an element expression at index N is contextually typed by
// the type of the property with the numeric name N in the contextual type, if any, or otherwise
// the numeric index type of the contextual type, if any.
var array = [1, 2, 3];
var array1 = [true, 2, 3]; // Contextual type by the numeric index type of the contextual type
var tup: [number, number, number] = [1, 2, 3, 4];
var tup1: [number|string, number|string, number|string] = [1, 2, 3, "string"];
var tup2: [number, number, number] = [1, 2, 3, "string"]; // Error
~~~~
!!! error TS2322: Type '[number, number, number, string]' is not assignable to type '[number, number, number]'.
!!! error TS2322: Types of property 'pop' are incompatible.
!!! error TS2322: Type '() => string | number' is not assignable to type '() => number'.
!!! error TS2322: Type 'string | number' is not assignable to type 'number'.
!!! error TS2322: Type 'string' is not assignable to type 'number'.

// In a contextually typed array literal expression containing one or more spread elements,
// an element expression at index N is contextually typed by the numeric index type of the contextual type, if any.
var spr = [1, 2, 3, ...array];
var spr1 = [1, 2, 3, ...tup];
var spr2:[number, number, number] = [1, 2, 3, ...tup]; // Error
~~~~
!!! error TS2322: Type 'number[]' is not assignable to type '[number, number, number]'.
!!! error TS2322: Property '0' is missing in type 'number[]'.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [arrayLiteralExpressionContextualTyping.ts]
// In a contextually typed array literal expression containing no spread elements, an element expression at index N is contextually typed by
// the type of the property with the numeric name N in the contextual type, if any, or otherwise
// the numeric index type of the contextual type, if any.
var array = [1, 2, 3];
var array1 = [true, 2, 3]; // Contextual type by the numeric index type of the contextual type
var tup: [number, number, number] = [1, 2, 3, 4];
var tup1: [number|string, number|string, number|string] = [1, 2, 3, "string"];
var tup2: [number, number, number] = [1, 2, 3, "string"]; // Error

// In a contextually typed array literal expression containing one or more spread elements,
// an element expression at index N is contextually typed by the numeric index type of the contextual type, if any.
var spr = [1, 2, 3, ...array];
var spr1 = [1, 2, 3, ...tup];
var spr2:[number, number, number] = [1, 2, 3, ...tup]; // Error


//// [arrayLiteralExpressionContextualTyping.js]
// In a contextually typed array literal expression containing no spread elements, an element expression at index N is contextually typed by
// the type of the property with the numeric name N in the contextual type, if any, or otherwise
// the numeric index type of the contextual type, if any.
var array = [1, 2, 3];
var array1 = [true, 2, 3]; // Contextual type by the numeric index type of the contextual type
var tup = [1, 2, 3, 4];
var tup1 = [1, 2, 3, "string"];
var tup2 = [1, 2, 3, "string"]; // Error
// In a contextually typed array literal expression containing one or more spread elements,
// an element expression at index N is contextually typed by the numeric index type of the contextual type, if any.
var spr = [1, 2, 3].concat(array);
var spr1 = [1, 2, 3].concat(tup);
var spr2 = [1, 2, 3].concat(tup); // Error
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
tests/cases/conformance/expressions/contextualTyping/getSetAccessorContextualTyping.ts(8,16): error TS2322: Type 'string' is not assignable to type 'number'.


==== tests/cases/conformance/expressions/contextualTyping/getSetAccessorContextualTyping.ts (1 errors) ====
// In the body of a get accessor with no return type annotation,
// if a matching set accessor exists and that set accessor has a parameter type annotation,
// return expressions are contextually typed by the type given in the set accessor's parameter type annotation.

class C {
set X(x: number) { }
get X() {
return "string"; // Error; get contextual type by set accessor parameter type annotation
~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.
}

set Y(y) { }
get Y() {
return true;
}

set W(w) { }
get W(): boolean {
return true;
}

set Z(z: number) { }
get Z() {
return 1;
}
}
68 changes: 68 additions & 0 deletions tests/baselines/reference/getSetAccessorContextualTyping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//// [getSetAccessorContextualTyping.ts]
// In the body of a get accessor with no return type annotation,
// if a matching set accessor exists and that set accessor has a parameter type annotation,
// return expressions are contextually typed by the type given in the set accessor's parameter type annotation.

class C {
set X(x: number) { }
get X() {
return "string"; // Error; get contextual type by set accessor parameter type annotation
}

set Y(y) { }
get Y() {
return true;
}

set W(w) { }
get W(): boolean {
return true;
}

set Z(z: number) { }
get Z() {
return 1;
}
}

//// [getSetAccessorContextualTyping.js]
// In the body of a get accessor with no return type annotation,
// if a matching set accessor exists and that set accessor has a parameter type annotation,
// return expressions are contextually typed by the type given in the set accessor's parameter type annotation.
var C = (function () {
function C() {
}
Object.defineProperty(C.prototype, "X", {
get: function () {
return "string"; // Error; get contextual type by set accessor parameter type annotation
},
set: function (x) { },
enumerable: true,
configurable: true
});
Object.defineProperty(C.prototype, "Y", {
get: function () {
return true;
},
set: function (y) { },
enumerable: true,
configurable: true
});
Object.defineProperty(C.prototype, "W", {
get: function () {
return true;
},
set: function (w) { },
enumerable: true,
configurable: true
});
Object.defineProperty(C.prototype, "Z", {
get: function () {
return 1;
},
set: function (z) { },
enumerable: true,
configurable: true
});
return C;
})();
10 changes: 8 additions & 2 deletions tests/baselines/reference/objectLiteralContextualTyping.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//// [objectLiteralContextualTyping.ts]
// Tests related to #1774
// In a contextually typed object literal, each property value expression is contextually typed by
// the type of the property with a matching name in the contextual type, if any, or otherwise
// for a numerically named property, the numeric index type of the contextual type, if any, or otherwise
// the string index type of the contextual type, if any.

interface Item {
name: string;
Expand Down Expand Up @@ -28,7 +31,10 @@ var b: {};


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