TypeScript

πŸ“Œ Type Aliasλž€?

Type AliasλŠ” κΈ°μ‘΄ νƒ€μž…μ— 별칭(Alias) 을 λΆ€μ—¬ν•˜λŠ” κΈ°λŠ₯μž…λ‹ˆλ‹€.
객체뿐만 μ•„λ‹ˆλΌ μœ λ‹ˆμ˜¨ νƒ€μž…, ν•¨μˆ˜ νƒ€μž…, νŠœν”Œ λ“± λ‹€μ–‘ν•œ νƒ€μž…μ„ μ •μ˜ν•  λ•Œ μ‚¬μš©λ©λ‹ˆλ‹€.

βœ… κΈ°λ³Έ 예제

// κΈ°λ³Έ νƒ€μž…μ— 별칭 λΆ€μ—¬
type UserId = number;
type UserName = string;
type IsActive = boolean;

let userId: UserId = 12345;
let userName: UserName = "홍길동";
let isActive: IsActive = true;

βœ… 객체 νƒ€μž… μ •μ˜

// μ£Όλ¬Έ μƒνƒœ νƒ€μž… μ •μ˜
type OrderStatus = "pending" | "processing" | "shipped" | "delivered" | "canceled";

type Order = {
  id: number;
  productId: number;
  customer: string;
  quantity: number;
  status: OrderStatus;
  orderDate: Date;
};

// μ‚¬μš© 예
const order1: Order = {
  id: 1001,
  productId: 5,
  customer: "이지은",
  quantity: 2,
  status: "pending",
  orderDate: new Date(),
};

βœ… μœ λ‹ˆμ˜¨ νƒ€μž…μ„ ν™œμš©ν•˜λ©΄ μ œν•œλœ κ°’λ§Œ μ‚¬μš©ν•  수 μžˆμ–΄ μ•ˆμ „ν•œ μ½”λ“œ μž‘μ„±μ΄ κ°€λŠ₯!

βœ… ν•¨μˆ˜μ™€ λ©”μ„œλ“œμ— Type Alias μ‚¬μš©

// ν•¨μˆ˜ νƒ€μž… 별칭
type Calculator = (a: number, b: number) => number;

const add: Calculator = (x, y) => x + y;
const multiply: Calculator = (x, y) => x * y;

console.log(add(10, 20));  // 30
console.log(multiply(10, 20));  // 200

πŸ“Œ 객체 readonly 속성

읽기 μ „μš© 속성을 μ‚¬μš©ν•˜λ©΄ 객체의 λΆˆλ³€μ„±(immutability) 을 μœ μ§€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

type User = {
  readonly id: number;
  readonly createdAt: Date;
  name: string;
  age: number;
};

const user: User = {
  id: 12345,
  createdAt: new Date(),
  name: "홍길동",
  age: 30,
};

// κ°€λŠ₯: 일반 속성 μˆ˜μ •
user.name = "κΉ€μ² μˆ˜";
user.age = 31;

// μ—λŸ¬: readonly 속성 μˆ˜μ • λΆˆκ°€
// user.id = 67890;
// user.createdAt = new Date();

βœ… 읽기 μ „μš© 속성을 ν™œμš©ν•˜λ©΄ 데이터λ₯Ό λ³΄ν˜Έν•  수 있음!


πŸ“Œ Interfaceλž€?

InterfaceλŠ” 객체의 ꡬ쑰λ₯Ό μ •μ˜ν•˜λŠ” μš©λ„λ‘œ μ‚¬μš©λ©λ‹ˆλ‹€. type alias와 μœ μ‚¬ν•˜μ§€λ§Œ, extendsλ₯Ό 톡해 λ‹€λ₯Έ μΈν„°νŽ˜μ΄μŠ€λ₯Ό ν™•μž₯ν•  수 있으며, ν΄λž˜μŠ€μ—μ„œλ„ implements ν‚€μ›Œλ“œλ‘œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

βœ… κΈ°λ³Έ 예제

interface Car {
  model: string;
  price: number;
  tax(): number;
}

const myCar: Car = {
  model: "Tesla",
  price: 5000,
  tax() {
    return this.price * 0.1;
  },
};

βœ… Interface ν™•μž₯ (extends)

interface Animal {
  name: string;
}

interface Dog extends Animal {
  breed: string;
}

const myDog: Dog = {
  name: "바둑이",
  breed: "골든 λ¦¬νŠΈλ¦¬λ²„",
};

βœ… **extends**λ₯Ό μ‚¬μš©ν•˜μ—¬ κΈ°μ‘΄ μΈν„°νŽ˜μ΄μŠ€λ₯Ό ν™•μž₯ν•  수 있음!


πŸ“Œ Type Alias vs Interface 비ꡐ

비ꡐ ν•­λͺ© Type Alias Interface
객체 ꡬ쑰 μ •μ˜ βœ… κ°€λŠ₯ βœ… κ°€λŠ₯
ν™•μž₯ 방법 & μ—°μ‚°μž μ‚¬μš© extends ν‚€μ›Œλ“œ μ‚¬μš©
μœ λ‹ˆμ˜¨ νƒ€μž… μ‚¬μš© βœ… κ°€λŠ₯ ❌ λΆˆκ°€λŠ₯
ν•¨μˆ˜ νƒ€μž… μ •μ˜ βœ… κ°€λŠ₯ βœ… κ°€λŠ₯
쀑볡 μ„ μ–Έ ❌ λΆˆκ°€λŠ₯ βœ… μžλ™ 병합 κ°€λŠ₯

πŸ“Œ Class (클래슀)λž€?

TypeScriptμ—μ„œ 클래슀(Class) λŠ” 객체λ₯Ό λ§Œλ“€κΈ° μœ„ν•œ 섀계도(ν…œν”Œλ¦Ώ) μž…λ‹ˆλ‹€.
μ½”λ“œλ₯Ό μž¬μ‚¬μš©ν•˜κ³ , κ΅¬μ‘°ν™”ν•˜μ—¬ 효율적인 ν”„λ‘œκ·Έλž˜λ°μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€.

βœ… κΈ°λ³Έ 클래슀 μ •μ˜

class Car {
  model: string;
  price: number;

  constructor(model: string, price: number) {
    this.model = model;
    this.price = price;
  }

  tax(): number {
    return this.price * 0.1;
  }
}

βœ… 클래슀 상속 (Extends)

class Animal {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
  sound() {
    console.log(`${this.name}이(κ°€) μ†Œλ¦¬λ₯Ό λƒ…λ‹ˆλ‹€.`);
  }
}

class Dog extends Animal {
  bark() {
    console.log(`${this.name}이(κ°€) 멍멍! μ§–μŠ΅λ‹ˆλ‹€.`);
  }
}

βœ… λΆ€λͺ¨ 클래슀의 κΈ°λŠ₯을 μœ μ§€ν•˜λ©΄μ„œ μƒˆλ‘œμš΄ κΈ°λŠ₯ μΆ”κ°€ κ°€λŠ₯!


🎯 정리: μ–Έμ œ type, interface, classλ₯Ό μ‚¬μš©ν• κΉŒ?

βœ” 객체 ꡬ쑰λ₯Ό μ •μ˜ν•  λ•Œ β†’ interface μ‚¬μš© (ν™•μž₯ κ°€λŠ₯)
βœ” μœ λ‹ˆμ˜¨ νƒ€μž…, νŠœν”Œ, ν•¨μˆ˜ νƒ€μž…μ„ μ •μ˜ν•  λ•Œ β†’ type alias μ‚¬μš©
βœ” 클래슀λ₯Ό μ‚¬μš©ν•  λ•Œ, μΈν„°νŽ˜μ΄μŠ€λ₯Ό ν™œμš© β†’ interface + implements μ‚¬μš©
βœ” ν΄λž˜μŠ€μ—μ„œ 곡톡 속성을 물렀받을 λ•Œ β†’ extends μ‚¬μš©


πŸŽ‰ 마무리

이번 κΈ€μ—μ„œλŠ” TypeScriptμ—μ„œ 자주 μ‚¬μš©λ˜λŠ” type alias, interface, class의 κ°œλ…κ³Ό 차이점을 μ‚΄νŽ΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€. 각각의 κ°œλ…μ„ 잘 ν™œμš©ν•˜λ©΄ 더 μ•ˆμ „ν•œ νƒ€μž… 기반 μ½”λ“œλ₯Ό μž‘μ„±ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 🎯