타입종류
- String
- Number
- Boolean
- Object
- Array
- Tuple
- Enum
- Any
- Void
- Null
- Undefined
- Never
String
const str: string = "string";
Number
const num: number = "number";
Boolean
const bool: boolean = "boolean";
Object
const obj: { name: string; age: number } = {
name: "song",
age: 27,
};
Array
const arr: Array<number> = [1, 2, 3];
const names: Array<string> = ["song", "minseong"];
const item: number[] = [1, 2, 3];
Tuple
배열의 길이가 고정되고 각 요소의 타입을 다르게 지정
하는 형식입니다.
const tuple: [string, number] = ["seoul", 5];
Enum
- 특정 값(상수) 들의 집합을 의미합니다.
- 인덱스 번호로도 접근이 가능합니다.
enum shoes {
Nike,
Adidas,
vans,
}
const myShoes: Shoes = Shoes.Nike;
const myShoes: Shoes = Shoes[0];
Any
- 모든 타입이 입력가능한 타입니다.
- (개인적으로 쓸데없지만 쓸일이 많다는....)
const str: any = "hello";
const numb: any = 27;
const arr: any = ["hello", 2, true];
Void
- 변수에는 null과 undefined에 선언하고
- 함수의 반환값이 없을때 선언합니다.
const name: void = undefined;
function person(): void {
console.log("exit");
}