🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this example shows how to create and use TypeScript string enums with examples.
In TypeScript, enums are set of named constants. Though it is optional, enums should be a named group of related constants.
Typescript String-based Enums
In a string enum, each member has to be constant-initialized with a string literal, or with another string enum member.
Check out enums in Java at Java Enums Tutorial.
Enum syntax is very similar to other languages e.g. Java.
// Example 2 - String-based Enums
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
Typescript String-based Enums Examples
Example 1: How to create String-based enum in Typescript
Let's create a typescript file named enums.ts and add following code to it:
// Example 2 - String-based Enums
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
// Access enum member values
console.log(Direction.Up);
console.log(Direction.Down);
console.log(Direction.Left);
console.log(Direction.Right);
console.log(Direction['Up']);
console.log(Direction['Down']);
Example 2: How to access enum member values
Use Enum.member or Enum['member'] format to access enum member values. For Example:
console.log(Direction.Up);
console.log(Direction.Down);
console.log(Direction['Up']);
console.log(Direction['Down']);
Let's run above typescript file using the following command:
c:\user\typescript-tutorial> tsc .\enums.ts
Look at the generated code in JavaScript. It has a generated lookup table like this.
var Direction;
(function (Direction) {
Direction["Up"] = "UP";
Direction["Down"] = "DOWN";
Direction["Left"] = "LEFT";
Direction["Right"] = "RIGHT";
})(Direction || (Direction = {}));
// Access enum member values
console.log(Direction.Up);
console.log(Direction.Down);
console.log(Direction.Left);
console.log(Direction.Right);
console.log(Direction['Up']);
console.log(Direction['Down']);
The above file created as enums.js file. Now we don't need a browser, we can use node.js to run this javascript file. Let's run this enums.js file using the following command:
c:\user\typescript-tutorial> node .\enums.js
UP
DOWN
LEFT
RIGHT
UP
DOWN
Follow similar steps to run below typescript examples.
Example 3: LogLevel Enum
// Example 1- String-based Enums
export enum LogLevel {
Off = "Off",
Fatal = "Fatal",
Error = "Error",
Warn = "Warn",
Info = "Info",
Debug = "Debug"
}
// Access enum member values
console.log(LogLevel.Debug);
console.log(LogLevel.Error);
console.log(LogLevel.Warn);
console.log(LogLevel.Info);
console.log(LogLevel.Fatal);
console.log(LogLevel['Fatal']);
console.log(LogLevel['Debug']);
console.log(LogLevel['Info']);
Example 4: Weekday Enum
// Example 3 - String-based Enums
enum Weekday {
Monday = "Monday",
Tuesday = "Tuesday",
Wednesday = "Wednesday",
Thursday = "Thursday",
Friday = "Friday",
Saturday = "Saturday",
Sunday = "Sunday"
}
// Access enum member values
console.log(Weekday.Monday);
console.log(Weekday.Tuesday);
console.log(Weekday.Wednesday);
console.log(Weekday.Thursday);
console.log(Weekday.Friday);
console.log(Weekday.Saturday);
console.log(Weekday.Sunday);
console.log(Weekday['Saturday']);
console.log(Weekday['Sunday']);
Example 5: Enum as a function argument
To pass enum in functions, declare the argument type of enum itself.
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
function checkDirection(direction: Direction){
console.log(direction);
}
checkDirection(Direction.Up);
checkDirection(Direction.Down);
checkDirection(Direction.Left);
checkDirection(Direction.Right);
Output:
C:\user\typescript-tutorial> tsc .\enums.js
C:\user\typescript-tutorial> node .\enums.js
UP
DOWN
LEFT
RIGHT
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment