Skip to content

Commit 7634369

Browse files
committed
fix(49719): omit TS2301 error with enabled usedefineforclassfields
1 parent 7e91485 commit 7634369

17 files changed

+222
-4
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,10 +2217,9 @@ namespace ts {
22172217
return undefined;
22182218
}
22192219

2220-
if (propertyWithInvalidInitializer && !(getEmitScriptTarget(compilerOptions) === ScriptTarget.ESNext && useDefineForClassFields)) {
2220+
if (propertyWithInvalidInitializer && !useDefineForClassFields) {
22212221
// We have a match, but the reference occurred within a property initializer and the identifier also binds
2222-
// to a local variable in the constructor where the code will be emitted. Note that this is actually allowed
2223-
// with ESNext+useDefineForClassFields because the scope semantics are different.
2222+
// to a local variable in the constructor where the code will be emitted.
22242223
const propertyName = (propertyWithInvalidInitializer as PropertyDeclaration).name;
22252224
error(errorLocation, Diagnostics.Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor,
22262225
declarationNameToString(propertyName), diagnosticName(nameArg!));

src/testRunner/compilerRunner.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ namespace Harness {
141141
"preserveConstEnums",
142142
"skipLibCheck",
143143
"exactOptionalPropertyTypes",
144-
"useUnknownInCatchVariables"
144+
"useDefineForClassFields",
145+
"useUnknownInCatchVariables",
145146
];
146147
private fileName: string;
147148
private justName: string;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/compiler/classMemberInitializerScoping2.ts(3,9): error TS2301: Initializer of instance member variable 'p' cannot reference identifier 'x' declared in the constructor.
2+
3+
4+
==== tests/cases/compiler/classMemberInitializerScoping2.ts (1 errors) ====
5+
const x = 1
6+
class C {
7+
p = x
8+
~
9+
!!! error TS2301: Initializer of instance member variable 'p' cannot reference identifier 'x' declared in the constructor.
10+
constructor(x: string) { }
11+
}
12+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [classMemberInitializerScoping2.ts]
2+
const x = 1
3+
class C {
4+
p = x
5+
constructor(x: string) { }
6+
}
7+
8+
9+
//// [classMemberInitializerScoping2.js]
10+
const x = 1;
11+
class C {
12+
constructor(x) {
13+
this.p = x;
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/classMemberInitializerScoping2.ts ===
2+
const x = 1
3+
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 0, 5))
4+
5+
class C {
6+
>C : Symbol(C, Decl(classMemberInitializerScoping2.ts, 0, 11))
7+
8+
p = x
9+
>p : Symbol(C.p, Decl(classMemberInitializerScoping2.ts, 1, 9))
10+
11+
constructor(x: string) { }
12+
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 3, 16))
13+
}
14+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/classMemberInitializerScoping2.ts ===
2+
const x = 1
3+
>x : 1
4+
>1 : 1
5+
6+
class C {
7+
>C : C
8+
9+
p = x
10+
>p : any
11+
>x : any
12+
13+
constructor(x: string) { }
14+
>x : string
15+
}
16+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [classMemberInitializerScoping2.ts]
2+
const x = 1
3+
class C {
4+
p = x
5+
constructor(x: string) { }
6+
}
7+
8+
9+
//// [classMemberInitializerScoping2.js]
10+
const x = 1;
11+
class C {
12+
constructor(x) {
13+
Object.defineProperty(this, "p", {
14+
enumerable: true,
15+
configurable: true,
16+
writable: true,
17+
value: x
18+
});
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/classMemberInitializerScoping2.ts ===
2+
const x = 1
3+
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 0, 5))
4+
5+
class C {
6+
>C : Symbol(C, Decl(classMemberInitializerScoping2.ts, 0, 11))
7+
8+
p = x
9+
>p : Symbol(C.p, Decl(classMemberInitializerScoping2.ts, 1, 9))
10+
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 0, 5))
11+
12+
constructor(x: string) { }
13+
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 3, 16))
14+
}
15+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/classMemberInitializerScoping2.ts ===
2+
const x = 1
3+
>x : 1
4+
>1 : 1
5+
6+
class C {
7+
>C : C
8+
9+
p = x
10+
>p : number
11+
>x : 1
12+
13+
constructor(x: string) { }
14+
>x : string
15+
}
16+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/compiler/classMemberInitializerScoping2.ts(3,9): error TS2301: Initializer of instance member variable 'p' cannot reference identifier 'x' declared in the constructor.
2+
3+
4+
==== tests/cases/compiler/classMemberInitializerScoping2.ts (1 errors) ====
5+
const x = 1
6+
class C {
7+
p = x
8+
~
9+
!!! error TS2301: Initializer of instance member variable 'p' cannot reference identifier 'x' declared in the constructor.
10+
constructor(x: string) { }
11+
}
12+

0 commit comments

Comments
 (0)