TypeScript type - 기본 종류

hyeyeong lee·2023년 7월 26일

'type 안정성'은 code가 예상한 type대로 동작함을 보장하는 것



boolean

참(true), 거짓(false)
조건문, 비교 연산


number

모든 숫자

*일반 programming 언어
정수: short, int, long
실수: float, double


string

text


배열

기본 type에 [ ]를 붙인 형태


tuple

여러 type의 원소

data type의 개수와 순서 일치 필수
배열 method push 사용 지향


enum

열거형 data type

명확하게 관련된 상수들을 group화
number 혹은 string type의 값만 할당 가능
값이 설정되어 있지 않으면 숫자 0으로 시작





가변적 type


any

super type

JavaScript의 object type과 같은 최상위 type


unknown

any type 보다 안정

다른 type의 변수에 할당하려면 명시적으로 type을 확인과 안정성 보장을 위한 재할당 필요

// Type Assertion(type 단언)
stringValue = unknownValue as string;
// typeof keyword를 이용한 type check
typeof unknownValue === 'string'

// 이후 unknown type의 변수를 string type의 변수에 할당

union

여러 type 중 하나를 가질 수 있는 변수를 선언할 때 사용
| 연산자로 type들을 결합하여 표현



가변적 data type 문제점

code의 안정성과 유지 보수성 저해 가능성

0개의 댓글