microsoft / microsoft/TypeScript
`importHelpers` incorrectly requires `tslib` for native `#private` class members at every dated `target` (ES2022–ES2025), even though no helper is ever emitted
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
### 🔎 Search Terms
TS2354, "requires an imported helper", tslib private field, importHelpers ES2022, classPrivateFieldGet ES2022, classPrivateFieldSet ES2022, classPrivateFieldIn, ClassAndClassElementDecorators, LanguageFeatureMinimumTarget, private field tslib not needed, importHelpers private field false positive, private field decorator target ESNext
### 🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ (no entries about `tslib`, `importHelpers`, or private fields exist there).
- Reproduces on every TypeScript version from **4.3.5** (when native `#private` fields and the `ES2022` target were introduced) through the current **7.0.2** and the **7.1.0-dev** nightly — i.e. every version able to target `ES2022`.
- Not a regression introduced at any specific version, but the *mechanism* changed once, without changing the observable behavior: TS 4.3.5 through 5.4.5 used a blunt, unconditional `if (languageVersion < ScriptTarget.ESNext)` guard around the private-field helper check. TS 5.5.2 refactored this into today's three-term `LanguageFeatureMinimumTarget`-based condition (see "Additional information" below) — but for private fields specifically, the observable result is identical in every version: every dated `target` requires `tslib`; only the literal string `"ESNext"` doesn't.
- I was unable to reproduce this in the Playground specifically (see below), so I could not bisect via the Playground's version picker; I instead bisected locally by installing each `typescript@x.y.z` from npm and running `tsc` directly (versions tested: 4.3.5, 4.5.5, 4.7.4, 4.9.5, 5.0.4, 5.1.6, 5.2.2, 5.3.3, 5.4.5, 5.5.4, 5.6.3, 5.7.3, 5.8.3, 5.9.3, 6.0.3, 7.0.2, 7.1.0-dev — all reproduce it identically).
### ⏯ Playground Link
https://github.com/astegmaier/typescript-private-field-tslib-repro (you have to do this locally because the playground always has tslib installed)
### 💻 Code
```ts
// tsconfig.json: { "target": "ES2022", "importHelpers": true }
// no tslib installed in node_modules
export class C {
#x = 0;
get(): number { return this.#x; } // needs __classPrivateFieldGet per tsc
set(v: number): void { this.#x = v; } // needs __classPrivateFieldSet per tsc
has(o: unknown): boolean { return #x in (o as C); } // needs __classPrivateFieldIn per tsc
}
```
### 🙁 Actual behavior
With `target: "ES2022"` (or any dated target through `"ES2025"`) and `importHelpers: true`, and no `tslib` installed, `tsc` reports:
```
error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.
```
on the very first private-field access it checks (`get()`, above — commenting that method out reveals `set()` fails identically, and commenting that out too reveals `has()` fails identically; `tsc` only ever reports the first one per file). This happens for *every* form of native `#private` member access I could find: field reads/writes, compound assignment, optional chaining, brand checks (`#x in obj`), private methods, private accessors, static private fields/methods, static blocks, and private fields inside class expressions, generic classes, subclasses, and closures.
Once `tslib` **is** installed, the error goes away — but the compiled output for every one of the constructs above contains **zero** references to `tslib`, at any target. I confirmed this by forcing full (non-declaration-only) emission with `tslib` present and installed, and grepping the output `.js` for `tslib`: no matches, for any of the 13 private-field-related constructs listed above, at `ES2022` through `ES2025`.
### 🙂 Expected behavior
`tsc` should not require `tslib` to be resolvable for constructs that are natively supported at the configured `target` and that the emitter never actually references, the same way it doesn't for e.g. arrow functions or `let`/`const` at `ES2022`.
### Additional information about the issue
**Root cause.** In `checker.ts`'s `checkPropertyAccessExpressionOrQualifiedName` (and the parallel check in `checkInExpression` for `#x in obj`), the private-field helper requirement is gated by:
```ts
if (isPrivateIdentifier(right)) {
if (
languageVersion < LanguageFeatureMinimumTarget.PrivateNamesAndClassStaticBlocks ||
languageVersion < LanguageFeatureMinimumTarget.ClassAndClassElementDecorators ||
!useDefineForClassFields
) {
// ...checkExternalEmitHelpers(..., ClassPrivateFieldGet / ClassPrivateFieldSet)
}
```
and in `types.ts`:
```ts
export const LanguageFeatureMinimumTarget: Record = {
PrivateNamesAndClassStaticBlocks: ScriptTarget.ES2022, // correct: native since ES2022
ClassAndClassElementDecorators: ScriptTarget.ESNext, // <- pinned to the ESNext sentinel forever
...
};
```
`ClassAndClassElementDecorators` is pinned to `ScriptTarget.ESNext` (`99`) because TC39 decorators have never been assigned to a dated ECMAScript edition. So `languageVersion < LanguageFeatureMinimumTarget.ClassAndClassElementDecorators` is `true` for **every** dated target — making the whole `||` condition unconditionally true regardless of `target`, regardless of whether the file uses decorators at all, and regardless of `useDefineForClassFields`. Only the literal target string `"ESNext"` clears that term. This appears to be an unintentional side effect of gating an unrelated feature's (decorators') native-support threshold together with private fields' (correctly-gated-at-ES2022) one, rather than a deliberate design choice.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 checker.ts 中的 checkPropertyAccessExpressionOrQualifiedName 及其对应的 checkInExpression 检查开始,然后检查 types.ts 中的 LanguageFeatureMinimumTarget。使用 ES2022 目标和 importHelpers,通过链接的 TypeScript 私有字段示例重现该问题。当生成的输出不包含 tslib 引用时,原生私有成员访问不再报告缺少 tslib helper,并且相关情况具有回归测试覆盖,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 活跃
- 描述清晰度
- 描述清楚
- 新手友好度
- 72/100