0

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[];

1 Answer 1

2
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

Sign up to request clarification or add additional context in comments.

1 Comment

I checked your channel, great work, thank you. The problem is that the RobotTransformType enumeration is type of a model property like : class RobotTypeModel { @prop({ enum: RobotTransformType }) transformType: RobotTransformType[]; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.