Microsoft/TypeScript

static initialization blocks do not parse prototype

Open

#51,786 创建于 2022年12月6日

在 GitHub 查看
 (0 评论) (0 反应) (0 负责人)TypeScript (6,726 fork)batch import
BugDomain: JavaScriptHelp Wanted

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

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.

贡献者指南