Skip to content

Commit 0d4414d

Browse files
Allow this in constructor parameter defaults when legal (#57682)
1 parent ac962ea commit 0d4414d

10 files changed

+103
-74
lines changed

src/compiler/checker.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29815,12 +29815,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2981529815
error(node, Diagnostics.this_cannot_be_referenced_in_current_location);
2981629816
// do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
2981729817
break;
29818-
case SyntaxKind.Constructor:
29819-
if (isInConstructorArgumentInitializer(node, container)) {
29820-
error(node, Diagnostics.this_cannot_be_referenced_in_constructor_arguments);
29821-
// do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
29822-
}
29823-
break;
2982429818
}
2982529819
}
2982629820

src/compiler/diagnosticMessages.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,10 +1836,6 @@
18361836
"category": "Error",
18371837
"code": 2332
18381838
},
1839-
"'this' cannot be referenced in constructor arguments.": {
1840-
"category": "Error",
1841-
"code": 2333
1842-
},
18431839
"'this' cannot be referenced in a static property initializer.": {
18441840
"category": "Error",
18451841
"code": 2334
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
constructorDefaultValuesReferencingThis.ts(2,21): error TS2333: 'this' cannot be referenced in constructor arguments.
2-
constructorDefaultValuesReferencingThis.ts(6,21): error TS2333: 'this' cannot be referenced in constructor arguments.
3-
constructorDefaultValuesReferencingThis.ts(10,28): error TS2333: 'this' cannot be referenced in constructor arguments.
1+
constructorDefaultValuesReferencingThis.ts(15,21): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
42

53

6-
==== constructorDefaultValuesReferencingThis.ts (3 errors) ====
4+
==== constructorDefaultValuesReferencingThis.ts (1 errors) ====
75
class C {
6+
public baseProp = 1;
87
constructor(x = this) { }
9-
~~~~
10-
!!! error TS2333: 'this' cannot be referenced in constructor arguments.
118
}
129

1310
class D<T> {
1411
constructor(x = this) { }
15-
~~~~
16-
!!! error TS2333: 'this' cannot be referenced in constructor arguments.
1712
}
1813

1914
class E<T> {
2015
constructor(public x = this) { }
21-
~~~~
22-
!!! error TS2333: 'this' cannot be referenced in constructor arguments.
23-
}
16+
}
17+
18+
class F extends C {
19+
constructor(y = this.baseProp) {
20+
~~~~
21+
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
22+
super();
23+
}
24+
}
25+

tests/baselines/reference/constructorDefaultValuesReferencingThis.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
//// [constructorDefaultValuesReferencingThis.ts]
44
class C {
5+
public baseProp = 1;
56
constructor(x = this) { }
67
}
78

@@ -11,12 +12,35 @@ class D<T> {
1112

1213
class E<T> {
1314
constructor(public x = this) { }
14-
}
15+
}
16+
17+
class F extends C {
18+
constructor(y = this.baseProp) {
19+
super();
20+
}
21+
}
22+
1523

1624
//// [constructorDefaultValuesReferencingThis.js]
25+
var __extends = (this && this.__extends) || (function () {
26+
var extendStatics = function (d, b) {
27+
extendStatics = Object.setPrototypeOf ||
28+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
29+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
30+
return extendStatics(d, b);
31+
};
32+
return function (d, b) {
33+
if (typeof b !== "function" && b !== null)
34+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
35+
extendStatics(d, b);
36+
function __() { this.constructor = d; }
37+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
38+
};
39+
})();
1740
var C = /** @class */ (function () {
1841
function C(x) {
1942
if (x === void 0) { x = this; }
43+
this.baseProp = 1;
2044
}
2145
return C;
2246
}());
@@ -33,3 +57,11 @@ var E = /** @class */ (function () {
3357
}
3458
return E;
3559
}());
60+
var F = /** @class */ (function (_super) {
61+
__extends(F, _super);
62+
function F(y) {
63+
if (y === void 0) { y = _this.baseProp; }
64+
return _super.call(this) || this;
65+
}
66+
return F;
67+
}(C));

tests/baselines/reference/constructorDefaultValuesReferencingThis.symbols

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,44 @@
44
class C {
55
>C : Symbol(C, Decl(constructorDefaultValuesReferencingThis.ts, 0, 0))
66

7+
public baseProp = 1;
8+
>baseProp : Symbol(C.baseProp, Decl(constructorDefaultValuesReferencingThis.ts, 0, 9))
9+
710
constructor(x = this) { }
8-
>x : Symbol(x, Decl(constructorDefaultValuesReferencingThis.ts, 1, 16))
11+
>x : Symbol(x, Decl(constructorDefaultValuesReferencingThis.ts, 2, 16))
912
>this : Symbol(C, Decl(constructorDefaultValuesReferencingThis.ts, 0, 0))
1013
}
1114

1215
class D<T> {
13-
>D : Symbol(D, Decl(constructorDefaultValuesReferencingThis.ts, 2, 1))
14-
>T : Symbol(T, Decl(constructorDefaultValuesReferencingThis.ts, 4, 8))
16+
>D : Symbol(D, Decl(constructorDefaultValuesReferencingThis.ts, 3, 1))
17+
>T : Symbol(T, Decl(constructorDefaultValuesReferencingThis.ts, 5, 8))
1518

1619
constructor(x = this) { }
17-
>x : Symbol(x, Decl(constructorDefaultValuesReferencingThis.ts, 5, 16))
18-
>this : Symbol(D, Decl(constructorDefaultValuesReferencingThis.ts, 2, 1))
20+
>x : Symbol(x, Decl(constructorDefaultValuesReferencingThis.ts, 6, 16))
21+
>this : Symbol(D, Decl(constructorDefaultValuesReferencingThis.ts, 3, 1))
1922
}
2023

2124
class E<T> {
22-
>E : Symbol(E, Decl(constructorDefaultValuesReferencingThis.ts, 6, 1))
23-
>T : Symbol(T, Decl(constructorDefaultValuesReferencingThis.ts, 8, 8))
25+
>E : Symbol(E, Decl(constructorDefaultValuesReferencingThis.ts, 7, 1))
26+
>T : Symbol(T, Decl(constructorDefaultValuesReferencingThis.ts, 9, 8))
2427

2528
constructor(public x = this) { }
26-
>x : Symbol(E.x, Decl(constructorDefaultValuesReferencingThis.ts, 9, 16))
27-
>this : Symbol(E, Decl(constructorDefaultValuesReferencingThis.ts, 6, 1))
29+
>x : Symbol(E.x, Decl(constructorDefaultValuesReferencingThis.ts, 10, 16))
30+
>this : Symbol(E, Decl(constructorDefaultValuesReferencingThis.ts, 7, 1))
2831
}
32+
33+
class F extends C {
34+
>F : Symbol(F, Decl(constructorDefaultValuesReferencingThis.ts, 11, 1))
35+
>C : Symbol(C, Decl(constructorDefaultValuesReferencingThis.ts, 0, 0))
36+
37+
constructor(y = this.baseProp) {
38+
>y : Symbol(y, Decl(constructorDefaultValuesReferencingThis.ts, 14, 16))
39+
>this.baseProp : Symbol(C.baseProp, Decl(constructorDefaultValuesReferencingThis.ts, 0, 9))
40+
>this : Symbol(F, Decl(constructorDefaultValuesReferencingThis.ts, 11, 1))
41+
>baseProp : Symbol(C.baseProp, Decl(constructorDefaultValuesReferencingThis.ts, 0, 9))
42+
43+
super();
44+
>super : Symbol(C, Decl(constructorDefaultValuesReferencingThis.ts, 0, 0))
45+
}
46+
}
47+

tests/baselines/reference/constructorDefaultValuesReferencingThis.types

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
class C {
55
>C : C
66

7+
public baseProp = 1;
8+
>baseProp : number
9+
>1 : 1
10+
711
constructor(x = this) { }
812
>x : this
913
>this : this
@@ -24,3 +28,20 @@ class E<T> {
2428
>x : this
2529
>this : this
2630
}
31+
32+
class F extends C {
33+
>F : F
34+
>C : C
35+
36+
constructor(y = this.baseProp) {
37+
>y : number
38+
>this.baseProp : number
39+
>this : this
40+
>baseProp : number
41+
42+
super();
43+
>super() : void
44+
>super : typeof C
45+
}
46+
}
47+

tests/baselines/reference/thisInConstructorParameter1.errors.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/baselines/reference/thisInConstructorParameter2.errors.txt

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/baselines/reference/typeOfThisInConstructorParamList.errors.txt

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorDefaultValuesReferencingThis.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class C {
2+
public baseProp = 1;
23
constructor(x = this) { }
34
}
45

@@ -8,4 +9,10 @@ class D<T> {
89

910
class E<T> {
1011
constructor(public x = this) { }
11-
}
12+
}
13+
14+
class F extends C {
15+
constructor(y = this.baseProp) {
16+
super();
17+
}
18+
}

0 commit comments

Comments
 (0)