π§©Β Contents Table
νμ μ€ν¬λ¦½νΈλ₯Ό μ¬μ©νλ©΄ κ°μ²΄μ§ν₯ν νλ‘κ·Έλλ°μ μ¬μ©νκΈ°μ μ©μ΄ν μ μ΄ μμ.
κ°μ²΄ μ§ν₯μ λν΄ μμ 보기 μμ, ν¨μμ μ€λ²λ‘λ©κ³Ό μ€λ²λΌμ΄λ©μ λν΄ λ¨Όμ μμλ³΄κ² μ.
μλ°μ€ν¬λ¦½νΈλ νμ μμ€ν μ΄ μκ³ μΈμμ λν μ νλ μκΈ° λλ¬Έμ μ€λ²λ‘λ©μ κ°λ μ΄ μ‘΄μ¬νμ§ μμ.
νμ§λ§ νμ μ€ν¬λ¦½νΈμμλ ν¨μ μ€λ²λ‘λ©μ μ μν μ μμ.
μ΄λ μ§μ§ μ€λ²λ‘λ©μ μλκ³ νμ μ λν μ μλ₯Ό μΆκ°νλ κ² λΏμ΄λ©° μλ°μ€ν¬λ¦½νΈ ꡬνλΆλ νλμ.
μ€λ²λ‘λ© μκ·Έλμ²λ νμ μ λν κ²μ¬λ₯Ό ν΄μ€μΌλ‘μ¨ μλͺ»λ ν¨μ νΈμΆμ 미리 κ²μ¬ν΄μ€ μ μμ.
μλ₯Ό λ€μ΄ νΉμ κΈκΌ΄μ΄ ν°νΈ λμμΈ μμ€ν μ€ νλλ§ μ¬μ©νλ μν©μ΄λΌκ³ κ°μ νλ€λ©΄ μλμ κ°μ΄ μ€λ²λ‘λ©μ ν΅ν΄ νμ μλ¬λ₯Ό λ°μμμΌ μλͺ»λ ν¨μ νΈμΆμ λ§μ μ μμ.
// ν°νΈ νμ
μ μ
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"