Skip to content

Execution order of static field initialization and static initialization blocks is not kept under target: esnext and useDefineForClassFields: falseΒ #45574

@uhyo

Description

@uhyo

Bug Report

πŸ”Ž Search Terms

class static initializer block esnext useDefineForClassFields

πŸ•— Version & Regression Information

4.4.1-rc, 4.5.0-dev.20210825

Static Initialization Blocks was newly introduced in TS 4.4.

⏯ Playground Link

Playground

πŸ’» Code

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "useDefineForClassFields": false
  }
}

Code

class A {
  static foo = 123;
  static {
    console.log(this.foo); // should show 123
  }
}

πŸ™ Actual behavior

Following code is emitted. The line marked as // should show 123 would not print 123.

var _a;
class A {
    static {
        console.log(this.foo); // should show 123
    }
}
_a = A;
A.foo = 123;

πŸ™‚ Expected behavior

An emit such that execution order of the static field initialization and the static initialization block are kept.

static {} initialization blocks are evaluated in document order interleaved with static field initializers.

ECMAScript class static initialization blocks

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: JS EmitThe issue relates to the emission of JavaScriptFix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions