Skip to content

Commit af901ba

Browse files
authored
No error on this exprs in static property inits (#36781)
No error on `this` expressions in static property declaration initialisers when targetting ESNext and with useDefineForClassFields. In this case the emit is correct and the types are correct, so the error should not be issued.
1 parent 454cdb8 commit af901ba

File tree

5 files changed

+85
-1
lines changed

5 files changed

+85
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20947,7 +20947,7 @@ namespace ts {
2094720947
break;
2094820948
case SyntaxKind.PropertyDeclaration:
2094920949
case SyntaxKind.PropertySignature:
20950-
if (hasModifier(container, ModifierFlags.Static)) {
20950+
if (hasModifier(container, ModifierFlags.Static) && !(compilerOptions.target === ScriptTarget.ESNext && compilerOptions.useDefineForClassFields)) {
2095120951
error(node, Diagnostics.this_cannot_be_referenced_in_a_static_property_initializer);
2095220952
// do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
2095320953
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [thisInClassBodyStaticESNext.ts]
2+
// all are allowed with es-compliant class field emit
3+
class Foo {
4+
x = this
5+
static t = this
6+
static at = () => this
7+
static ft = function () { return this }
8+
static mt() { return this }
9+
}
10+
11+
12+
//// [thisInClassBodyStaticESNext.js]
13+
// all are allowed with es-compliant class field emit
14+
class Foo {
15+
x = this;
16+
static t = this;
17+
static at = () => this;
18+
static ft = function () { return this; };
19+
static mt() { return this; }
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/thisInClassBodyStaticESNext.ts ===
2+
// all are allowed with es-compliant class field emit
3+
class Foo {
4+
>Foo : Symbol(Foo, Decl(thisInClassBodyStaticESNext.ts, 0, 0))
5+
6+
x = this
7+
>x : Symbol(Foo.x, Decl(thisInClassBodyStaticESNext.ts, 1, 11))
8+
>this : Symbol(Foo, Decl(thisInClassBodyStaticESNext.ts, 0, 0))
9+
10+
static t = this
11+
>t : Symbol(Foo.t, Decl(thisInClassBodyStaticESNext.ts, 2, 12))
12+
>this : Symbol(Foo, Decl(thisInClassBodyStaticESNext.ts, 0, 0))
13+
14+
static at = () => this
15+
>at : Symbol(Foo.at, Decl(thisInClassBodyStaticESNext.ts, 3, 19))
16+
>this : Symbol(Foo, Decl(thisInClassBodyStaticESNext.ts, 0, 0))
17+
18+
static ft = function () { return this }
19+
>ft : Symbol(Foo.ft, Decl(thisInClassBodyStaticESNext.ts, 4, 26))
20+
21+
static mt() { return this }
22+
>mt : Symbol(Foo.mt, Decl(thisInClassBodyStaticESNext.ts, 5, 43))
23+
>this : Symbol(Foo, Decl(thisInClassBodyStaticESNext.ts, 0, 0))
24+
}
25+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/compiler/thisInClassBodyStaticESNext.ts ===
2+
// all are allowed with es-compliant class field emit
3+
class Foo {
4+
>Foo : Foo
5+
6+
x = this
7+
>x : this
8+
>this : this
9+
10+
static t = this
11+
>t : typeof Foo
12+
>this : typeof Foo
13+
14+
static at = () => this
15+
>at : () => typeof Foo
16+
>() => this : () => typeof Foo
17+
>this : typeof Foo
18+
19+
static ft = function () { return this }
20+
>ft : () => any
21+
>function () { return this } : () => any
22+
>this : any
23+
24+
static mt() { return this }
25+
>mt : () => typeof Foo
26+
>this : typeof Foo
27+
}
28+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @target: esnext
2+
// @useDefineForClassFields: true
3+
4+
// all are allowed with es-compliant class field emit
5+
class Foo {
6+
x = this
7+
static t = this
8+
static at = () => this
9+
static ft = function () { return this }
10+
static mt() { return this }
11+
}

0 commit comments

Comments
 (0)