Enumerations
const myEnum = {
Up : 'Up string',
Down : 'Down string',
Right : 'Right string',
Left : 'Left string'
}
let myValue: number = myEnum.Up;
Returns an array
Object.keys(myEnum)
Check if a value equals
=== myEnum.Down
Check if a value is in the enumeration
myEnum.hasOwnProperty('Up');
Object.Freeze
However it is possible in your code to redefine these constants
myEnum.Up = 'change the text';
To prevent the value from being changed we can freeze the enumeration
const myEnum = Object.freeze({
Up : 'Up string',
Down : 'Down string',
Right : 'Right string',
Left : 'Left string'
});
Type Literals
Do not use enums, just use type literals ?
type Colors = 'red | 'blue' | 'green'
TypeScript Enumerations
At the moment the "enum" keyword has not been added to JavaScript
TypeScript Enumerations
© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext