PracticeEveryday
TypeScript 본문
TypeScript 타입
1. String : 문자열
let str: string = "hi";
: (콜론) => Type Annotation 이라고 함. Annotation : 주석
2. Number : 숫자
let num: number = 10;
3. Boolean : 진위 값 (T / F)
let isLoggedIn: boolean = True;
4. Object
let person: object = {
name: "kim",
age : 28
}
5. Array : 배열
let arr: number[] = [1, 2, 3]; // 무조건 number만 안에
let arr_str: string[] = ['a', 'b', 'c']; // 무조건 string만 안에
6. Tuple : 튜플 (길이가 고정되고 각 인덱스 별 타입이 지정되어 있는 타입
let arr: [ string, number] = ['hi', 123]
arr[0] = 1 // error
arr[1] = 'hi' // error
7. Enum : 이넘 특정값(상수)들의 집합
enum Shoes {
Nike,
Adidas,
Stn,
}
let myShoes = Shoes.Nike;
console.log(myShoes) // 0
console.log(Shoes.Adidas) // 1
초기화 하지 않으면 숫자형 이넘으로 0부터 초기화 되어 값이 할당된다.
enum Shoes {
Nike,
Adidas= 10,
Stn,
}
let myShoes = Shoes.Nike;
console.log(myShoes) // 0
console.log(Shoes.Adidas) // 10
console.log(Shoes.Stn) // 11
// 문자형 enum
enum Shoes_str {
Nike = '나이키',
Adidas= '아디다스',
// 문자열은 무조건 초기화 시켜줘야함
Stn, // error
}
console.log(Shoes_str.Nike) // '나이키'
8. any : 모든 타입 다 허용
// 자바스크립트로 구현 되어 있는 것을 옮길 때 any로 주고 점진적으로 바꾸면 좋다!
let str: any = 'hi';
str = 1 // not error
let num: any = 10;
let arr: any = ['a', 2, true];
9. void : 변수에는 undefined와 null만 할당 할 수 있고 함수에 쓸 때는 반환값이 없을 때 void를 사용한다.
undefined : 찾으시는 주소가 없습니다. 선언만 되어 있고 초기화 되지 않은 "값 자체가 없다"
null : 없는 => 명시적으로 빈값을 넣어준 것 초기화
void: 무효의
let unuseful: void = undefined;
const noReturn(): void {
console.log("Don't have return value")
}
10 Never : 무한루프에 빠질 때
const neverDie(): never {
while(true) {
}
}
기본 타입 | 타입스크립트 핸드북
타입스크립트 기본 타입 타입스크립트로 변수나 함수와 같은 자바스크립트 코드에 타입을 정의할 수 있습니다. 타입스크립트의 기본 타입에는 크게 다음 12가지가 있습니다. Boolean Number String Obj
joshua1988.github.io
TS Playground - An online editor for exploring TypeScript and JavaScript
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
www.typescriptlang.org
'JavaScript > TypeScript' 카테고리의 다른 글
TypeScript (0) | 2022.06.14 |
---|---|
TypeScript (0) | 2022.06.10 |
TypeScript (0) | 2022.06.10 |
TypeScript (0) | 2022.06.04 |
TypeScript (0) | 2022.04.30 |
Comments