Class-transformer: Transforming Enum

Created on 26 Jul 2016  路  7Comments  路  Source: typestack/class-transformer

Hi,

Is it possible to transform ENUM to String and String to ENUM?

thanks
Karthik

Most helpful comment

I suggest the use of a transformation decorator @Transform(myEnumToString). For example you will have in your enum type module :

export enum PlanType {
    Basic = 1,
    Premium = 2,
    Advanced = 3
}

export function planTypeToString(type: PlanType): string {
    return PlanType[type];
}

your Plan class would look cleaner and your planType property would keep its type PlanType:

import { PlanType, planTypeToString} from './PlanType';

class Plan {
    @Exclude()
    @Transform(planTypeToString)
    planType: PlanType;
}

All 7 comments

afaik string enums are not supported by Typescript

What i meant by is if you have a enum like below

enum PlanType {
Basic,
Premium
}

class Plan {
name: string;
type: PlanType;
}

When we convert the following json { name: "Basic Plan", type: "Basic" } to Plan object, the type property is set as "Basic" string instead of PlanType.Basic enum.

Is it possible to convert the String representation of enum to enum?

I don't think its possible because of Typescript specifics. Basic in PlanType is not a string representation, so it cannot be mapped to a string

Finally found a way to do this

enum PlanType {
    Basic = 1,
    Premium = 2,
    Advanced = 3
}

class Plan {

    @Exclude()
    planType: PlanType;

    get type() : string {
        return PlanType[this.planType];
    }

    set type(typeName: string) {
        this.planType = PlanType[typeName];
    }
}

let plan: Plan = new Plan();

plan.planType = PlanType.Basic;
console.log(plan.type); // Basic
console.log(plan.planType); // 1

plan.type = "Premium";
console.log(plan.type); // Premium
console.log(plan.planType); // 2


plan.planType = 3;
console.log(plan.type); // Advanced
console.log(plan.planType); // 3

I suggest the use of a transformation decorator @Transform(myEnumToString). For example you will have in your enum type module :

export enum PlanType {
    Basic = 1,
    Premium = 2,
    Advanced = 3
}

export function planTypeToString(type: PlanType): string {
    return PlanType[type];
}

your Plan class would look cleaner and your planType property would keep its type PlanType:

import { PlanType, planTypeToString} from './PlanType';

class Plan {
    @Exclude()
    @Transform(planTypeToString)
    planType: PlanType;
}

Short option:

enum Status {
 pending = 2,
  inactive = 3,
  active = 4
}
@Transform(status => Status[status])
@Column({ name: 'Status' })
status: Status

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sebastian-zarzycki-es picture sebastian-zarzycki-es  路  6Comments

patricknazar picture patricknazar  路  4Comments

Kalhama picture Kalhama  路  5Comments

AckerApple picture AckerApple  路  5Comments

samfrach picture samfrach  路  6Comments