Skip to content

Commit 4de2060

Browse files
committed
show error for target lower than esnext with useddefineforclassfields enabled
1 parent 17029a6 commit 4de2060

4 files changed

+15
-4
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2210,7 +2210,7 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
22102210
// 1. When result is undefined, after checking for a missing "this."
22112211
// 2. When result is defined
22122212
function checkAndReportErrorForInvalidInitializer() {
2213-
if (propertyWithInvalidInitializer && !useDefineForClassFields) {
2213+
if (propertyWithInvalidInitializer && !(useDefineForClassFields && getEmitScriptTarget(compilerOptions) >= ScriptTarget.ESNext)) {
22142214
// We have a match, but the reference occurred within a property initializer and the identifier also binds
22152215
// to a local variable in the constructor where the code will be emitted. Note that this is actually allowed
22162216
// with ESNext+useDefineForClassFields because the scope semantics are different.
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+

tests/baselines/reference/classMemberInitializerScoping2(target=es2017,usedefineforclassfields=true).symbols

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class C {
77

88
p = x
99
>p : Symbol(C.p, Decl(classMemberInitializerScoping2.ts, 1, 9))
10-
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 0, 5))
1110

1211
constructor(x: string) { }
1312
>x : Symbol(x, Decl(classMemberInitializerScoping2.ts, 3, 16))

tests/baselines/reference/classMemberInitializerScoping2(target=es2017,usedefineforclassfields=true).types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class C {
77
>C : C
88

99
p = x
10-
>p : number
11-
>x : 1
10+
>p : any
11+
>x : any
1212

1313
constructor(x: string) { }
1414
>x : string

0 commit comments

Comments
 (0)