babel-plugin-transform-typescript-metadata icon indicating copy to clipboard operation
babel-plugin-transform-typescript-metadata copied to clipboard

Emitted type is undefined for enum

Open kabukki opened this issue 6 years ago • 3 comments

Disclaimer: I am not entirely sure I understand everything that is happening here, but I'll try my best to make sense.

I am using Typeorm while compiling Typescript with Babel 7, so I need experimentalDecorators (with @babel/plugin-proposal-decorators) and emitDecoratorMetadata (with this package).

Everything runs fine, but when I run the output code, I get the following error: Data type "undefined" in "CharacterEntity.gender" is not supported by "mysql" database., which refer to:

// CharacterEntity.ts 
@Entity('characters')
export class CharacterEntity {
	// ...

	@Column()
	gender: CharacterGender;

	// ...
}

// CharacterGender.ts
export enum CharacterGender {
	MALE = 'male',
	FEMALE = 'female'
}

When looking at the code procuded by Babel, I believe only these lines are relevant:

_dec9 = (0, _typeorm.Column)(), _dec10 = Reflect.metadata("design:type", typeof _enums.CharacterGender === "undefined" ? Object : _enums.CharacterGender)

// ...

_descriptor4 = _applyDecoratedDescriptor(_class2.prototype, "gender", [_dec9, _dec10], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: null
})

// ...

 _initializerDefineProperty(this, "gender", _descriptor4, this);

So this issue is blocking me. Note that everything compiles and runs fine when using the Typescript compiler, which seems to produce:

__decorate([
    typeorm_1.Column(),
    __metadata("design:type", String)
], CharacterEntity.prototype, "gender", void 0);

I hope this is clear enough to provide enough information, it would be great if you could look into this or simply explain what I'm doing wrong :)

Thanks !

kabukki avatar Jul 02 '19 16:07 kabukki

I think here TSC is intelligent enough to convert enums into string / number when needed. I didn't test enums completely, I'll try to find a fix asap.

leonardfactory avatar Jul 25 '19 13:07 leonardfactory

Sorry for the extremely late answer. However, this problem arises from missing type informations (that TSC resolves thanks to its checker that is not available here in Babel. I'd try to add something like an hack for this kind of situation, like a leading comment, but the best thing for your situation now is just to rely on hand-configured varchar type for the column

leonardfactory avatar Oct 23 '19 23:10 leonardfactory

@leonardfactory any news on that ?

tada5hi avatar Feb 17 '23 21:02 tada5hi