Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

typescript type from enum values

enum Weekday {
  MONDAY = 'mon',
  TUESDAY = 'tue',
  WEDNESDAY = 'wed'
}

type WeekdayType = `${Weekday}`;
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 :: typescript extend type 
Typescript :: await constructor typescript 
Typescript :: basic variable typescript 
Typescript :: disable out of stock products shopify 
Typescript :: factory design pattern typescript 
Typescript :: typescript pick type from interface 
Typescript :: call function dynamically typescript 
Typescript :: typescript parameter function type 
Typescript :: filename requests python 
Typescript :: comments visual studio code html 
Typescript :: concat type typescript 
Typescript :: ordenar por fecha arreglo de objetos typescript 
Typescript :: Custom validation for phone-number using class-validator package 
Typescript :: dota 2 space to center hero 
Typescript :: clean broken shortcuts in windows start menu 
Typescript :: preventing +,-,e from input ts 
Typescript :: how to show array of objects in flatlist react native 
Typescript :: get top elements from a list python 
Typescript :: paragraph dots after 2 lines css 
Typescript :: typescript var global: typeof globalThis 
Typescript :: typescript string 
Typescript :: TypeScript Example Code Snippet 
Typescript :: this typescript 
Typescript :: Request exceeded the limit of 10 internal redirects due to probable configuration error 
Typescript :: ansible facts suse 
Typescript :: type to string typescript 
Typescript :: typescript wrapping for array 
Typescript :: pass multiple arguments to thread python 
Typescript :: formatting to six digits in python 
Typescript :: Will Tenet come in plate format 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =