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ν•˜μž. μ–΄μ°¨ν”Ό μΆ”μΆœν•  수 μžˆμœΌλ―€λ‘œ, μ‚¬μš©μžκ°€ μ‚¬μš©ν•˜κΈ° μ‰½κ²Œ λ§Œλ“œλŠ” 것이 μ’‹λ‹€.