Skip to content

transformer: miscalculates design:type for enums #14740

@cjnoname

Description

@cjnoname

Summary

When using reflect-metadata to read design:type from a class property whose type is an enum, the transformer emits Object instead of String for string enums or Number for numeric enums. This makes runtime reflection incorrect for enum typed properties.

Repro steps

Clone the minimal repo and run the example.

Observe the output of Reflect.getMetadata("design:type", A.prototype, "enum1").

Example code used for testing

import "reflect-metadata";

import  { Enum2 } from "./Enum2.js";
import { ObjectType, Field } from "type-graphql";

enum Enum1 {
  A = "A",
  B = "B",
  C = "C",
}

class A {
  @Field(() => Enum1)
  enum1: Enum1;

 @Field(() => Enum2)
  enum2: Enum2;
}

const reflectedType1 = Reflect.getMetadata("design:type", A.prototype, "enum1");
console.log(reflectedType1.name);

Actual behavior

reflectedType1.name logs "Object".

Expected behavior

If the property type is a string enum, reflect-metadata should return "String". If the property type is a number enum, it should return "Number". In other words, the runtime design:type should match the primitive underlying the enum.

Be careful, the enum imported from other files should also represent the correct value.

Minimal reproduction repository

The repository with a minimal reproduction is available at
https://github.com/cjnoname/my-issue

Metadata

Metadata

Assignees

Labels

A-transformerArea - Transformer / Transpiler

Type

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions