-
enum 타입,Typescript 2023. 1. 16. 21:20
enum 타입
- 자바스크립트에는 없고, 타입스크립트에만 있는 타입
- enumeration의 줄임말
- 숫자형 enum
enum Direction { Up, // = 0 Down, // = 1 Left, // = 2 Right, // = 3 } console.log(Direction.UP, Direction.Down, Direction.Left) // 결과 : 0 1 2 const up: Direction = Direction.Up; // Direction = Direction.UP | Direction.Down | Direction.Left | Direction.Right const leftOrRight: Direction.Left | Direction.Right = Direction.Left;
enum Direction { Up, Down, Left, Right, } const up: Direction = Direction.Up; // Direction = Direction.UP | Direction.Down | Direction.Left | Direction.Right const leftOrRight: Direction.Left | Direction.Right = Direction.Left; console.log(Direction[2]); // 결과 : Left
- 문자형 enum
- 문자만 할당할 수 있고 숫자형과는 다르게 숫자를 주게되면 다음 숫자가 +1 늘어나고 그런건 없다.
enum Direction { Up = "UP", Down = "DOWN", Left = "LEFT", Right = "RIGHT", }
- 복합 enum
enum BooleanLikeHeterogeneousEnum { No = 0, Yes = "YES", } // 권고하지 않음
'Typescript' 카테고리의 다른 글
함수 타입 (0) 2023.01.16 기본 타입 (0) 2023.01.16 Typescript 설치 및 세팅 (0) 2023.01.16