Skip to content

Commit 84c3da8

Browse files
authored
feat(transformer): add decorator metadata limitation warning (#609)
1 parent 42d2a41 commit 84c3da8

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

src/docs/guide/usage/transformer/typescript.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,83 @@ const result = transform("lib.ts", sourceCode, {
7171
});
7272
```
7373

74+
:::: warning Decorator Metadata: Types that requires type inference will fallback to `Object`
75+
76+
Due to the lack of full type inference feature, Oxc transformer will fallback to `Object` type if it cannot calculate the type of the decorator metadata.
77+
78+
For example, the following code will be transformed to the following code:
79+
80+
::: code-group
81+
82+
```ts [input.ts]
83+
import { Something1 } from "./somewhere";
84+
85+
type Something2 = Exclude<string | number, string>;
86+
87+
export class Foo {
88+
@test
89+
foo(input1: Something1, input2: Something2) {}
90+
}
91+
```
92+
93+
```js [output(oxc).js]
94+
// omit helper functions
95+
import { Something1 } from "./somewhere";
96+
var _ref;
97+
export class Foo {
98+
foo(input1, input2) {}
99+
}
100+
_decorate(
101+
[
102+
test,
103+
_decorateMetadata("design:type", Function),
104+
_decorateMetadata("design:paramtypes", [
105+
typeof (_ref = typeof Something1 !== "undefined" && Something1)
106+
=== "function"
107+
? _ref
108+
: Object,
109+
Object, // [!code highlight]
110+
]),
111+
_decorateMetadata("design:returntype", void 0),
112+
],
113+
Foo.prototype,
114+
"foo",
115+
null,
116+
);
117+
```
118+
119+
```js [output(typescript_compiler).js]
120+
// omit helper functions
121+
var _a;
122+
import { Something1 } from "./somewhere";
123+
export class Foo {
124+
foo(input1, input2) {}
125+
}
126+
__decorate(
127+
[
128+
test,
129+
__metadata("design:type", Function),
130+
__metadata("design:paramtypes", [
131+
typeof (_a = typeof Something1 !== "undefined" && Something1)
132+
=== "function"
133+
? _a
134+
: Object,
135+
Number, // [!code highlight]
136+
]),
137+
__metadata("design:returntype", void 0),
138+
],
139+
Foo.prototype,
140+
"foo",
141+
null,
142+
);
143+
```
144+
145+
:::
146+
147+
This behavior aligns with TypeScript's behavior when using a type that is external.
148+
149+
::::
150+
74151
## TSX
75152

76153
Transforming TSX files is supported as well.

0 commit comments

Comments
 (0)