Microsoft/TypeScript

static initialization blocks do not parse prototype

Ouverte

#51 786 ouverte le 6 déc. 2022

 (0 commentaire) (0 réaction) (0 personne assignée)TypeScript (13 395 forks)batch import
BugDomain: JavaScriptHelp Wanted

Métriques du dépôt

Stars
 (108 860 étoiles)
Métriques de merge PR
 (Métriques PR en attente)

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.

Guide contributeur