🧩 Contents Table

νƒ€μž…μŠ€ν¬λ¦½νŠΈλ₯Ό μ‚¬μš©ν•˜λ©΄ 객체지ν–₯ν˜• ν”„λ‘œκ·Έλž˜λ°μ„ μ‚¬μš©ν•˜κΈ°μ— μš©μ΄ν•œ 점이 있음.

객체 μ§€ν–₯에 λŒ€ν•΄ μ•Œμ•„ 보기 μ•žμ„œ, ν•¨μˆ˜μ˜ μ˜€λ²„λ‘œλ”©κ³Ό μ˜€λ²„λΌμ΄λ”©μ— λŒ€ν•΄ λ¨Όμ € μ•Œμ•„λ³΄κ² μŒ.

ν•¨μˆ˜ μ˜€λ²„λ‘œλ”©μ— λŒ€ν•΄μ„œ

ν•¨μˆ˜ μ˜€λ²„λ‘œλ”© μ‹œκ·Έλ‹ˆμ²˜ overloading signature

μžλ°”μŠ€ν¬λ¦½νŠΈλŠ” νƒ€μž… μ‹œμŠ€ν…œμ΄ μ—†κ³  μΈμˆ˜μ— λŒ€ν•œ μ œν•œλ„ μ—†κΈ° λ•Œλ¬Έμ— μ˜€λ²„λ‘œλ”©μ˜ κ°œλ…μ΄ μ‘΄μž¬ν•˜μ§€ μ•ŠμŒ.

ν•˜μ§€λ§Œ νƒ€μž…μŠ€ν¬λ¦½νŠΈμ—μ„œλŠ” ν•¨μˆ˜ μ˜€λ²„λ‘œλ”©μ„ μ •μ˜ν•  수 있음.

μ΄λŠ” μ§„μ§œ μ˜€λ²„λ‘œλ”©μ€ μ•„λ‹ˆκ³  νƒ€μž…μ— λŒ€ν•œ μ •μ˜λ₯Ό μΆ”κ°€ν•˜λŠ” 것 뿐이며 μžλ°”μŠ€ν¬λ¦½νŠΈ κ΅¬ν˜„λΆ€λŠ” ν•˜λ‚˜μž„.

μ˜€λ²„λ‘œλ”© μ‹œκ·Έλ‹ˆμ²˜λŠ” νƒ€μž…μ— λŒ€ν•œ 검사λ₯Ό ν•΄μ€ŒμœΌλ‘œμ¨ 잘λͺ»λœ ν•¨μˆ˜ ν˜ΈμΆœμ„ 미리 검사해쀄 수 있음.

예λ₯Ό λ“€μ–΄ νŠΉμ • 글꼴이 폰트 λ””μžμΈ μ‹œμŠ€ν…œ 쀑 ν•˜λ‚˜λ§Œ μ‚¬μš©ν•˜λŠ” 상황이라고 κ°€μ •ν•œλ‹€λ©΄ μ•„λž˜μ™€ 같이 μ˜€λ²„λ‘œλ”©μ„ 톡해 νƒ€μž…μ—λŸ¬λ₯Ό λ°œμƒμ‹œμΌœ 잘λͺ»λœ ν•¨μˆ˜ ν˜ΈμΆœμ„ 막을 수 있음.

// 폰트 νƒ€μž… μ •μ˜
type FontType = 'Pretendard' | 'Numans';

// λ””μžμΈ μ‹œμŠ€ν…œ μŠ€νƒ€μΌ μ •μ˜
type DesignSystem = 'Body' | 'Caption';

// 각 λ””μžμΈ μ‹œμŠ€ν…œλ³„ μŠ€νƒ€μΌ μ •μ˜
interface FontStyle {
  fontSize: number;
  fontWeight: number;
  fontFamily: string;
}

// ν•¨μˆ˜ μ˜€λ²„λ‘œλ”© μ‹œκ·Έλ‹ˆμ²˜ μ •μ˜
// 1. Pretendard ν°νŠΈλŠ” Body, Caption λͺ¨λ‘ μ‚¬μš© κ°€λŠ₯
function getFontStyle(font: 'Pretendard', system: DesignSystem): FontStyle;
// 2. Numans ν°νŠΈλŠ” Caption만 μ‚¬μš© κ°€λŠ₯
function getFontStyle(font: 'Numans', system: 'Caption'): FontStyle;

const fontStyles = {
  Body: {
    fontSize: 16,
    fontWeight: 400,
  },
  Caption: {
    fontSize: 12,
    fontWeight: 300,
  },
};

const fontFamily = {
  Pretendard: "'Pretendard Variable', 'Pretendard', sans-serif",
  Numans: "'Numans', serif",
};

// μ‹€μ œ κ΅¬ν˜„λΆ€
function getFontStyle(font: FontType, system: DesignSystem): FontStyle {
  // Numans ν°νŠΈκ°€ Body μ‹œμŠ€ν…œκ³Ό μ‚¬μš©λ  경우 λŸ°νƒ€μž„ μ—λŸ¬ (μ‹€μ œλ‘œλŠ” νƒ€μž… κ²€μ‚¬μ—μ„œ 미리 λ§‰νž˜)
  if (font === 'Numans' && system === 'Body') {
    throw new Error('Numans ν°νŠΈλŠ” Body μ‹œμŠ€ν…œμ—μ„œ μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.');
  }

  return {
    fontSize: fontStyles[system].fontSize,
    fontWeight: fontStyles[system].fontWeight,
    fontFamily: fontFamily[font],
  };
}

getFontStyle('Pretendard', 'Body');
getFontStyle('Pretendard', 'Caption');
getFontStyle('Numans', 'Body');
/*
No overload matches this call.
  Overload 1 of 2, '(font: "Pretendard", system: DesignSystem): FontStyle', gave the following error.
    Argument of type '"Numans"' is not assignable to parameter of type '"Pretendard"'.
  Overload 2 of 2, '(font: "Numans", system: "Caption"): FontStyle', gave the following error.
    Argument of type '"Body"' is not assignable to parameter of type '"Caption"'.
*/
getFontStyle('Numans', 'Caption');

μΈν„°νŽ˜μ΄μŠ€ implements

μ˜€λ²„λ‘œλ”©ν•  λŒ€μƒ λ©”μ„œλ“œλ₯Ό μΈν„°νŽ˜μ΄μŠ€μ— μ„ μ–Έν•˜κ³  implements λ₯Ό 톡해 μ‹€μ œ κ΅¬ν˜„λΆ€λŠ” 각각의 ν΄λž˜μŠ€μ— μ •μ˜.

interface UserInfo {
  getDisplayName(): string;
}

class User implements UserInfo {
  constructor(
    public id: number,
    public name: string,
    public email: string
  ) {}
  
  getDisplayName(): string {
    return `${this.name} (${this.email})`;
  }
}

class Admin implements UserInfo {
  constructor(
    public id: number,
    public name: string,
    public permissions: string[]
  ) {}
  
  getDisplayName(): string {
    return `${this.name} - Admin with ${this.permissions.length} permissions`;
  }
}

function displayUserInfo(user: UserInfo): string {
  return user.getDisplayName();
}

// Example
const user = new User(1, 'zzin', '[email protected]');
const admin = new Admin(2, 'Lee', ['read', 'write', 'delete']);

console.log(getUserInfo(user)); // "zzin ([email protected])"
console.log(getUserInfo(admin)); // "Lee - Admin with 3 permissions"