Microsoft/TypeScript

Class marked as unused when used in a static block

Open

#57,620 建立於 2024年3月3日

在 GitHub 查看
 (16 留言) (0 反應) (0 負責人)TypeScript (6,726 fork)batch import
BugDomain: LS: Suggestion DiagnosticsHelp Wanted

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

🔎 Search Terms

class unused static field block

🕗 Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.5.0-dev.20240303#code/DYUwLgBAQsCu4AsD2SwBEQDcCWBjEA+rgE4gCGYIA3ALABQuwZAzs9HIiulniBAN70IEAMTYAJgEIAXBGZhi2AHYBzWnSFywFPKLLBgSAO4BhJEvnFYuSAF4IAM33Nq9TQAdFmCn1znL1mBIxAAUAJQCmsLYDhAhkjDwYMioGDj4AHQi+oam-gqBEYJ0wqUQycTGEEogRhAAosSVoQBEAJpIsBC4ZEpKqBDK8r1g2D7lCNhsjCxs4tikNsAAni1h6mUAvprbGiVaOriR+8KJnKk8+ESk4-YhErKWyioRtgB8x2WnHMlcabxZHLGMwWAo2CD2AquE6lPygiDiS58ew1OpnX4XdIgcIbL7olLcLGAgzA-JWcH2JzAFy4sqIolicQQwbiWmlUhgWDEJQIpFsza43aaFTgFnhT7s8BcnnJKZZCSC+hCuigSBkZkAJnU9HxfyR13IlBCLTIZDW2roIAAHu5gpB+JsgA

💻 Code

let BluethootDevice_create;
class BluethootDevice {
  #id!: string;

  static #allowConstruct = false;

  private constructor() {
    if (!BluethootDevice.#allowConstruct) {
      throw new Error("You cannot instantiate this class directly");
    }
  }

  static {
    BluethootDevice_create = (id: string) => {
      BluethootDevice.#allowConstruct = true;
      const device = new BluethootDevice();
      BluethootDevice.#allowConstruct = false;
      device.#id = id;
      return device;
    };
  }

  get id() {
    return this.#id;
  }
}

let a = 2;

BluethootDevice_create("aa");

export {}

🙁 Actual behavior

The class BluethootDevice is marked as unused, but it's used by the BluethootDevice_create function

🙂 Expected behavior

BluethootDevice should not be marked as unused

Additional information about the issue

No response

貢獻者指南