fix(transformer/legacy-decorator): emit Array for ReadonlyArray<T> in decorator metadata#22265
Merged
Dunqing merged 2 commits intoMay 21, 2026
Conversation
7bbfc9b to
7d1252a
Compare
7d1252a to
802f7c3
Compare
… decorator metadata
`ReadonlyArray<T>` is a TypeScript-only utility type with no runtime value.
The legacy decorator metadata emit currently wraps it in the runtime guard
`typeof (_ref = typeof X !== "undefined" && X) === "function" ? _ref : Object`,
which always falls through to `Object` because there is no runtime
`ReadonlyArray` binding. tsc resolves this through the checker and emits
`Array`; OXC's existing handling of the short form `readonly T[]` already
emits `Array`, so the generic form is the only divergent shape.
```ts
class Entity {
@d() shortForm!: readonly string[]; // tsc: Array, OXC: Array (already correct)
@d() genericForm!: ReadonlyArray<string>; // tsc: Array, OXC: Object (bug)
@d() nestedReadonly!: ReadonlyArray<ReadonlyArray<number>>; // same
}
```
The two surface forms describe the same type but produce different runtime
metadata. Downstream consumers (TypeORM `@Column` array inference, NestJS
Swagger schema, AutoMapper field detection) rely on the `Array`
constructor; `Object` causes silent degradation.
In `serialize_type_reference_node`, recognize `ReadonlyArray` by name when
the symbol is unresolved or type-only. Emit `global_array(ctx)` directly,
bypassing the wrapper. A user-shadowed `class ReadonlyArray {}` would have
a value symbol and falls through to the normal class-handling path, so the
shadow case is preserved.
```rust
if ident.name == "ReadonlyArray"
&& symbol_id.is_none_or(|sid| ctx.scoping().symbol_flags(sid).is_type())
{
return Self::global_array(ctx);
}
```
Only `ReadonlyArray` is handled in this PR. `ReadonlyMap`, `ReadonlySet`,
`WeakMap`, `WeakSet`, and `Readonly<Array<T>>` peel are left for a follow-up
because tsc's spec emit for those is less unambiguous and warrants
case-by-case verification. The same predicate-style approach extends
naturally if reviewers want them folded in.
- [x] 4 unit tests in `tests/integrations/decorator_metadata.rs`: bare
ReadonlyArray, nested ReadonlyArray, regression on `readonly T[]` short
form, and a user-shadowed `class ReadonlyArray {}` regression
- [x] New conformance fixture `oxc/metadata/readonly-array/`
- [x] `cargo test -p oxc_transformer` passes
- [x] `cargo run -p oxc_transform_conformance` shows 0 regressions; 229 OXC
fixtures still pass, plus the new `readonly-array` fixture
- [x] `cargo fmt -p oxc_transformer` and `cargo clippy -p oxc_transformer
--tests --no-deps` clean
AI assistance was used in writing this patch and tests; the contributor has
reviewed and tested locally.
…e to unresolved refs
Only treat `ReadonlyArray` as the global lib type when the reference is
truly unresolved (`symbol_id.is_none()`). The previous `is_type()`
predicate also matched locally-declared `interface ReadonlyArray` and
`type ReadonlyArray = …`, miscompiling them to `Array` despite the
user's clearly-different local type.
The `class ReadonlyArray {}` case continues to fall through (Class
symbols have `is_type() == false`) and is covered by Kyle's existing
test.
Adds a conformance fixture for the interface-shadow case.
802f7c3 to
485def7
Compare
Merging this PR will not alter performance
Comparing Footnotes
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ReadonlyArray<T>is a TypeScript-only utility type with no runtime value. The legacy decorator metadata emit currently wraps it in the runtime guardtypeof (_ref = typeof X !== "undefined" && X) === "function" ? _ref : Object, which always falls through toObjectbecause there is no runtimeReadonlyArraybinding. tsc resolves this through the checker and emitsArray; OXC's existing handling of the short formreadonly T[]already emitsArray, so the generic form is the only divergent shape.The bug
The two surface forms describe the same type but produce different runtime metadata. Downstream consumers (TypeORM
@Columnarray inference, NestJS Swagger schema, AutoMapper field detection) rely on theArrayconstructor;Objectcauses silent degradation.Fix
In
serialize_type_reference_node, recognizeReadonlyArrayby name when the symbol is unresolved or type-only. Emitglobal_array(ctx)directly, bypassing the wrapper. A user-shadowedclass ReadonlyArray {}would have a value symbol and falls through to the normal class-handling path, so the shadow case is preserved.Scope
Only
ReadonlyArrayis handled in this PR.ReadonlyMap,ReadonlySet,WeakMap,WeakSet, andReadonly<Array<T>>peel are left for a follow-up because tsc's spec emit for those is less unambiguous and warrants case-by-case verification. The same predicate-style approach extends naturally if reviewers want them folded in.Test plan
decorator_metadata_readonly_array.rs: bare ReadonlyArray, nested ReadonlyArray, regression onreadonly T[]short form, and a user-shadowedclass ReadonlyArray {}regressionoxc/metadata/readonly-array/cargo test -p oxc_transformerpasses (31 unit + 9 integration)cargo run -p oxc_transform_conformanceshows 229 OXC fixtures still passing, +1 new fixture (readonly-array); 0 regressionscargo fmt -p oxc_transformerandcargo clippy -p oxc_transformer --tests --no-depscleanAI assistance was used in writing this patch and tests; the contributor has reviewed and tested locally.