leonardfactory / babel-plugin-transform-typescript-metadata

Babel plugin to emit decorator metadata like typescript compiler

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Emitted type is undefined for enum

kabukki opened this issue · 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 !

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.

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

commented

@leonardfactory any news on that ?