Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

enums in typescript

enum Direction {
  Up = 1,
  Down,
  Left,
  Right,
}
Comment

typescript enum

enum Sides {LEFT, RIGHT};
Sides.LEFT;  // 0
Sides.RIGHT; // 1

const typeFromEnum: Sides.LEFT = Sides.LEFT; // Enums become types!

console.log(Sides);   // { '0': 'LEFT', '1': 'RIGHT', LEFT: 0, RIGHT: 1 } 

type leftOrRight = keyof typeof Sides; // 'LEFT' | 'RIGHT'

let sideName: string = Sides[0];  // 'LEFT'  reverse mapping


enum EnumWithString {
  X = "XX",
  Y = "YY",
};
console.log(EnumWithString); // { X: 'XX', Y: 'YY' }  no reverse mapping
Comment

TypeScript enum

enum Direction {
  Up = 1,
  Down,
  Left,
  Right,
}
We’ll first start off with numeric enums, which are probably more familiar if you’re coming from other languages. An enum can be defined using the enum keyword.

If we wanted, we could leave off the initializers entirely:
enum Direction {
  Up,
  Down,
  Left,
  Right,
}
  
enum Direction {
  Up = "UP",
  Down = "DOWN",
  Left = "LEFT",
  Right = "RIGHT",
}
Comment

typescript enum

enum EMoney {
	gopay = 'gopay',
	dana = 'dana',
	ovo = 'ovo'
}

enum Bank {
	bca = 'bca',
	mandiri = 'mandiri',
	bri = 'bri'
}

interface OnlineShop<T> {
	payment: T
}

const payment: OnlineShop<Bank> = {
	payment: Bank.bca
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: ts async function type 
Typescript :: alphabets range using re 
Typescript :: declare array typescript 
Typescript :: linux copy all directory contents to another directory 
Typescript :: js pop object from id 
Typescript :: outputs i angular 
Typescript :: typeorm select join column querybuilder 
Typescript :: i comparer for lists c# 
Typescript :: angular loadchildren lazy loading 
Typescript :: Scripts cannot be executed on this system. 
Typescript :: how to add enchantments to mobs plugin 
Typescript :: how to add custom snippets in emmet in visual studio code 
Typescript :: order documents in firestore 
Typescript :: nest js http exceptions 
Typescript :: uat testing vs system testing 
Typescript :: typescript discriminated unions 
Typescript :: delete array typescript 
Typescript :: join elements in a list with , java 
Typescript :: date formats in mongodb 
Typescript :: linux bash scripts tutorial 
Typescript :: callback ref typescript 
Typescript :: Custom _App with getInitialProps typescript example 
Typescript :: idle angular 15 menute 
Typescript :: multiple hosts in same role 
Typescript :: get database num hits django 
Typescript :: fputs c++ 
Typescript :: What is the reason we are using properties file 
Typescript :: which network device reads the source and destination MAC addresses, looks up the destination to determine where to send the frame, and forwards it out to the correct port 
Typescript :: how many energy levels are there 
Typescript :: cannot find name describe jasmine 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =