Lu Blue
Lu Blue

Reputation: 355

Typescript enum array variable

Is it possible to have a string[] type variable but that allows only values from an enumeration ?

I am trying to achieve it this way but something is wrong:

enum RobotTransformType = {
 JET = "JET",
 CAR = "CAR"
 ... many other options
}

let multipleTransformBot: RobotTransformType[];
multipleTransformBot = ["JET","CAR",... maybe others];

It also important to know that this enumeration is the type of a Typegoose(typescript mongoose) model:

@prop({ enum: RobotTransformType })
transformType: RobotTransformType[];

Upvotes: 0

Views: 195

Answers (1)

Yakov Fain
Yakov Fain

Reputation: 12376

enum RobotTransformType  {
 JET = "JET",
 CAR = "CAR"
}

let multipleTransformBot: RobotTransformType[];
multipleTransformBot = [RobotTransformType.JET,
    RobotTransformType.CAR
];

Watch my lesson on string enums here: https://www.youtube.com/playlist?list=PLkKunJj_bZecSLIEeXEhUxD7e7aj7-fN3

Upvotes: 2

Related Questions