아이템 1. 타입스크립트와 자바스크립트의 관계 이해하기(p.2~p.9)

타입스크립트란?

<aside> 💡 타입스크립트?

JavaScript With Syntax For Types.

타입 체커

let city = 'new york city';
console.log(city.**toUppercase()**);
// Property 'toUppercase' does not exist on type 'string'. 
// Did you mean 'toUpperCase'?

<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);
}