Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum transpilling in the context of type:orm inconsistent with standard build #4550

Closed
drFabio opened this issue May 6, 2022 · 4 comments
Closed
Labels

Comments

@drFabio
Copy link

drFabio commented May 6, 2022

Describe the bug

I am not entirely sure if this is an SWC bug , and I think it is sidesteppable but I wanted to report it so others with the same error can check a solution, and if it is indeed a bug it can be fixed.

Consider this proof of concept

While trying to use an enum in the context of a TypeORM field the generated code when run results on the error:

"DataTypeNotSupportedError: Data type "undefined" in "SomeEntity.problemField" is not supported by "sqlite" database."

This seems to be caused because the transpiled code, on the code that uses the enum gets generated to:

    __metadata(
      'design:type',
      typeof _someEnum.SomeEnum === 'undefined' ? Object : _someEnum.SomeEnum,
    )

That confuses typeorm for some reason, the generated code by nest (that works) on the relevant part becomes:

    __metadata('design:type', String),

It seems that the type of swc should be String. Whether this is the incorrect behavior or not I do not have enough knowledge to say.

Input code

The example is on the repo here

import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { SomeEnum } from './SomeEnum';

@Entity()
export class SomeEntity {
  @PrimaryGeneratedColumn()
  id?: number;

  @Column()
  name?: string;

  @Column({ nullable: false, enum: SomeEnum })
  strategy!: SomeEnum;
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": true,
      "decorators": true
    },
    "transform": {
      "legacyDecorator": true,
      "decoratorMetadata": true
    },
    "target": "es2020",
    "keepClassNames": true
  },
  "module": {
    "type": "commonjs",

    "noInterop": true
  }
}

Playground link

No response

Expected behavior

Generated enums are compatible with both typeOrm and NestJs expectations. (Again not sure if it is "They" that are wrong or swc")

Actual behavior

Generated transpiled enums differ from the ones expected by NestJs or typeorm

Version

1.2.177

Additional context

This behavior could be verified by checking the poc .

Then one could try to run it and ensure it runs:

 yarn start:dev

After that one could try to build and check the result of dist/SomeEntity.js

 yarn build

And finally one could check the transpiled swc and check the result of output/SomeEntity.js

npx swc ./src -d output 

This behavior seems to be easily dealt by explicitly stating types in case of enums. As illustrated by this branch

import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { SomeEnum } from './SomeEnum';
import 'reflect-metadata';
@Entity()
export class SomeEntity {
  @PrimaryGeneratedColumn()
  id?: number;

  @Column()
  name?: string;

  @Column({ nullable: false, enum: SomeEnum, type: 'varchar' })
  problemField!: SomeEnum;
}
@drFabio drFabio added the C-bug label May 6, 2022
@kdubrovnyi
Copy link

@drFabio hello, looks like the same one that we just bumped into, also there is another bug that was reported some time ago regarding this issue: #1160
it seems like it requires a full typechecking implementation to be implemented, so not sure when it's going to be fixed...
Asked the author in this other bug.

@kdy1
Copy link
Member

kdy1 commented May 7, 2022

Closing as duplicate of #1160

@kdy1 kdy1 closed this as completed May 7, 2022
@drFabio
Copy link
Author

drFabio commented Jun 15, 2022

@dsteinbach-ep this was the issue I was referring on the closed discussion

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 16, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

4 participants