ts -> static types
개발하는 중간에 타입을 체크함
js -> dynamic types
개발할때는 알수없고 런타임에 돌입해야지만 잘못된 코드를 확인할 수 있다.
function record(name, grade) {
if (typeof name !== "string" || typeof grade !== "number") {
throw new Error("Incorrect input");
}
return `name : ${name} grade : ${grade}`;
}
function record(name:string, grade:number) {
return `name : ${name} grade : ${grade}`;
}
런타임 체크할필요없이 개발중에 체크할수있기때문에 한 단계 먼저 에러를 확인할 수 있다.
TypeScript의 기본 자료형
-JS의 기본자료형을 포함
- Boolean (Primitive Type)
- Number (Primitive Type)
- String (Primitive Type)
- Null , Undefined (Primitive Type)
- Symbol (Primitive Type)
- Array : object형
Primitive Type
new Boolean(false);// typeof new Boolean(false) : 'object'
new String('world');// typeof new String('world') : 'object'
new Number(42);// typeof new Number(42) : 'object'
-프로그래밍을 도울 몇가지 추가적인 타입
- Any, Void, Never, UnKnown
- Enum
- Tuple: object 형