2024-06-11.md
π‘DIL: μ΄νν°λΈ νμ μ€ν¬λ¦½νΈ
μ€ν°λ: μκ° CS, https://github.com/monthly-cs/2024-05-effective-typescript
μμ±μΌ: 2024-06-11
μμ±μ: dusunax
μμ΄ν 47: κ³΅κ° APIμ λ±μ₯νλ λͺ¨λ νμ μ μ΅μ€ν¬νΈνκΈ°
- μλνν° λͺ¨λμμ μ΅μ€ν¬νΈ λμ§ μμ νμ μ λ³΄κ° νμν κ²½μ°, νμ κ° λ§€ν λꡬλ₯Ό μ¬μ©ν΄μ μ°Έμ‘°νλ λ°©λ²μ μ°Ύμ μ μλ€.
- λΌμ΄λΈλ¬λ¦¬ μ μμλ νλ‘μ νΈ μ΄κΈ°μ νμ
μ΅μ€ν¬νΈλ₯Ό μμ±ν΄μΌ νλ€.
- ν¨μ μ μΈμ νμ μ λ³΄κ° μλ€? νμ μ λ³΄κ° export λκ³ μλ κ²
interface SecretName {
first: string;
last: string;
}
interface SecretSanta {
name: SecretName;
gift: string;
}
export function getGift(name: SecretName, gift: string): SecretSanta {
// ν¨μλ§ export
// ...
}
- ν¨μ μκ·Έλμ²μμ νμ
μ μΆμΆν΄λΌ μ μλ€.
- Parameters, ReturnType μ λλ¦
type MySanta = ReturnType<typeof getGift>; // λ¦¬ν΄ νμ
// ^? type MySanta = SecretSanta
type MyName = Parameters<typeof getGift>[0]; // νλΌλ―Έν°
// ^? type MyName = SecretName
Things to Remember
- Export types that appear in any form in any public method. Your users will be able to extract them anyway, so you may as well make it easy for them.
- κ³΅κ° λ©μλμ νμ μ exportνμ. μ΄μ°¨νΌ μΆμΆν μ μμΌλ―λ‘, μ¬μ©μκ° μ¬μ©νκΈ° μ½κ² λ§λλ κ²μ΄ μ’λ€.