microsoft / microsoft/TypeScript

Allow `this` in static initializers

Open
#8,363 1 comment 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback ES Next Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

The following is an error:

function factory() {
  return class {
    static x = 1;
    static y = this.x * 2;
  }
}

I can see some potential for confusion here, though I'm not sure what that confusion might be. On the flip side, it seems useful to be able to refer to an anonymous class in a static initializer. It is also easy to teach that "this in instance initializers = the instance, this in static initializers = the class, same as methods vs. static methods."

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported TypeScript example and investigate how static initializers handle this in anonymous classes. Define and verify the intended behavior against the stated comparison with instance initializers and static methods; the issue does not name implementation files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.