mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-09 20:51:43 -06:00
Merge pull request #2814 from Microsoft/conformanceContextuallyTypedExp
Conformance test for 4.19 Contextually Typed Expressions
This commit is contained in:
commit
80103b086a
@ -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)[]'.
|
||||
@ -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)[]
|
||||
@ -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[]'.
|
||||
|
||||
@ -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
|
||||
@ -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
tests/baselines/reference/getSetAccessorContextualTyping.js
Normal file
68
tests/baselines/reference/getSetAccessorContextualTyping.js
Normal file
@ -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;
|
||||
})();
|
||||
@ -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;
|
||||
@ -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" });
|
||||
|
||||
@ -1,71 +1,74 @@
|
||||
=== tests/cases/conformance/expressions/contextualTyping/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 {
|
||||
>Item : Symbol(Item, Decl(objectLiteralContextualTyping.ts, 0, 0))
|
||||
|
||||
name: string;
|
||||
>name : Symbol(name, Decl(objectLiteralContextualTyping.ts, 2, 16))
|
||||
>name : Symbol(name, Decl(objectLiteralContextualTyping.ts, 5, 16))
|
||||
|
||||
description?: string;
|
||||
>description : Symbol(description, Decl(objectLiteralContextualTyping.ts, 3, 17))
|
||||
>description : Symbol(description, Decl(objectLiteralContextualTyping.ts, 6, 17))
|
||||
}
|
||||
|
||||
declare function foo(item: Item): string;
|
||||
>foo : Symbol(foo, Decl(objectLiteralContextualTyping.ts, 5, 1), Decl(objectLiteralContextualTyping.ts, 7, 41))
|
||||
>item : Symbol(item, Decl(objectLiteralContextualTyping.ts, 7, 21))
|
||||
>foo : Symbol(foo, Decl(objectLiteralContextualTyping.ts, 8, 1), Decl(objectLiteralContextualTyping.ts, 10, 41))
|
||||
>item : Symbol(item, Decl(objectLiteralContextualTyping.ts, 10, 21))
|
||||
>Item : Symbol(Item, Decl(objectLiteralContextualTyping.ts, 0, 0))
|
||||
|
||||
declare function foo(item: any): number;
|
||||
>foo : Symbol(foo, Decl(objectLiteralContextualTyping.ts, 5, 1), Decl(objectLiteralContextualTyping.ts, 7, 41))
|
||||
>item : Symbol(item, Decl(objectLiteralContextualTyping.ts, 8, 21))
|
||||
>foo : Symbol(foo, Decl(objectLiteralContextualTyping.ts, 8, 1), Decl(objectLiteralContextualTyping.ts, 10, 41))
|
||||
>item : Symbol(item, Decl(objectLiteralContextualTyping.ts, 11, 21))
|
||||
|
||||
var x = foo({ name: "Sprocket" });
|
||||
>x : Symbol(x, Decl(objectLiteralContextualTyping.ts, 10, 3), Decl(objectLiteralContextualTyping.ts, 11, 3))
|
||||
>foo : Symbol(foo, Decl(objectLiteralContextualTyping.ts, 5, 1), Decl(objectLiteralContextualTyping.ts, 7, 41))
|
||||
>name : Symbol(name, Decl(objectLiteralContextualTyping.ts, 10, 13))
|
||||
>x : Symbol(x, Decl(objectLiteralContextualTyping.ts, 13, 3), Decl(objectLiteralContextualTyping.ts, 14, 3))
|
||||
>foo : Symbol(foo, Decl(objectLiteralContextualTyping.ts, 8, 1), Decl(objectLiteralContextualTyping.ts, 10, 41))
|
||||
>name : Symbol(name, Decl(objectLiteralContextualTyping.ts, 13, 13))
|
||||
|
||||
var x: string;
|
||||
>x : Symbol(x, Decl(objectLiteralContextualTyping.ts, 10, 3), Decl(objectLiteralContextualTyping.ts, 11, 3))
|
||||
>x : Symbol(x, Decl(objectLiteralContextualTyping.ts, 13, 3), Decl(objectLiteralContextualTyping.ts, 14, 3))
|
||||
|
||||
var y = foo({ name: "Sprocket", description: "Bumpy wheel" });
|
||||
>y : Symbol(y, Decl(objectLiteralContextualTyping.ts, 13, 3), Decl(objectLiteralContextualTyping.ts, 14, 3))
|
||||
>foo : Symbol(foo, Decl(objectLiteralContextualTyping.ts, 5, 1), Decl(objectLiteralContextualTyping.ts, 7, 41))
|
||||
>name : Symbol(name, Decl(objectLiteralContextualTyping.ts, 13, 13))
|
||||
>description : Symbol(description, Decl(objectLiteralContextualTyping.ts, 13, 31))
|
||||
|
||||
var y: string;
|
||||
>y : Symbol(y, Decl(objectLiteralContextualTyping.ts, 13, 3), Decl(objectLiteralContextualTyping.ts, 14, 3))
|
||||
|
||||
var z = foo({ name: "Sprocket", description: false });
|
||||
>z : Symbol(z, Decl(objectLiteralContextualTyping.ts, 16, 3), Decl(objectLiteralContextualTyping.ts, 17, 3))
|
||||
>foo : Symbol(foo, Decl(objectLiteralContextualTyping.ts, 5, 1), Decl(objectLiteralContextualTyping.ts, 7, 41))
|
||||
>y : Symbol(y, Decl(objectLiteralContextualTyping.ts, 16, 3), Decl(objectLiteralContextualTyping.ts, 17, 3))
|
||||
>foo : Symbol(foo, Decl(objectLiteralContextualTyping.ts, 8, 1), Decl(objectLiteralContextualTyping.ts, 10, 41))
|
||||
>name : Symbol(name, Decl(objectLiteralContextualTyping.ts, 16, 13))
|
||||
>description : Symbol(description, Decl(objectLiteralContextualTyping.ts, 16, 31))
|
||||
|
||||
var y: string;
|
||||
>y : Symbol(y, Decl(objectLiteralContextualTyping.ts, 16, 3), Decl(objectLiteralContextualTyping.ts, 17, 3))
|
||||
|
||||
var z = foo({ name: "Sprocket", description: false });
|
||||
>z : Symbol(z, Decl(objectLiteralContextualTyping.ts, 19, 3), Decl(objectLiteralContextualTyping.ts, 20, 3))
|
||||
>foo : Symbol(foo, Decl(objectLiteralContextualTyping.ts, 8, 1), Decl(objectLiteralContextualTyping.ts, 10, 41))
|
||||
>name : Symbol(name, Decl(objectLiteralContextualTyping.ts, 19, 13))
|
||||
>description : Symbol(description, Decl(objectLiteralContextualTyping.ts, 19, 31))
|
||||
|
||||
var z: number;
|
||||
>z : Symbol(z, Decl(objectLiteralContextualTyping.ts, 16, 3), Decl(objectLiteralContextualTyping.ts, 17, 3))
|
||||
>z : Symbol(z, Decl(objectLiteralContextualTyping.ts, 19, 3), Decl(objectLiteralContextualTyping.ts, 20, 3))
|
||||
|
||||
var w = foo({ a: 10 });
|
||||
>w : Symbol(w, Decl(objectLiteralContextualTyping.ts, 19, 3), Decl(objectLiteralContextualTyping.ts, 20, 3))
|
||||
>foo : Symbol(foo, Decl(objectLiteralContextualTyping.ts, 5, 1), Decl(objectLiteralContextualTyping.ts, 7, 41))
|
||||
>a : Symbol(a, Decl(objectLiteralContextualTyping.ts, 19, 13))
|
||||
>w : Symbol(w, Decl(objectLiteralContextualTyping.ts, 22, 3), Decl(objectLiteralContextualTyping.ts, 23, 3))
|
||||
>foo : Symbol(foo, Decl(objectLiteralContextualTyping.ts, 8, 1), Decl(objectLiteralContextualTyping.ts, 10, 41))
|
||||
>a : Symbol(a, Decl(objectLiteralContextualTyping.ts, 22, 13))
|
||||
|
||||
var w: number;
|
||||
>w : Symbol(w, Decl(objectLiteralContextualTyping.ts, 19, 3), Decl(objectLiteralContextualTyping.ts, 20, 3))
|
||||
>w : Symbol(w, Decl(objectLiteralContextualTyping.ts, 22, 3), Decl(objectLiteralContextualTyping.ts, 23, 3))
|
||||
|
||||
declare function bar<T>(param: { x?: T }): T;
|
||||
>bar : Symbol(bar, Decl(objectLiteralContextualTyping.ts, 20, 14))
|
||||
>T : Symbol(T, Decl(objectLiteralContextualTyping.ts, 22, 21))
|
||||
>param : Symbol(param, Decl(objectLiteralContextualTyping.ts, 22, 24))
|
||||
>x : Symbol(x, Decl(objectLiteralContextualTyping.ts, 22, 32))
|
||||
>T : Symbol(T, Decl(objectLiteralContextualTyping.ts, 22, 21))
|
||||
>T : Symbol(T, Decl(objectLiteralContextualTyping.ts, 22, 21))
|
||||
>bar : Symbol(bar, Decl(objectLiteralContextualTyping.ts, 23, 14))
|
||||
>T : Symbol(T, Decl(objectLiteralContextualTyping.ts, 25, 21))
|
||||
>param : Symbol(param, Decl(objectLiteralContextualTyping.ts, 25, 24))
|
||||
>x : Symbol(x, Decl(objectLiteralContextualTyping.ts, 25, 32))
|
||||
>T : Symbol(T, Decl(objectLiteralContextualTyping.ts, 25, 21))
|
||||
>T : Symbol(T, Decl(objectLiteralContextualTyping.ts, 25, 21))
|
||||
|
||||
var b = bar({});
|
||||
>b : Symbol(b, Decl(objectLiteralContextualTyping.ts, 24, 3), Decl(objectLiteralContextualTyping.ts, 25, 3))
|
||||
>bar : Symbol(bar, Decl(objectLiteralContextualTyping.ts, 20, 14))
|
||||
>b : Symbol(b, Decl(objectLiteralContextualTyping.ts, 27, 3), Decl(objectLiteralContextualTyping.ts, 28, 3))
|
||||
>bar : Symbol(bar, Decl(objectLiteralContextualTyping.ts, 23, 14))
|
||||
|
||||
var b: {};
|
||||
>b : Symbol(b, Decl(objectLiteralContextualTyping.ts, 24, 3), Decl(objectLiteralContextualTyping.ts, 25, 3))
|
||||
>b : Symbol(b, Decl(objectLiteralContextualTyping.ts, 27, 3), Decl(objectLiteralContextualTyping.ts, 28, 3))
|
||||
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
=== tests/cases/conformance/expressions/contextualTyping/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 {
|
||||
>Item : Item
|
||||
|
||||
@ -0,0 +1,18 @@
|
||||
// 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)[]
|
||||
@ -0,0 +1,14 @@
|
||||
// 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
|
||||
@ -0,0 +1,26 @@
|
||||
// @target: es5
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,7 @@
|
||||
// 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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user