typescript number to enum code example

Example 1: typescript enum to array

// Helper
const StringIsNumber = value => isNaN(Number(value)) === false;

// Turn enum into array
function ToArray(enumme) {
    return Object.keys(enumme)
        .filter(StringIsNumber)
        .map(key => enumme[key]);
}

Example 2: typescript enum to string

enum AnEnum {
    One = 1,
    Two = 2
}
let stringOne = AnEnum[1]; // "One"
let stringTwo = AnEnum[AnEnum.Two]; // "Two"

Example 3: enum in ts

enum Direction {
    Up,
    Down,
    Left,
    Right,
}

Example 4: ts enum

enum Response {
    No = 0,
    Yes = 1,
}

function respond(recipient: string, message: Response): void {
    // ...
}

respond("Princess Caroline", Response.Yes)

Example 5: enum to number typescript

enum UserResponse {
  No = 0,
  Yes = 1,
}
console.log(UserResponse.Yes) //logs : 1