mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 04:43:37 -05:00
Merge pull request #2773 from Microsoft/conformanceDestructuringAssignment
Conformance test spec change in section 4.17.1 destructuring assignment and 5.1.2 variable declaration conformance tests
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
/* AssignmentPattern:
|
||||
* ObjectAssignmentPattern
|
||||
* ArrayAssignmentPattern
|
||||
* ArrayAssignmentPattern:
|
||||
* [Elision<opt> AssignmentRestElementopt ]
|
||||
* [AssignmentElementList]
|
||||
* [AssignmentElementList, Elision<opt> AssignmentRestElementopt ]
|
||||
* AssignmentElementList:
|
||||
* Elision<opt> AssignmentElement
|
||||
* AssignmentElementList, Elisionopt AssignmentElement
|
||||
* AssignmentElement:
|
||||
* LeftHandSideExpression Initialiseropt
|
||||
* AssignmentPattern Initialiseropt
|
||||
* AssignmentRestElement:
|
||||
* ... LeftHandSideExpression
|
||||
*/
|
||||
|
||||
// In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left.
|
||||
// An expression of type S is considered assignable to an assignment target V if one of the following is true
|
||||
|
||||
// V is an array assignment pattern, S is the type Any or an array-like type (section 3.3.2), and, for each assignment element E in V,
|
||||
// S is the type Any, or
|
||||
|
||||
var [a0, a1]: any = undefined;
|
||||
var [a2 = false, a3 = 1]: any = undefined;
|
||||
|
||||
// V is an array assignment pattern, S is the type Any or an array-like type (section 3.3.2), and, for each assignment element E in V,
|
||||
// S is a tuple- like type (section 3.3.3) with a property named N of a type that is assignable to the target given in E,
|
||||
// where N is the numeric index of E in the array assignment pattern, or
|
||||
var [b0, b1, b2] = [2, 3, 4];
|
||||
var [b3, b4, b5]: [number, number, string] = [1, 2, "string"];
|
||||
|
||||
function foo() {
|
||||
return [1, 2, 3];
|
||||
}
|
||||
|
||||
var [b6, b7] = foo();
|
||||
var [...b8] = foo();
|
||||
|
||||
// S is not a tuple- like type and the numeric index signature type of S is assignable to the target given in E.
|
||||
var temp = [1,2,3]
|
||||
var [c0, c1] = [...temp];
|
||||
var [c2] = [];
|
||||
var [[[c3]], [[[[c4]]]]] = [[[]], [[[[]]]]]
|
||||
var [[c5], c6]: [[string|number], boolean] = [[1], true];
|
||||
var [, c7] = [1, 2, 3];
|
||||
var [,,, c8] = [1, 2, 3, 4];
|
||||
var [,,, c9] = [1, 2, 3, 4];
|
||||
var [,,,...c10] = [1, 2, 3, 4, "hello"];
|
||||
var [c11, c12, ...c13] = [1, 2, "string"];
|
||||
var [c14, c15, c16] = [1, 2, "string"];
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
// @target: es6
|
||||
|
||||
/* AssignmentPattern:
|
||||
* ObjectAssignmentPattern
|
||||
* ArrayAssignmentPattern
|
||||
* ArrayAssignmentPattern:
|
||||
* [Elision<opt> AssignmentRestElementopt ]
|
||||
* [AssignmentElementList]
|
||||
* [AssignmentElementList, Elision<opt> AssignmentRestElementopt ]
|
||||
* AssignmentElementList:
|
||||
* Elision<opt> AssignmentElement
|
||||
* AssignmentElementList, Elisionopt AssignmentElement
|
||||
* AssignmentElement:
|
||||
* LeftHandSideExpression Initialiseropt
|
||||
* AssignmentPattern Initialiseropt
|
||||
* AssignmentRestElement:
|
||||
* ... LeftHandSideExpression
|
||||
*/
|
||||
|
||||
// In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left.
|
||||
// An expression of type S is considered assignable to an assignment target V if one of the following is true
|
||||
|
||||
// V is an array assignment pattern, S is the type Any or an array-like type (section 3.3.2), and, for each assignment element E in V,
|
||||
// S is the type Any, or
|
||||
|
||||
var [a0, a1]: any = undefined;
|
||||
var [a2 = false, a3 = 1]: any = undefined;
|
||||
|
||||
// V is an array assignment pattern, S is the type Any or an array-like type (section 3.3.2), and, for each assignment element E in V,
|
||||
// S is a tuple- like type (section 3.3.3) with a property named N of a type that is assignable to the target given in E,
|
||||
// where N is the numeric index of E in the array assignment pattern, or
|
||||
var [b0, b1, b2] = [2, 3, 4];
|
||||
var [b3, b4, b5]: [number, number, string] = [1, 2, "string"];
|
||||
|
||||
function foo() {
|
||||
return [1, 2, 3];
|
||||
}
|
||||
|
||||
var [b6, b7] = foo();
|
||||
var [...b8] = foo();
|
||||
|
||||
// S is not a tuple- like type and the numeric index signature type of S is assignable to the target given in E.
|
||||
var temp = [1,2,3]
|
||||
var [c0, c1] = [...temp];
|
||||
var [c2] = [];
|
||||
var [[[c3]], [[[[c4]]]]] = [[[]], [[[[]]]]]
|
||||
var [[c5], c6]: [[string|number], boolean] = [[1], true];
|
||||
var [, c7] = [1, 2, 3];
|
||||
var [,,, c8] = [1, 2, 3, 4];
|
||||
var [,,, c9] = [1, 2, 3, 4];
|
||||
var [,,,...c10] = [1, 2, 3, 4, "hello"];
|
||||
var [c11, c12, ...c13] = [1, 2, "string"];
|
||||
var [c14, c15, c16] = [1, 2, "string"];
|
||||
@@ -0,0 +1,34 @@
|
||||
// V is an array assignment pattern, S is the type Any or an array-like type (section 3.3.2), and, for each assignment element E in V,
|
||||
// S is the type Any, or
|
||||
var [[a0], [[a1]]] = [] // Error
|
||||
var [[a2], [[a3]]] = undefined // Error
|
||||
|
||||
// V is an array assignment pattern, S is the type Any or an array-like type (section 3.3.2), and, for each assignment element E in V,
|
||||
// S is a tuple- like type (section 3.3.3) with a property named N of a type that is assignable to the target given in E,
|
||||
// where N is the numeric index of E in the array assignment pattern, or
|
||||
var [b0, b1, b2]: [number, boolean, string] = [1, 2, "string"]; // Error
|
||||
interface J extends Array<Number> {
|
||||
2: number;
|
||||
}
|
||||
|
||||
function bar(): J {
|
||||
return <[number, number, number]>[1, 2, 3];
|
||||
}
|
||||
var [b3 = "string", b4, b5] = bar(); // Error
|
||||
|
||||
// V is an array assignment pattern, S is the type Any or an array-like type (section 3.3.2), and, for each assignment element E in V,
|
||||
// S is not a tuple- like type and the numeric index signature type of S is assignable to the target given in E.
|
||||
var temp = [1, 2, 3]
|
||||
var [c0, c1]: [number, number] = [...temp]; // Error
|
||||
var [c2, c3]: [string, string] = [...temp]; // Error
|
||||
|
||||
interface F {
|
||||
[idx: number]: boolean
|
||||
}
|
||||
|
||||
function foo(idx: number): F {
|
||||
return {
|
||||
2: true
|
||||
}
|
||||
}
|
||||
var [c4, c5, c6] = foo(1); // Error
|
||||
@@ -0,0 +1,54 @@
|
||||
// In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left.
|
||||
// An expression of type S is considered assignable to an assignment target V if one of the following is true
|
||||
|
||||
// V is an object assignment pattern and, for each assignment property P in V,
|
||||
// S is the type Any, or
|
||||
var { a1 }: any = undefined;
|
||||
var { a2 }: any = {};
|
||||
|
||||
// V is an object assignment pattern and, for each assignment property P in V,
|
||||
// S has an apparent property with the property name specified in
|
||||
// P of a type that is assignable to the target given in P, or
|
||||
var { b1, } = { b1:1, };
|
||||
var { b2: { b21 } = { b21: "string" } } = { b2: { b21: "world" } };
|
||||
var {1: b3} = { 1: "string" };
|
||||
var {b4 = 1}: any = { b4: 100000 };
|
||||
var {b5: { b52 } } = { b5: { b52 } };
|
||||
|
||||
// V is an object assignment pattern and, for each assignment property P in V,
|
||||
// P specifies a numeric property name and S has a numeric index signature
|
||||
// of a type that is assignable to the target given in P, or
|
||||
|
||||
interface F {
|
||||
[idx: number]: boolean;
|
||||
}
|
||||
|
||||
function foo(): F {
|
||||
return {
|
||||
1: true
|
||||
};
|
||||
}
|
||||
|
||||
function bar(): F {
|
||||
return {
|
||||
2: true
|
||||
};
|
||||
}
|
||||
var {1: c0} = foo();
|
||||
var {1: c1} = bar();
|
||||
|
||||
// V is an object assignment pattern and, for each assignment property P in V,
|
||||
// S has a string index signature of a type that is assignable to the target given in P
|
||||
|
||||
interface F1 {
|
||||
[str: string]: number;
|
||||
}
|
||||
|
||||
function foo1(): F1 {
|
||||
return {
|
||||
"prop1": 2
|
||||
}
|
||||
}
|
||||
|
||||
var {"prop1": d1} = foo1();
|
||||
var {"prop2": d1} = foo1();
|
||||
@@ -0,0 +1,55 @@
|
||||
// @target: es6
|
||||
// In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left.
|
||||
// An expression of type S is considered assignable to an assignment target V if one of the following is true
|
||||
|
||||
// V is an object assignment pattern and, for each assignment property P in V,
|
||||
// S is the type Any, or
|
||||
var { a1 }: any = undefined;
|
||||
var { a2 }: any = {};
|
||||
|
||||
// V is an object assignment pattern and, for each assignment property P in V,
|
||||
// S has an apparent property with the property name specified in
|
||||
// P of a type that is assignable to the target given in P, or
|
||||
var { b1, } = { b1:1, };
|
||||
var { b2: { b21 } = { b21: "string" } } = { b2: { b21: "world" } };
|
||||
var {1: b3} = { 1: "string" };
|
||||
var {b4 = 1}: any = { b4: 100000 };
|
||||
var {b5: { b52 } } = { b5: { b52 } };
|
||||
|
||||
// V is an object assignment pattern and, for each assignment property P in V,
|
||||
// P specifies a numeric property name and S has a numeric index signature
|
||||
// of a type that is assignable to the target given in P, or
|
||||
|
||||
interface F {
|
||||
[idx: number]: boolean;
|
||||
}
|
||||
|
||||
function foo(): F {
|
||||
return {
|
||||
1: true
|
||||
};
|
||||
}
|
||||
|
||||
function bar(): F {
|
||||
return {
|
||||
2: true
|
||||
};
|
||||
}
|
||||
var {1: c0} = foo();
|
||||
var {1: c1} = bar();
|
||||
|
||||
// V is an object assignment pattern and, for each assignment property P in V,
|
||||
// S has a string index signature of a type that is assignable to the target given in P
|
||||
|
||||
interface F1 {
|
||||
[str: string]: number;
|
||||
}
|
||||
|
||||
function foo1(): F1 {
|
||||
return {
|
||||
"prop1": 2
|
||||
}
|
||||
}
|
||||
|
||||
var {"prop1": d1} = foo1();
|
||||
var {"prop2": d1} = foo1();
|
||||
@@ -0,0 +1,10 @@
|
||||
// Error
|
||||
var {h?} = { h?: 1 };
|
||||
var {i}: string | number = { i: 2 };
|
||||
var {i1}: string | number| {} = { i1: 2 };
|
||||
var { f2: {f21} = { f212: "string" } }: any = undefined;
|
||||
var { ...d1 } = {
|
||||
a: 1, b: 1, d1: 9, e: 10
|
||||
}
|
||||
var {1} = { 1 };
|
||||
var {"prop"} = { "prop": 1 };
|
||||
@@ -0,0 +1,40 @@
|
||||
// The type T associated with a destructuring variable declaration is determined as follows:
|
||||
// If the declaration includes a type annotation, T is that type.
|
||||
var {a1, a2}: { a1: number, a2: string } = { a1: 10, a2: "world" }
|
||||
var [a3, [[a4]], a5]: [number, [[string]], boolean] = [1, [["hello"]], true];
|
||||
|
||||
// The type T associated with a destructuring variable declaration is determined as follows:
|
||||
// Otherwise, if the declaration includes an initializer expression, T is the type of that initializer expression.
|
||||
var { b1: { b11 } = { b11: "string" } } = { b1: { b11: "world" } };
|
||||
var temp = { t1: true, t2: "false" };
|
||||
var [b2 = 3, b3 = true, b4 = temp] = [3, false, { t1: false, t2: "hello" }];
|
||||
var [b5 = 3, b6 = true, b7 = temp] = [undefined, undefined, undefined];
|
||||
|
||||
// The type T associated with a binding element is determined as follows:
|
||||
// If the binding element is a rest element, T is an array type with
|
||||
// an element type E, where E is the type of the numeric index signature of S.
|
||||
var [...c1] = [1,2,3];
|
||||
var [...c2] = [1,2,3, "string"];
|
||||
|
||||
// The type T associated with a binding element is determined as follows:
|
||||
// Otherwise, if S is a tuple- like type (section 3.3.3):
|
||||
// Let N be the zero-based index of the binding element in the array binding pattern.
|
||||
// If S has a property with the numerical name N, T is the type of that property.
|
||||
var [d1,d2] = [1,"string"]
|
||||
|
||||
// The type T associated with a binding element is determined as follows:
|
||||
// Otherwise, if S is a tuple- like type (section 3.3.3):
|
||||
// Otherwise, if S has a numeric index signature, T is the type of the numeric index signature.
|
||||
var temp1 = [true, false, true]
|
||||
var [d3, d4] = [1, "string", ...temp1];
|
||||
|
||||
// Combining both forms of destructuring,
|
||||
var {e: [e1, e2, e3 = { b1: 1000, b4: 200 }]} = { e: [1, 2, { b1: 4, b4: 0 }] };
|
||||
var {f: [f1, f2, { f3: f4, f5 }, , ]} = { f: [1, 2, { f3: 4, f5: 0 }] };
|
||||
|
||||
// When a destructuring variable declaration, binding property, or binding element specifies
|
||||
// an initializer expression, the type of the initializer expression is required to be assignable
|
||||
// to the widened form of the type associated with the destructuring variable declaration, binding property, or binding element.
|
||||
var {g: {g1 = [undefined, null]}}: { g: { g1: any[] } } = { g: { g1: [1, 2] } };
|
||||
var {h: {h1 = [undefined, null]}}: { h: { h1: number[] } } = { h: { h1: [1, 2] } };
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
// @target: es6
|
||||
// The type T associated with a destructuring variable declaration is determined as follows:
|
||||
// If the declaration includes a type annotation, T is that type.
|
||||
var {a1, a2}: { a1: number, a2: string } = { a1: 10, a2: "world" }
|
||||
var [a3, [[a4]], a5]: [number, [[string]], boolean] = [1, [["hello"]], true];
|
||||
|
||||
// The type T associated with a destructuring variable declaration is determined as follows:
|
||||
// Otherwise, if the declaration includes an initializer expression, T is the type of that initializer expression.
|
||||
var { b1: { b11 } = { b11: "string" } } = { b1: { b11: "world" } };
|
||||
var temp = { t1: true, t2: "false" };
|
||||
var [b2 = 3, b3 = true, b4 = temp] = [3, false, { t1: false, t2: "hello" }];
|
||||
var [b5 = 3, b6 = true, b7 = temp] = [undefined, undefined, undefined];
|
||||
|
||||
// The type T associated with a binding element is determined as follows:
|
||||
// If the binding element is a rest element, T is an array type with
|
||||
// an element type E, where E is the type of the numeric index signature of S.
|
||||
var [...c1] = [1,2,3];
|
||||
var [...c2] = [1,2,3, "string"];
|
||||
|
||||
// The type T associated with a binding element is determined as follows:
|
||||
// Otherwise, if S is a tuple- like type (section 3.3.3):
|
||||
// Let N be the zero-based index of the binding element in the array binding pattern.
|
||||
// If S has a property with the numerical name N, T is the type of that property.
|
||||
var [d1,d2] = [1,"string"]
|
||||
|
||||
// The type T associated with a binding element is determined as follows:
|
||||
// Otherwise, if S is a tuple- like type (section 3.3.3):
|
||||
// Otherwise, if S has a numeric index signature, T is the type of the numeric index signature.
|
||||
var temp1 = [true, false, true]
|
||||
var [d3, d4] = [1, "string", ...temp1];
|
||||
|
||||
// Combining both forms of destructuring,
|
||||
var {e: [e1, e2, e3 = { b1: 1000, b4: 200 }]} = { e: [1, 2, { b1: 4, b4: 0 }] };
|
||||
var {f: [f1, f2, { f3: f4, f5 }, , ]} = { f: [1, 2, { f3: 4, f5: 0 }] };
|
||||
|
||||
// When a destructuring variable declaration, binding property, or binding element specifies
|
||||
// an initializer expression, the type of the initializer expression is required to be assignable
|
||||
// to the widened form of the type associated with the destructuring variable declaration, binding property, or binding element.
|
||||
var {g: {g1 = [undefined, null]}}: { g: { g1: any[] } } = { g: { g1: [1, 2] } };
|
||||
var {h: {h1 = [undefined, null]}}: { h: { h1: number[] } } = { h: { h1: [1, 2] } };
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// The type T associated with a destructuring variable declaration is determined as follows:
|
||||
// If the declaration includes a type annotation, T is that type.
|
||||
var {a1, a2}: { a1: number, a2: string } = { a1: true, a2: 1 } // Error
|
||||
var [a3, [[a4]], a5]: [number, [[string]], boolean] = [1, [[false]], true]; // Error
|
||||
|
||||
// The type T associated with a destructuring variable declaration is determined as follows:
|
||||
// Otherwise, if the declaration includes an initializer expression, T is the type of that initializer expression.
|
||||
var temp = { t1: true, t2: "false" };
|
||||
var [b0 = 3, b1 = true, b2 = temp] = [3, false, { t1: false, t2: 5}]; // Error
|
||||
|
||||
// The type T associated with a binding element is determined as follows:
|
||||
// If the binding element is a rest element, T is an array type with
|
||||
// an element type E, where E is the type of the numeric index signature of S.
|
||||
var [c1, c2, { c3: c4, c5 }, , ...c6] = [1, 2, { c3: 4, c5: 0 }]; // Error
|
||||
|
||||
// When a destructuring variable declaration, binding property, or binding element specifies
|
||||
// an initializer expression, the type of the initializer expression is required to be assignable
|
||||
// to the widened form of the type associated with the destructuring variable declaration, binding property, or binding element.
|
||||
var {d: {d1 = ["string", null]}}: { d: { d1: number[] } } = { d: { d1: [1, 2] } }; // Error
|
||||
Reference in New Issue
Block a user