Skip to content

Referencing this from a static class field #36267

Closed
@rauschma

Description

@rauschma

TypeScript Version: Nightly Playground

The following code (Playground link) is legal in JavaScript, but produces a static error in TypeScript:

class C {
  static a = 'a';
  static b = this.a;
    // 'this' cannot be referenced in a static property initializer.
}
console.log(C.b); // 'a'

Practical use case – my library Enumify for enums:

class Color extends Enumify {
  static red = new Color();
  static orange = new Color();
  static yellow = new Color();
  static green = new Color();
  static blue = new Color();
  static purple = new Color();
  static _ = this.closeEnum(); // TypeScript: Color.closeEnum()
}

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix 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