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

贡献者指南