<aside> 💡 타입스크립트?
JavaScript With Syntax For Types.
TypeScript is JavaScript with syntax for types.
TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.
타입스크립트는 문법적으로도 자바스크립트의 상위집합이다.
자바스크립트 파일은 .js(또는 .jsx) 확장자를 사용하고, 타입스크립트는 .ts(또는 .tsx) 확장자를 사용한다.
main.js
파일명을 main.ts
로 바꾼다고 해도 달라지는 건 없으며, 이는 마이그레이션 하는 데 엄청난 이점이 된다. (기존 코드를 그대로 유지하면서 일부분만 타입스크립트 적용이 가능하기 때문에)let city = 'new york city';
console.log(city.**toUppercase()**);
// Property 'toUppercase' does not exist on type 'string'.
// Did you mean 'toUpperCase'?
<aside> 💡 타입스크립트 에러 이쁘게 보기
Pretty TypeScript Errors </aside>
타입 시스템의 목표 중 하나는 런타임에 오류를 발생시킬 코드를 미리 찾아 내는 것이다.
const states = [
{ name: 'Alabama', capital: 'Montgomery' },
{ name: 'Alaska', capital: 'Juneau' },
{ name: 'Arizona', capital: 'Phoenix' },
];
for (const state of states) {
console.log(state.capitol);
}
// undefined
// undefined
// undefined
// TypeScript
for (const state of states) {
console.log(state.**capitol**);
// Property 'capitol' does not exist on type '{ name: string; capital: string; }'.
// Did you mean 'capital'?
}
const states = [
{ name: 'Alabama', capitol: 'Montgomery' },
{ name: 'Alaska', capitol: 'Juneau' },
{ name: 'Arizona', capitol: 'Phoenix' },
];
for (const state of states) {
console.log(state.**capital**); // ✅ 어느 쪽이 오타인지 알 수 없다.
// Property 'capital' does not exist on type '{ name: string; capitol: string; }'.
// Did you mean 'capitol'?
}
interface State {
name: string;
capital: string;
}
const states: State[] = [
{ name: 'Alabama', **capitol: 'Montgomery'** },
{ name: 'Alaska', **capitol: 'Juneau'** },
{ name: 'Arizona', **capitol: 'Phoenix'** },
// Object literal may only specify known properties, but 'capitol' does not exist in type 'State'.
// Did you mean to write 'capital'?
];
for (const state of states) {
console.log(state.capital);
}