For the following class:
module LayoutEngine {
enum DocumentFormat {
DOCX = 1
};
export class DocHeader {
public format : DocumentFormat;
}
}
I have two questions:
thanks - dave
An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).
An enum is defined using the enum keyword, directly inside a namespace, class, or structure. All the constant names can be declared inside the curly brackets and separated by a comma. The following defines an enum for the weekdays. Above, the WeekDays enum declares members in each line separated by a comma.
Enums or enumerations are a new data type supported in TypeScript. Most object-oriented languages like Java and C# use enums. This is now available in TypeScript too. In simple words, enums allow us to declare a set of named constants i.e. a collection of related values that can be numeric or string values.
In TypeScript, enums, or enumerated types, are data structures of constant length that hold a set of constant values. Each of these constant values is known as a member of the enum. Enums are useful when setting properties or values that can only be a certain number of possible values.
The above has a compile error where it says "Public property 'format' of exported class has or is using private type 'DocumentFormat'.
Simply export :
module LayoutEngine {
export enum DocumentFormat {
DOCX = 1
};
export class DocHeader {
public format : DocumentFormat;
}
}
Is there a way to place the enum declaration inside the class?
the enum
typescript type needs to be at a module level (a file or inside a module). Of course if you want it inside the class just use a json object
module LayoutEngine {
export class DocHeader {
DocumentFormat = {
DOCX: 1
};
public format : number;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With