
1. 불리언 (boolean)
let result: boolean = true;
2. 숫자 (number)
let count: number = 5;
3. 문자열 (string)
- 큰 따옴표나 작은 따옴표로 감싼다.
- 백틱( ` )으로 감싸면 ${ } 형태로 변수를 포함시킬 수 있다.
let name: string = 'Noah';
let sentence: string = `Hi, my name is ${name}`;
4. 배열 (array)
배열 타입은 두 가지 방법으로 쓸 수 있다.
- 타입 뒤에 [ ] 쓰기
- Array<타입>
let list1: number[] = [1, 2, 3];
let list2: Array<string> = ['Noah', 'Jamie'];
5. 튜플, enum
유니온 타입으로 치환 가능하다.
2021.04.13 - [TypeScript] 유니온 타입은 언제 사용하는가?
6. any
- 알지 못하는 타입을 표현하기 위함.
- 타입의 일부만 알고 전체는 알지 못할 때.
let notSure: any = 3;
let list: any[] = ['Noah', 2, false]
7. void
보통 함수에서 반환 값이 없을 때, 반환 타입을 표현하기 위해 사용.
const func = (): void => {
console.log(1)
};
8. null, undefined
각자 자기 타입에만 할당 가능.
let u: undefined = undefined;
let n: null = null;
9. never
never를 반환하는 함수는 함수의 마지막에 도달할 수 없다.
const error = (message: string): never => {
throw new Error(message);
}
const infiniteLoop = (): never => {
while (true) { }
}
10. 객체
원시타입(숫자, 문자열, 불리언, null 등)이 아닌 모든 타입을 나타냄.
type apiResponse = object | null;
const result1: apiResponse = { prop: 0 };
const result2: apiResponse = null;
'TypeScript' 카테고리의 다른 글
[TypeScript] 제네릭을 사용하는 이유 (0) | 2021.04.15 |
---|---|
TypeScript로 이해하는 객체지향 (0) | 2021.04.14 |
[TypeScript] 유니온 타입은 언제 사용하는가? (0) | 2021.04.13 |
TypeScript에서 추가된 함수 기능들! (0) | 2021.04.13 |
Type Assertion(타입 단언)을 사용하는 이유 (0) | 2021.04.10 |