Skip to content

Commit 5d0e199

Browse files
authored
[Release 2.0] fix10076 : Destructing with "this" (#10209)
* Call checkExpression eventhough there is no appropriate type from destructuring of array * Add tests and baselines
1 parent 0cfeaaf commit 5d0e199

7 files changed

+88
-3
lines changed

src/compiler/checker.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -4308,9 +4308,14 @@ namespace ts {
43084308
return property;
43094309
}
43104310

4311-
// Return the symbol for the property with the given name in the given type. Creates synthetic union properties when
4312-
// necessary, maps primitive types and type parameters are to their apparent types, and augments with properties from
4313-
// Object and Function as appropriate.
4311+
/**
4312+
* Return the symbol for the property with the given name in the given type. Creates synthetic union properties when
4313+
* necessary, maps primitive types and type parameters are to their apparent types, and augments with properties from
4314+
* Object and Function as appropriate.
4315+
*
4316+
* @param type a type to look up property from
4317+
* @param name a name of property to look up in a given type
4318+
*/
43144319
function getPropertyOfType(type: Type, name: string): Symbol {
43154320
type = getApparentType(type);
43164321
if (type.flags & TypeFlags.ObjectType) {
@@ -12516,6 +12521,9 @@ namespace ts {
1251612521
return checkDestructuringAssignment(element, type, contextualMapper);
1251712522
}
1251812523
else {
12524+
// We still need to check element expression here because we may need to set appropriate flag on the expression
12525+
// such as NodeCheckFlags.LexicalThis on "this"expression.
12526+
checkExpression(element);
1251912527
if (isTupleType(sourceType)) {
1252012528
error(element, Diagnostics.Tuple_type_0_with_length_1_cannot_be_assigned_to_tuple_with_length_2, typeToString(sourceType), (<TupleType>sourceType).elementTypes.length, elements.length);
1252112529
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tests/cases/compiler/emitCapturingThisInTupleDestructuring1.ts(3,17): error TS2493: Tuple type '[any]' with length '1' cannot be assigned to tuple with length '3'.
2+
tests/cases/compiler/emitCapturingThisInTupleDestructuring1.ts(3,29): error TS2493: Tuple type '[any]' with length '1' cannot be assigned to tuple with length '3'.
3+
4+
5+
==== tests/cases/compiler/emitCapturingThisInTupleDestructuring1.ts (2 errors) ====
6+
declare function wrapper(x: any);
7+
wrapper((array: [any]) => {
8+
[this.test, this.test1, this.test2] = array; // even though there is a compiler error, we should still emit lexical capture for "this"
9+
~~~~~~~~~~
10+
!!! error TS2493: Tuple type '[any]' with length '1' cannot be assigned to tuple with length '3'.
11+
~~~~~~~~~~
12+
!!! error TS2493: Tuple type '[any]' with length '1' cannot be assigned to tuple with length '3'.
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [emitCapturingThisInTupleDestructuring1.ts]
2+
declare function wrapper(x: any);
3+
wrapper((array: [any]) => {
4+
[this.test, this.test1, this.test2] = array; // even though there is a compiler error, we should still emit lexical capture for "this"
5+
});
6+
7+
//// [emitCapturingThisInTupleDestructuring1.js]
8+
var _this = this;
9+
wrapper(function (array) {
10+
_this.test = array[0], _this.test1 = array[1], _this.test2 = array[2]; // even though there is a compiler error, we should still emit lexical capture for "this"
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
tests/cases/compiler/emitCapturingThisInTupleDestructuring2.ts(8,39): error TS2493: Tuple type '[number, number]' with length '2' cannot be assigned to tuple with length '3'.
2+
3+
4+
==== tests/cases/compiler/emitCapturingThisInTupleDestructuring2.ts (1 errors) ====
5+
var array1: [number, number] = [1, 2];
6+
7+
class B {
8+
test: number;
9+
test1: any;
10+
test2: any;
11+
method() {
12+
() => [this.test, this.test1, this.test2] = array1; // even though there is a compiler error, we should still emit lexical capture for "this"
13+
~~~~~~~~~~
14+
!!! error TS2493: Tuple type '[number, number]' with length '2' cannot be assigned to tuple with length '3'.
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [emitCapturingThisInTupleDestructuring2.ts]
2+
var array1: [number, number] = [1, 2];
3+
4+
class B {
5+
test: number;
6+
test1: any;
7+
test2: any;
8+
method() {
9+
() => [this.test, this.test1, this.test2] = array1; // even though there is a compiler error, we should still emit lexical capture for "this"
10+
}
11+
}
12+
13+
//// [emitCapturingThisInTupleDestructuring2.js]
14+
var array1 = [1, 2];
15+
var B = (function () {
16+
function B() {
17+
}
18+
B.prototype.method = function () {
19+
var _this = this;
20+
(function () { return (_this.test = array1[0], _this.test1 = array1[1], _this.test2 = array1[2], array1); }); // even though there is a compiler error, we should still emit lexical capture for "this"
21+
};
22+
return B;
23+
}());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare function wrapper(x: any);
2+
wrapper((array: [any]) => {
3+
[this.test, this.test1, this.test2] = array; // even though there is a compiler error, we should still emit lexical capture for "this"
4+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var array1: [number, number] = [1, 2];
2+
3+
class B {
4+
test: number;
5+
test1: any;
6+
test2: any;
7+
method() {
8+
() => [this.test, this.test1, this.test2] = array1; // even though there is a compiler error, we should still emit lexical capture for "this"
9+
}
10+
}

0 commit comments

Comments
 (0)