Skip to content

Commit ec1205c

Browse files
authored
Fix/issue 53286 (#53885)
1 parent 632ad0b commit ec1205c

6 files changed

+574
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2814,7 +2814,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
28142814
return true;
28152815
}
28162816
if (isUsedInFunctionOrInstanceProperty(usage, declaration)) {
2817-
if (getEmitScriptTarget(compilerOptions) === ScriptTarget.ESNext && useDefineForClassFields
2817+
if (getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2022 && useDefineForClassFields
28182818
&& getContainingClass(declaration)
28192819
&& (isPropertyDeclaration(declaration) || isParameterPropertyDeclaration(declaration, declaration.parent))) {
28202820
return !isPropertyImmediatelyReferencedWithinDeclaration(declaration, usage, /*stopAtAnyPropertyDeclaration*/ true);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts(2,16): error TS2729: Property 'bar' is used before its initialization.
2+
tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts(3,16): error TS2729: Property 'foo' is used before its initialization.
3+
tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts(6,19): error TS2729: Property 'm3' is used before its initialization.
4+
tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts(12,17): error TS2729: Property 'baz' is used before its initialization.
5+
6+
7+
==== tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts (4 errors) ====
8+
class C {
9+
qux = this.bar // should error
10+
~~~
11+
!!! error TS2729: Property 'bar' is used before its initialization.
12+
!!! related TS2728 tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts:3:5: 'bar' is declared here.
13+
bar = this.foo // should error
14+
~~~
15+
!!! error TS2729: Property 'foo' is used before its initialization.
16+
!!! related TS2728 tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts:11:17: 'foo' is declared here.
17+
quiz = this.bar // ok
18+
quench = this.m1() // ok
19+
quanch = this.m3() // should error
20+
~~
21+
!!! error TS2729: Property 'm3' is used before its initialization.
22+
!!! related TS2728 tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts:10:5: 'm3' is declared here.
23+
m1() {
24+
this.foo // ok
25+
}
26+
m3 = function() { }
27+
constructor(public foo: string) {}
28+
quim = this.baz // should error
29+
~~~
30+
!!! error TS2729: Property 'baz' is used before its initialization.
31+
!!! related TS2728 tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts:13:5: 'baz' is declared here.
32+
baz = this.foo; // should error
33+
quid = this.baz // ok
34+
m2() {
35+
this.foo // ok
36+
}
37+
}
38+
39+
class D extends C {
40+
quill = this.foo // ok
41+
}
42+
43+
class E {
44+
bar = () => this.foo1 + this.foo2; // both ok
45+
foo1 = '';
46+
constructor(public foo2: string) {}
47+
}
48+
49+
class F {
50+
Inner = class extends F {
51+
p2 = this.p1
52+
}
53+
p1 = 0
54+
}
55+
class G {
56+
Inner = class extends G {
57+
p2 = this.p1
58+
}
59+
constructor(public p1: number) {}
60+
}
61+
class H {
62+
constructor(public p1: C) {}
63+
64+
public p2 = () => {
65+
return this.p1.foo;
66+
}
67+
68+
public p3 = () => this.p1.foo;
69+
}
70+
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//// [assignParameterPropertyToPropertyDeclarationES2022.ts]
2+
class C {
3+
qux = this.bar // should error
4+
bar = this.foo // should error
5+
quiz = this.bar // ok
6+
quench = this.m1() // ok
7+
quanch = this.m3() // should error
8+
m1() {
9+
this.foo // ok
10+
}
11+
m3 = function() { }
12+
constructor(public foo: string) {}
13+
quim = this.baz // should error
14+
baz = this.foo; // should error
15+
quid = this.baz // ok
16+
m2() {
17+
this.foo // ok
18+
}
19+
}
20+
21+
class D extends C {
22+
quill = this.foo // ok
23+
}
24+
25+
class E {
26+
bar = () => this.foo1 + this.foo2; // both ok
27+
foo1 = '';
28+
constructor(public foo2: string) {}
29+
}
30+
31+
class F {
32+
Inner = class extends F {
33+
p2 = this.p1
34+
}
35+
p1 = 0
36+
}
37+
class G {
38+
Inner = class extends G {
39+
p2 = this.p1
40+
}
41+
constructor(public p1: number) {}
42+
}
43+
class H {
44+
constructor(public p1: C) {}
45+
46+
public p2 = () => {
47+
return this.p1.foo;
48+
}
49+
50+
public p3 = () => this.p1.foo;
51+
}
52+
53+
54+
//// [assignParameterPropertyToPropertyDeclarationES2022.js]
55+
class C {
56+
foo;
57+
qux = this.bar; // should error
58+
bar = this.foo; // should error
59+
quiz = this.bar; // ok
60+
quench = this.m1(); // ok
61+
quanch = this.m3(); // should error
62+
m1() {
63+
this.foo; // ok
64+
}
65+
m3 = function () { };
66+
constructor(foo) {
67+
this.foo = foo;
68+
}
69+
quim = this.baz; // should error
70+
baz = this.foo; // should error
71+
quid = this.baz; // ok
72+
m2() {
73+
this.foo; // ok
74+
}
75+
}
76+
class D extends C {
77+
quill = this.foo; // ok
78+
}
79+
class E {
80+
foo2;
81+
bar = () => this.foo1 + this.foo2; // both ok
82+
foo1 = '';
83+
constructor(foo2) {
84+
this.foo2 = foo2;
85+
}
86+
}
87+
class F {
88+
Inner = class extends F {
89+
p2 = this.p1;
90+
};
91+
p1 = 0;
92+
}
93+
class G {
94+
p1;
95+
Inner = class extends G {
96+
p2 = this.p1;
97+
};
98+
constructor(p1) {
99+
this.p1 = p1;
100+
}
101+
}
102+
class H {
103+
p1;
104+
constructor(p1) {
105+
this.p1 = p1;
106+
}
107+
p2 = () => {
108+
return this.p1.foo;
109+
};
110+
p3 = () => this.p1.foo;
111+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
=== tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationES2022.ts ===
2+
class C {
3+
>C : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
4+
5+
qux = this.bar // should error
6+
>qux : Symbol(C.qux, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 9))
7+
>this.bar : Symbol(C.bar, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 1, 18))
8+
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
9+
>bar : Symbol(C.bar, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 1, 18))
10+
11+
bar = this.foo // should error
12+
>bar : Symbol(C.bar, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 1, 18))
13+
>this.foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
14+
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
15+
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
16+
17+
quiz = this.bar // ok
18+
>quiz : Symbol(C.quiz, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 2, 18))
19+
>this.bar : Symbol(C.bar, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 1, 18))
20+
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
21+
>bar : Symbol(C.bar, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 1, 18))
22+
23+
quench = this.m1() // ok
24+
>quench : Symbol(C.quench, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 3, 19))
25+
>this.m1 : Symbol(C.m1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 5, 22))
26+
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
27+
>m1 : Symbol(C.m1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 5, 22))
28+
29+
quanch = this.m3() // should error
30+
>quanch : Symbol(C.quanch, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 4, 22))
31+
>this.m3 : Symbol(C.m3, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 8, 5))
32+
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
33+
>m3 : Symbol(C.m3, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 8, 5))
34+
35+
m1() {
36+
>m1 : Symbol(C.m1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 5, 22))
37+
38+
this.foo // ok
39+
>this.foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
40+
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
41+
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
42+
}
43+
m3 = function() { }
44+
>m3 : Symbol(C.m3, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 8, 5))
45+
46+
constructor(public foo: string) {}
47+
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
48+
49+
quim = this.baz // should error
50+
>quim : Symbol(C.quim, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 38))
51+
>this.baz : Symbol(C.baz, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 11, 19))
52+
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
53+
>baz : Symbol(C.baz, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 11, 19))
54+
55+
baz = this.foo; // should error
56+
>baz : Symbol(C.baz, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 11, 19))
57+
>this.foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
58+
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
59+
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
60+
61+
quid = this.baz // ok
62+
>quid : Symbol(C.quid, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 12, 19))
63+
>this.baz : Symbol(C.baz, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 11, 19))
64+
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
65+
>baz : Symbol(C.baz, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 11, 19))
66+
67+
m2() {
68+
>m2 : Symbol(C.m2, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 13, 19))
69+
70+
this.foo // ok
71+
>this.foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
72+
>this : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
73+
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
74+
}
75+
}
76+
77+
class D extends C {
78+
>D : Symbol(D, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 17, 1))
79+
>C : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
80+
81+
quill = this.foo // ok
82+
>quill : Symbol(D.quill, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 19, 19))
83+
>this.foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
84+
>this : Symbol(D, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 17, 1))
85+
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
86+
}
87+
88+
class E {
89+
>E : Symbol(E, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 21, 1))
90+
91+
bar = () => this.foo1 + this.foo2; // both ok
92+
>bar : Symbol(E.bar, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 23, 9))
93+
>this.foo1 : Symbol(E.foo1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 24, 38))
94+
>this : Symbol(E, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 21, 1))
95+
>foo1 : Symbol(E.foo1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 24, 38))
96+
>this.foo2 : Symbol(E.foo2, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 26, 16))
97+
>this : Symbol(E, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 21, 1))
98+
>foo2 : Symbol(E.foo2, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 26, 16))
99+
100+
foo1 = '';
101+
>foo1 : Symbol(E.foo1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 24, 38))
102+
103+
constructor(public foo2: string) {}
104+
>foo2 : Symbol(E.foo2, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 26, 16))
105+
}
106+
107+
class F {
108+
>F : Symbol(F, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 27, 1))
109+
110+
Inner = class extends F {
111+
>Inner : Symbol(F.Inner, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 29, 9))
112+
>F : Symbol(F, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 27, 1))
113+
114+
p2 = this.p1
115+
>p2 : Symbol((Anonymous class).p2, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 30, 29))
116+
>this.p1 : Symbol(F.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 32, 5))
117+
>this : Symbol((Anonymous class), Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 30, 11))
118+
>p1 : Symbol(F.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 32, 5))
119+
}
120+
p1 = 0
121+
>p1 : Symbol(F.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 32, 5))
122+
}
123+
class G {
124+
>G : Symbol(G, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 34, 1))
125+
126+
Inner = class extends G {
127+
>Inner : Symbol(G.Inner, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 35, 9))
128+
>G : Symbol(G, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 34, 1))
129+
130+
p2 = this.p1
131+
>p2 : Symbol((Anonymous class).p2, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 36, 29))
132+
>this.p1 : Symbol(G.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 39, 16))
133+
>this : Symbol((Anonymous class), Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 36, 11))
134+
>p1 : Symbol(G.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 39, 16))
135+
}
136+
constructor(public p1: number) {}
137+
>p1 : Symbol(G.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 39, 16))
138+
}
139+
class H {
140+
>H : Symbol(H, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 40, 1))
141+
142+
constructor(public p1: C) {}
143+
>p1 : Symbol(H.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 42, 16))
144+
>C : Symbol(C, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 0, 0))
145+
146+
public p2 = () => {
147+
>p2 : Symbol(H.p2, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 42, 32))
148+
149+
return this.p1.foo;
150+
>this.p1.foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
151+
>this.p1 : Symbol(H.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 42, 16))
152+
>this : Symbol(H, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 40, 1))
153+
>p1 : Symbol(H.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 42, 16))
154+
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
155+
}
156+
157+
public p3 = () => this.p1.foo;
158+
>p3 : Symbol(H.p3, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 46, 5))
159+
>this.p1.foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
160+
>this.p1 : Symbol(H.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 42, 16))
161+
>this : Symbol(H, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 40, 1))
162+
>p1 : Symbol(H.p1, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 42, 16))
163+
>foo : Symbol(C.foo, Decl(assignParameterPropertyToPropertyDeclarationES2022.ts, 10, 16))
164+
}
165+

0 commit comments

Comments
 (0)