swc: design:type wrong for enum values
Describe the bug In TSC, when you have an enum type, the base enum type is set as design:type. SWC is setting the enum object itself.
Input code
enum MyEnum {
x = "xxx",
y = "yyy"
}
class Xpto {
@Decorator()
value!: MyEnum;
}
function Decorator() {
return function (...args) {};
}
Config
{
"module": {
"type": "commonjs"
},
"jsc": {
"target": "es2018",
"parser": {
"syntax": "typescript",
"decorators": true,
"dynamicImport": false
},
"transform": {
"legacyDecorator": false,
"decoratorMetadata": true
}
}
}
Expected behavior A clear and concise description of what you expected to happen.
TSC:
__decorate([
Decorator(),
__metadata("design:type", String)
], Xpto.prototype, "value", void 0);
SWC:
Reflect.metadata("design:type", typeof MyEnum === "undefined" ? Object : MyEnum)
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 8
- Comments: 37 (14 by maintainers)
Commits related to this issue
- fix: Decorator metadata (#1248) swc_ecma_transforms: - Emit proper typename for `design:type` used with enum. (#1160) — committed to swc-project/swc by kdy1 4 years ago
- Add a test for #1160 — committed to kdy1/swc by kdy1 3 years ago
- Fix #1160 — committed to kdy1/swc by kdy1 3 years ago
- Add an integration test for #1160 — committed to kdy1/swc by kdy1 3 years ago
For me it was to explicitly set the types, it seems like there’s a problem inferring the type when built with
swc
, but if I explicitly set them i.e. decorator@Column({ type: DataTypes.STRING })
, then it works without issue.I’m not sure if TypeORM uses a different format but setting the type explicitly in the decorator solved any Sequelize issues.
I believe this single file limitation will be a deal breaker for most people interested in SWC.
If SWC can’t be a transparent replacement for TSC, there will be just very few use cases where it will be used.
Closing as we are not going to implement
project mode
oftsc
See if this typeorm sidestep I made is somehow portable to sequelize. Essentially there were infered enum types that did not work and needed to be explicitly set
@Exilz No. I don’t have one.
@Exilz I’ll fix this soon.
I expect today or tomorrow (It’s almost 11 PM here, so…)
Amazing, this solved all the issues. Somehow tsc infers correct types when building but swc didn’t, but by giving explicit types to Sequelize it works without issue. Thank you!
@kdy1 hello and thanks for the awesome product! It works amazing, but we bumped into the same issue with decorator metadata, while trying to adopt SWC in the company. I see that it’s been moved into Blocked by type checker milestone. Is the type-checking feature has been planned to be implemented in any foreseeable future? Thanks.
I think the option is enough for
tsc
mode (or project mode) vsbabel
mode.Eventually, I’ll add the ability to run babel plugins without modification. (See https://github.com/swc-project/swc/pull/1416)
@Exilz I’m now in progress of publishing. Github actions will publish new version. Typically it takes about one hour.
Oh. I noticed that github updates comments real-time.