typescript enum value to enum code example

Example 1: declare enum in type script

enum PrintMedia {
  Newspaper = 1,
  Newsletter,
  Magazine,
  Book
}

Example 2: ts enum

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

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

respond("Princess Caroline", Response.Yes)

Example 3: typescript enum value to enum

enum Number {
  One,
  Two
}  

const numberOne: Number = "One" as Number;
const otherNumberOne: Number = Number.One;

const stringOne = Number[Number.One];

Tags:

Misc Example