Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20930,7 +20930,7 @@ namespace ts {
break;
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.PropertySignature:
if (hasModifier(container, ModifierFlags.Static)) {
if (hasModifier(container, ModifierFlags.Static) && !(compilerOptions.target === ScriptTarget.ESNext && compilerOptions.useDefineForClassFields)) {
error(node, Diagnostics.this_cannot_be_referenced_in_a_static_property_initializer);
// do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
}
Expand Down
20 changes: 20 additions & 0 deletions tests/baselines/reference/thisInClassBodyStaticESNext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [thisInClassBodyStaticESNext.ts]
// all are allowed with es-compliant class field emit
class Foo {
x = this
static t = this
static at = () => this
static ft = function () { return this }
static mt() { return this }
}


//// [thisInClassBodyStaticESNext.js]
// all are allowed with es-compliant class field emit
class Foo {
x = this;
static t = this;
static at = () => this;
static ft = function () { return this; };
static mt() { return this; }
}
25 changes: 25 additions & 0 deletions tests/baselines/reference/thisInClassBodyStaticESNext.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/compiler/thisInClassBodyStaticESNext.ts ===
// all are allowed with es-compliant class field emit
class Foo {
>Foo : Symbol(Foo, Decl(thisInClassBodyStaticESNext.ts, 0, 0))

x = this
>x : Symbol(Foo.x, Decl(thisInClassBodyStaticESNext.ts, 1, 11))
>this : Symbol(Foo, Decl(thisInClassBodyStaticESNext.ts, 0, 0))

static t = this
>t : Symbol(Foo.t, Decl(thisInClassBodyStaticESNext.ts, 2, 12))
>this : Symbol(Foo, Decl(thisInClassBodyStaticESNext.ts, 0, 0))

static at = () => this
>at : Symbol(Foo.at, Decl(thisInClassBodyStaticESNext.ts, 3, 19))
>this : Symbol(Foo, Decl(thisInClassBodyStaticESNext.ts, 0, 0))

static ft = function () { return this }
>ft : Symbol(Foo.ft, Decl(thisInClassBodyStaticESNext.ts, 4, 26))

static mt() { return this }
>mt : Symbol(Foo.mt, Decl(thisInClassBodyStaticESNext.ts, 5, 43))
>this : Symbol(Foo, Decl(thisInClassBodyStaticESNext.ts, 0, 0))
}

28 changes: 28 additions & 0 deletions tests/baselines/reference/thisInClassBodyStaticESNext.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/compiler/thisInClassBodyStaticESNext.ts ===
// all are allowed with es-compliant class field emit
class Foo {
>Foo : Foo

x = this
>x : this
>this : this

static t = this
>t : typeof Foo
>this : typeof Foo

static at = () => this
>at : () => typeof Foo
>() => this : () => typeof Foo
>this : typeof Foo

static ft = function () { return this }
>ft : () => any
>function () { return this } : () => any
>this : any

static mt() { return this }
>mt : () => typeof Foo
>this : typeof Foo
}

11 changes: 11 additions & 0 deletions tests/cases/compiler/thisInClassBodyStaticESNext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @target: esnext
// @useDefineForClassFields: true

// all are allowed with es-compliant class field emit
class Foo {
x = this
static t = this
static at = () => this
static ft = function () { return this }
static mt() { return this }
}