TypeScript : 기본 타입

문자열 타입: string

1
let name: string = 'Park';

숫자 타입: number

1
let age: number = 100;

진위 타입: boolean

1
let isMan: boolean = true;

객체 타입: object

1
let areas: object = { name : "SEOUL", location : "KOR" }

배열 타입: Array

1
2
3
4
5
6
7
8
9
10
// 문자열 배열
let fruits: Array<string> = ['사과','딸기','바나나'];
let fruits: string[] = ['딸기','바나나','사과'];

// 숫자 배열
let numbers: Array<number> = [1,2,3];
let numbers: number[] = [3,2,1];

// 읽기 전용 배열
let arr: ReadonlyArray<number> = [1,2,3];

튜플 타입: tuple

튜플은 특정한 형태를 갖는 배열입니다.

1
let items: [string, number] = ['hello',1];

any

1
2
let name: any = 'Park';
let age: any = 100;

enum

특정 값들의 집합입니다.

1
2
3
4
5
enum fruits {
banana,
apple,
orange
}

enum은 기본적으로 숫자로 접근할 수 있습니다.

1
let most: fruits = fruits[0];

임의로 index 값을 변경할 수도 있습니다.

1
2
3
4
5
6
7
8
enum fruits {
banana,
apple = 2,
orange
}

console.log(fruits["banana"]); // 0
console.log(fruits["apple"]); // 2

문자형 enum

1
2
3
4
5
6
7
8
enum Arrows {
UP = "UP",
DOWN = "DOWN",
LEFT = "LEFT",
RIGHT = "RIGHT"
}

console.log(Arrows["UP"]); // UP

null, undefined

null과 undefined도 정의 할 수 있습니다.

1
2
let empty: null = null;
let nothing: undefined;