Skip to content

Commit 5702941

Browse files
authored
fix(49719): Incorrect error 2301 when using ES standard class properties (#49725)
* fix(49719): omit TS2301 error with enabled usedefineforclassfields * show error for target lower than esnext with useddefineforclassfields enabled * change target from esnext to es2022
1 parent 2f51a9e commit 5702941

18 files changed

+232
-2
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,7 @@ namespace ts {
21912191
// 1. When result is undefined, after checking for a missing "this."
21922192
// 2. When result is defined
21932193
function checkAndReportErrorForInvalidInitializer() {
2194-
if (propertyWithInvalidInitializer && !(getEmitScriptTarget(compilerOptions) === ScriptTarget.ESNext && useDefineForClassFields)) {
2194+
if (propertyWithInvalidInitializer && !(useDefineForClassFields && getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2022)) {
21952195
// We have a match, but the reference occurred within a property initializer and the identifier also binds
21962196
// to a local variable in the constructor where the code will be emitted. Note that this is actually allowed
21972197
// with ESNext+useDefineForClassFields because the scope semantics are different.

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;
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+
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+
}
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+
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+
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: 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+
}
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+
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+
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+
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+
}
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+
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
p = x;
13+
constructor(x) { }
14+
}
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+
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @target: es2017,esnext
2+
// @useDefineForClassFields: true,false
3+
4+
const x = 1
5+
class C {
6+
p = x
7+
constructor(x: string) { }
8+
}

0 commit comments

Comments
 (0)