Skip to content

fix(transformer/typescript): don't evaluate keys of declare fields with removeClassFieldsWithoutInitializer - #30272

Merged
Dunqing merged 1 commit into
mainfrom
fix/typescript-declare-field-key-removed
Nov 25, 2026
Merged

Dunqing merged 1 commit into
mainfrom
fix/typescript-declare-field-key-removed

Conversation

@Dunqing

@Dunqing Dunqing commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

With removeClassFieldsWithoutInitializer, the computed key of an undecorated declare field, such as declare [f()]: any, was still evaluated: the TypeScript transform moved f() into the class's computed-key static block. A declare field is type-only, so tsc and esbuild erase it together with its key, and Oxc's other field modes already do.

Example

Input (playground for this example):

const log: string[] = [];
const key = (name: string) => (log.push(name), name);

class Foo {
  declare [key("declared")]: any;
  [key("field")] = 1;
}
console.log(log);

Options: { "assumptions": { "setPublicClassFields": true }, "plugins": [["transform-typescript", { "removeClassFieldsWithoutInitializer": true }]] }

Command: cargo run -p oxc_transformer --example transformer -- input.ts --babel-options <dir with options.json>

Before output:

let _key;
const log = [];
const key = (name) => (log.push(name), name);
class Foo {
  constructor() {
    this[_key] = 1;
  }
  static {
    key("declared"), _key = key("field");
  }
}
console.log(log);

After output:

let _key;
const log = [];
const key = (name) => (log.push(name), name);
class Foo {
  constructor() {
    this[_key] = 1;
  }
  static {
    _key = key("field");
  }
}
console.log(log);

Run with node, the before output logs [ 'declared', 'field' ] and the after output logs [ 'field' ].

Fix

The branch that keeps side-effecting keys of removed fields now skips declare fields. Decorated fields take the branch above it, so legacy decorators still get their keys.

A new fixture covers instance and static declare fields with side-effecting keys, a plain declare x: number, and a non-declare [g()]: any whose key is still evaluated, with an exec test of the evaluated keys; it fails before this change. Transformer tests, conformance with --exec, clippy, and transformer/semantic coverage pass with no snapshot changes beyond the new fixture.

AI disclosure: written with Claude Code (Claude Opus 5.5; reasoning effort not recorded).

…with `removeClassFieldsWithoutInitializer`

With `removeClassFieldsWithoutInitializer`, the computed key of an
undecorated `declare` field, such as `declare [f()]: any`, was moved into
the class's computed key static block, so `f()` ran. A `declare` field is
type-only: TypeScript and esbuild erase it with its key, as Oxc already
does in the other field modes.
@github-actions github-actions Bot added the A-transformer Area - JS/TS transforms and downleveling, oxc_transformer label Nov 25, 2026
@Dunqing
Dunqing enabled auto-merge November 25, 2026 16:22
@Dunqing
Dunqing added this pull request to the merge queue Nov 25, 2026
@codspeed

codspeed Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

Warning

oxc-project has exceeded the Hobby plan limit of 5 active contributors. Upgrade your plan or apply to the open source program by December 8, 2026 to keep using CodSpeed.

✅ 81 untouched benchmarks
⏩ 29 skipped benchmarks1


Comparing fix/typescript-declare-field-key-removed (a3cbb89) with main (92c6dfd)

Open in CodSpeed

Footnotes

  1. 29 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Merged via the queue into main with commit 5a0133f Nov 25, 2026
41 checks passed
@Dunqing
Dunqing deleted the fix/typescript-declare-field-key-removed branch November 25, 2026 16:31
Dunqing added a commit to rolldown/rolldown that referenced this pull request Nov 26, 2026
Pins the decorators-lowering head restacked onto oxc main 746ad93f.

Snapshot changes from oxc main:
- ts_experimental_decorators: computed keys of decorated `declare`
  fields are evaluated and passed to __decorate again
  (oxc-project/oxc#30264).
- ts_declare_class_fields: keys of `declare` fields are no longer
  evaluated, as tsc and esbuild do (oxc-project/oxc#30272).
- invalid_define_config/value: the error names the key and value
  (oxc-project/oxc#30280).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-transformer Area - JS/TS transforms and downleveling, oxc_transformer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant