Microsoft/TypeScript

static initialization blocks do not parse prototype

Open

#51,786 opened on Dec 6, 2022

View on GitHub
ย (0 comments)ย (0 reactions)ย (0 assignees)TypeScriptย (48,455 stars)ย (6,726 forks)batch import
BugDomain: JavaScriptHelp Wanted

Description

Bug Report

When using static initialization blocks, changes to prototype are not parsed. They are only parsed when outside of the class.

๐Ÿ”Ž Search Terms

static initialization block, iife, prototype, salsa

๐Ÿ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about protoype

โฏ Playground Link

Playground Link

๐Ÿ’ป Code

class Foo {
  static {
    Foo.prototype.inner = 5;
    this.prototype.innerThis = 5;
  }

  test() {
    console.log(this.outer + 1);
    console.log(this.inner + 1);
    console.log(this.innerThis + 1);
  }

}

(() => {
    Foo.prototype.outerIIFE = 5;
})();

Foo.prototype.outer = 5;

๐Ÿ™ Actual behavior

Only Foo.prototype.outer has its type inferred.

๐Ÿ™‚ Expected behavior

I can understand if Foo.prototype.outerIIFE and Foo.prototype.innerThis are not working. But Foo.prototype.inner should be parsed. (bonus if we can add the other ones).


Usage would be to avoid having code appended to the end of a class, and instead, inside the class improving code clarity. That was one of the goals of implementing static initialization blocks. Sometimes properties need to be defined before instance initialization instead of after constructor() is called (eg: HTMLElement.observedAttributes needing to be ready before constructor() is called.). I believe prototype checking was added around Typescript 3.0, and static class blocks didn't exist yet.

Contributor guide

static initialization blocks do not parse prototype ยท Microsoft/TypeScript#51786 | Good First Issue