0

I have a constant

const requestBody: RequestBody = {
  clientId: clientId,
  orderingId: 113875599,
  receivedSignatureFlag: 'N',
  saturdayDeliveryFlag: 'N',
  deliveryMethodOptionCode: 'USPS',
  orderItemsList: [
    {
      itemCode: 'A123'
      transferCode: 'MAIL',
      specialHandling: 'N',
      deliveryExternalFlag: 'Y',
      orderQuantityAmount: 1,

    }
  ]
};

I have created an enum for the string values and would like to set the values from the enum. This is the enum

export enum ReqBodyEnum {
  RCVD_FLAG = 'N',
  SAT_DELIVERY_FLAG = 'N',
}

I tried setting the

receivedSignatureFlag:ReqBodyEnum[ReqBodyEnum.RCVD_FLAG]  -> this isnt working.

Also tried

receivedSignatureFlag:ReqBodyEnum.RCVD_FLAG

What is the best way to set the value from an enum?

1 Answer 1

1

It seems that you've misinterpretted what an enum is. Think of an enum as a collection of potential values (constants) for a given property.

As an example, in TypeScript, this would be represented as:

enum VehicleType {
  motorcyle = 'motorcycle',
  car = 'car',
  truck = 'truck',
  van = 'van'
}

class Vehicle {
  ...
  type: VehicleType;
  ...
}

To appropriate the above to your response, you would want to rethink the approach. Ideally you would want to create an enum for every property that requires it and create a type for the request body. Then you would want to apply that type to the constant in your controller and use the enums you created to assign the values to the properties. That might look something like this:

// Potentially a class, but I've used an interface for this example
interface RequestBody {
    clientId: number;
    orderingId: number;
    receivedSignatureFlag: ReceivedSignature; // Created a seperate enum for these 2 properties   
    saturdayDeliveryFlag: SaturdayDelivery;   
    deliveryMethodOptionCode: string;         // Could potentially also be an enum
    orderItemsList: any[];                    // Using a generic type here to save time
}

enum ReceivedSignature {
    N = 'N',
    Y = 'Y',
    DIGITAL = 'DIGITAL'
}

enum SaturdayDelivery {
    N = 'N',
    Y = 'Y',
}

// This is now typed
const requestBody: RequestBody = {
    clientId: clientId,
    orderingId: 113875599,
    receivedSignatureFlag: ReceivedSignature.N,
    saturdayDeliveryFlag: SaturdayDelivery.N,
    deliveryMethodOptionCode: 'USPS',
    orderItemsList: [
        {
            itemCode: 'A123',
            transferCode: 'MAIL',
            specialHandling: 'N',
            deliveryExternalFlag: 'Y',
            orderQuantityAmount: 1,
        }
    ]
};

It should be noted though that if the flags are binary in nature (either being 'N' or 'Y') you should probably stick with primitive boolean type. Note that I've added in a DIGITAL constant to the ReceivedSignature enum, making the enum necessary for that property. However it could be argued that the enum is unnecessary for the saturdayDeliveryFlag property.

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

Comments

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.