// singer : string
let singer = "Aretha";
TypeScript의 원시 타입은 JavaScript의 원시타입과 동일합니다.
원시 타입의 종류
TypeScript는 계산된 초기값을 갖는 변수의 타입을 유추할 수 있습니다.
// bestSong : string
let bestSong = Math.random() > 0.5 ? 'Chain of Fools' : 'Respect';
타입 시스템(type system)
TypeScript의 타입 시스템의 작동 과정
타입 오류가 발생한 예시
let firstName = 'Whitney';
// Error : This expression is not callable.
// Type 'Number' has no call signature
firstName.length();
// TypeScripe file
// Error: ',' expected
let let wat;
// compiled JavaScript file
let let, wat;
// Error: Property 'blub' does not exist on type 'Console'.
console.blub("Nothing is worth more than laughter.");
let firstName = 'Carole';
firstName = 'Joan';
// Error: Type 'boolean' is not assignable to type 'string'
firstName = true;
rocket; // Type: any
rocket = 'John Jett'; // Type: string
rocket.toUpperCase(); // Ok
rocket = 19.58; // Type: number
rocket.toPrecision(1); // Ok
rocket.toUpperCase(); // Error: 'toUpperCase' does not exist on type 'number'
let rocket: string;
rocket = 'Joan Jett';
// complied JavaScript file
let rocket;
rocket = 'Joan Jett';
let rocket: string;
// Error: Type 'number' is not assignable to type 'string'.
rocket = 19.58;
let firstName: string = 'Tina';
let cher = {
firstName: 'Joan',
lastName: 'Jett',
}
// Error: Property 'middleName' does not exist on type
cher.middleName;
// a.ts
export const shared = 'Cher';
// b.ts
export const shared = 'Cher';
// c.ts
// Error: Import declaration conflicts with local declaration of 'shared'.
import { shared } from './a';
const shared = 'cher';
// a.ts
// Error : Cannot redeclare blocked-scoped variable 'shared'
const shared = 'Cher';
// b.ts
// Error : Cannot redeclare blocked-scoped variable 'shared'
const shared = 'Cher';