Single Choice Easy

QWhich of the following is the right way of defining enum?

ID: #19088 Data Type Classification 88 views
Question Info
#19088Q ID
EasyDifficulty
Data Type ClassificationTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A enum Enum {}
  • B const enum DNA {}
  • C declare enum Enum {}
  • D All the options
Correct Answer

Explanation

All the options you provided are valid ways of defining an enum in TypeScript, but they serve different purposes:

  1. enum Enum {}: This is a standard way of defining an enum in TypeScript. It creates an enum named Enum with no values. You can add enum members inside the curly braces.

    
    enum Enum {
      Value1,
      Value2,
      Value3,
    }
    
    
  2. const enum DNA {}: This defines a const enum, where the enum is inlined at compile time. This means that the enum values are directly substituted in the generated JavaScript code, and no runtime object is created for the enum.
    
    const enum DNA {
      Adenine,
      Thymine,
      Cytosine,
      Guanine,
    }
    
    

    Note: Const enums cannot have computed or non-constant enum members.

  3. declare enum Enum {}: This is used to declare an enum that may be defined elsewhere (e.g., in an external JavaScript library). It tells TypeScript to expect that the enum is defined at runtime, even though the actual definition may not be available in the TypeScript code.
    
    declare enum Enum {
      Value1,
      Value2,
      Value3,
    }
    
    

Choose the option that best suits your needs based on whether you want a standard enum, a const enum, or if you're declaring an enum that's defined elsewhere.

No Previous Next Question

Share This Question

Challenge a friend or share with your study group.

Related MCQ Questions