2024-05-11.md
π‘DIL: μ΄νν°λΈ νμ μ€ν¬λ¦½νΈ
μ€ν°λ: μκ° CS, https://github.com/monthly-cs/2024-05-effective-typescript
μμ±μΌ: 2024-05-11
μμ±μ: dusunax
PPT μ½λ μ 리
function registerCallback(callback: () => void) {
if (callback) {
callback();
} else {
console.log("No callback provided.");
}
}
const button = document.getElementById("myButton");
registerCallback(() => {
button &&
button.addEventListener("click", (e) => {
e.stopPropagation();
console.log("Button clicked!");
});
});
/**
* - μ€λ₯κ° λ°μν κΉμ?
* - μ΄λ€ μμΉμμ λ°μν κΉμ?
* - μ΄μ λ 무μμΈκ°μ?
* */
// κ²½μ° A
function λ_λλκΈ°_A(input) {
return input / 2;
}
console.log(λ_λλκΈ°_A("10"));
// κ²½μ° B
function λ_λλκΈ°_B(input: number) {
return input / 2;
}
console.log(λ_λλκΈ°_B("10"));
// κ²½μ° C
function λ_λλκΈ°_C(input: number) {
return input / 2;
}
console.log(λ_λλκΈ°_C("10" as any));
function calculateArea(shape: Shape): number {
if (shape.kind === "circle") {
return Math.PI * shape.radius ** 2;
} else if (shape.kind === "square") {
return shape.width ** 2;
} else {
throw new Error("Unknown shape kind");
}
}
function calculateArea(shape: Shape): number {
if ("radius" in shape) {
return Math.PI * shape.radius ** 2;
} else if ("width" in shape) {
return shape.width ** 2;
} else {
throw new Error("Unknown shape kind");
}
}
function format(input: number): string; // μ«μ νμ
function format(input: string): string; // λ¬Έμμ΄ νμ
function format(input: any): string {
if (typeof input === "number") {
return `Number: ${input}`;
} else if (typeof input === "string") {
return `String: ${input}`;
} else {
throw new Error("Unsupported type");
}
}
const formattedNumber = format(42); // "Number: 42"
const formattedString = format("Hello"); // "String: Hello"
console.log(formattedNumber, formattedString);
any: λ¬Έμ μΌ λ¬Έμ
interface UserData {
id: string;
name: string;
height: string;
}
interface DataProvider {
fetchData: (userId: string) => any;
}
class UserApi implements DataProvider {
private data: any;
constructor() {
this.data = {};
}
setData(data: UserData) {
this.data = data;
}
fetchData(userId: string) {
// λ€νΈμν¬ μμ²~
// μ€μ μλ리μ€μμλ μ¬μ©μ IDλ₯Ό κΈ°λ°μΌλ‘ APIμμ μ¬μ©μ λ°μ΄ν°λ₯Ό κ°μ Έμ¬ κ²
return {
id: userId,
name: "κΉμ μ ",
height: "180.5",
};
}
}
const userApi: DataProvider = new UserApi();
const fetchedUserData = userApi.fetchData("μ μ μμ΄λ");
console.log(`User Height: ${fetchedUserData.height.toFixed(2)}`);
// π« λ°νμ μ€λ₯: toFixedλ ν¨μκ° μλ
any: μ λ€λ¦
interface UserData {
id: string;
name: string;
height: string;
}
interface DataProvider<T> {
fetchData: (userId: string) => T;
}
class UserApi implements DataProvider<UserData> {
private data: UserData | undefined;
constructor() {
this.data = undefined;
}
setData(data: UserData) {
this.data = data;
}
fetchData(userId: string) {
// λ€νΈμν¬ μμ²~
// μ€μ μλ리μ€μμλ μ¬μ©μ IDλ₯Ό κΈ°λ°μΌλ‘ APIμμ λΉλκΈ°λ‘ μ¬μ©μ λ°μ΄ν°λ₯Ό κ°μ Έμ¬ κ²
return {
id: userId,
name: "κΉμ μ ",
height: "180.5",
};
}
}
const userApi = new UserApi();
const fetchedUserData = userApi.fetchData("μ μ μμ΄λ");
console.log(`User Height: ${fetchedUserData.height.toFixed(2)}`);
// π« λ°νμ μ€λ₯: toFixedλ ν¨μκ° μλ
/ νμ¨ μ 보λ₯Ό νννλ μΈν°νμ΄μ€
interface ExchangeRate {
currency: string;
rate: number;
}
// λ¬λ¬λ₯Ό μνλ‘ νμ νλ ν¨μ
const exchangeDollarToWon: (amount: number, exchangeRate: ExchangeRate) => number =
(amount: number, exchangeRate: ExchangeRate): number => {
if (typeof amount !== 'number' || typeof exchangeRate.rate !== 'number') {
// μ«μκ° μλ μΈμκ° μ λ¬λλ©΄ μ€λ₯ λ°μ
throw new Error('Invalid input: arguments must be numbers');
}
return amount * exchangeRate.rate;
};
// λ¬λ¬λ₯Ό μ λ‘λ‘ νμ νλ ν¨μ
const exchangeDollarToEuro: any = (amount: number, exchangeRate: ExchangeRate): number => {
return amount * exchangeRate.rate;
};
// νμ μ 보
const dollarToWonRate: ExchangeRate = { currency: 'KRW', rate: 1150 };
const dollarToEuroRate: ExchangeRate = { currency: 'EUR', rate: 0.85 };
// νμ ν¨μ νΈμΆ
console.log(exchangeDollarToWon(100, dollarToWonRate)); // 115000
console.log(exchangeDollarToEuro(100, dollarToEuroRate)); // 85
console.log(exchangeDollarToEuro("100", dollarToEuroRate)); // "100" λ¬Έμμ΄μ΄ λ°νλ¨