라벨이 배열 타입인 게시물 표시

[Typescript] 기본 타입 정리

이미지
  원시 타입 let num : number = 555 ; let str : string = ' this is string ' ; let bool : boolean = true ; let nullType : null = null; let undefinedType : undefined = undefined; let symbolType : symbol = Symbol ( ' mySymbol ' ) ; 배열 타입 let arr : number [] = [ 1 , 2 , 3 ] ; let arr1 : Array < number > = [ 1 , 2 , 3 ] ; let arr3 : Array < number | string | boolean > = [ 1 , true , ' sss ' ] ; Tuple(튜플) 튜플은 배열의 길이가 고정되고 각 요소의 타입이 지정되어 있는 배열 형식을 의미한다. 주의할 점은 튜플의 경우 push하는 행위는 막지 못한다. let tuple : [ boolean , number , string ] = [ true , 111 , ' 11 ' ] ; 상수 일반적으로 const로 상수를 선언하면 값을 변경하거나 재할당 할 수 없다. 하지만 객체와 배열의 경우 const로 상수를 선언하였다 하더라도 아래와 같이 값의 재할당이 가능하다. 이러한 재할당을 방지하여 완전한 변경없는 객체를 만들고자 할 때에는 as const 키워드를 사용하면 된다. const obj = { a : 1 , b : 2 }; obj . a = 3 ; //값을 재할당 할 수 있다. const objAsConst = { a : 1 , b : 2 } as const ; objAsConst . a = 3 ; //Error 발생 Enum enum 멤버에 숫자로 값을 할당하면 1씩