Skip to content

Mapping Enum variants with same enum keys #393

Answered by nartc
aki-ks asked this question in Q&A
Discussion options

You must be logged in to vote

Thank you for showing interest and requesting this feature.

However, Enums are complicated: string enums, number enums, mixed enums, and const enums... Hence, it is tricky to get some generic support for enum in this sense.

What I can suggest you to do is to create a Resolver and use a switch statement

export const colorResolver: Resolver<
  {color: ColorEntity},
  {color: ColorDto},
  ColorDto
> = {
  resolve(source: TypeConverterSource): ColorDto {
    switch (source.color) {
      case Color.Red:
        return ColorDto.Red;
      case Color.Green:
        return ColorDto.Green;
      case Color.Blue:
        return ColorDto.Blue;
    }
  },
};

then use it on a mapFrom()

mapper.createMap(

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by nartc
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
enhancement New feature or request
2 participants
Converted from issue

This discussion was converted from issue #392 on January 01, 2022 15:46.