Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coding devil/8: Utility Types #11

Open
wants to merge 14 commits into
base: recently
Choose a base branch
from
Open

Coding devil/8: Utility Types #11

wants to merge 14 commits into from

Conversation

nyun-nye
Copy link
Collaborator

@nyun-nye nyun-nye commented Oct 2, 2024

✍ Coding devil/8: Utility Types

πŸ“Description

  • keyof
    • ν‚€ 값을 μΆ”μΆœν•˜λŠ” ν‚€μ›Œλ“œμ΄λ‹€.
    • keyof ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄μ„œ ν‚€ 값듀을 union ν˜•νƒœλ‘œ 받을 수 μžˆλ‹€.
  • Partial<T>
    • ν”„λ‘œνΌν‹°μ˜ νƒ€μž…μ„ λͺ¨λ‘ optional둜 λ³€κ²½ν•΄μ€€λ‹€.
    • μ΄λ ‡κ²Œ 되면 ν”„λ‘œνΌν‹° 쀑 μΌλΆ€λ§Œ μ‚¬μš©ν•˜λŠ” 것이 κ°€λŠ₯해진닀.
    • μΆ”ν›„ interfaceλ₯Ό ν™œμš©ν•  λ•Œ ν™œμš©λ„κ°€ κ°€μž₯ 높을 것 κ°™λ‹€.
  • Required<T>
    • λͺ¨λ“  ν”„λ‘œνΌν‹°λ₯Ό ν•„μˆ˜λ‘œ λ³€κ²½ν•΄μ€€λ‹€.
    • optional둜 μ§€μ •λœ ν”„λ‘œνΌν‹°λ„ ν•„μˆ˜λ‘œ μ‚¬μš©ν•΄μ•Όν•œλ‹€.
    • Partialκ³Ό λ°˜λŒ€λ˜λŠ” κ°œλ…μ΄λ‹€.
  • Readonly<T>
    • λͺ¨λ“  ν”„λ‘œνΌν‹°λ₯Ό 읽기 μ „μš©μœΌλ‘œ λ³€κ²½ν•΄μ€€λ‹€.
    • ν”„λ‘œνΌν‹°μ— 값을 ν• λ‹Ήν•˜λŠ” κ²ƒλ§Œ κ°€λŠ₯ν•˜κ³  μˆ˜μ •μ΄ λΆˆκ°€λŠ₯ν•˜λ‹€.
  • Record<K,T>
    • μ—¬κΈ°μ„œ KλŠ” keyλ₯Ό μ˜λ―Έν•˜κ³ , TλŠ” type을 μ˜λ―Έν•œλ‹€.
    • 킀와 값에 λŒ€ν•œ νƒ€μž…μ„ 지정해쀀닀.
  • Pick<T, K>
    • μ—¬κΈ°μ„œ KλŠ” keyλ₯Ό μ˜λ―Έν•˜κ³ , TλŠ” type을 μ˜λ―Έν•œλ‹€.
    • T νƒ€μž…μ—μ„œ K ν”„λ‘œνΌν‹°λ§Œ κ³¨λΌμ„œ μ‚¬μš©ν•  수 μžˆλ‹€.
  • Omit<T,K>
    • T νƒ€μž…μ—μ„œ K ν”„λ‘œνΌν‹°λ§Œ μƒλž΅ν•΄μ„œ μ‚¬μš©ν•  수 μžˆλ‹€.
    • Pickκ³Ό λ°˜λŒ€λ˜λŠ” κ°œλ…μ΄λ‹€.
  • Exclude<T1, T2>
    • T1μ—μ„œ T2λ₯Ό μ œμ™Έν•˜κ³  μ‚¬μš©ν•œλ‹€.
    • Omitκ³Ό ν˜Όλ™ν•  수 μžˆμ§€λ§Œ Omit은 ν”„λ‘œνΌν‹°λ₯Ό μ œκ±°ν•˜λŠ” 것이고 ExcludeλŠ” νƒ€μž…μ„ μ œκ±°ν•˜λŠ” 것이닀.
  • NonNullable<Type>
    • Nullκ³Ό undefindλ₯Ό μ œμ™Έν•œ νƒ€μž…μ„ μƒμ„±ν•œλ‹€.
        type T1 = string | null | undefined | void;
        type T2 = NonNullable<T1>;
      
      • μœ„ 예제 μ½”λ“œμ—μ„œ T2에 hoverν•˜λ©΄ μ•„λž˜ 사진과 같은 문ꡬ가 λœ¬λ‹€. TypeScriptμ—μ„œ {}λŠ” λͺ¨λ“  값이 될 수 μžˆλŠ” 객체 νƒ€μž…μ„ μ˜λ―Έν•œλ‹€. κ·ΈλŸ¬λ‚˜ nullμ΄λ‚˜ undefinedλŠ” 객체가 μ•„λ‹ˆλ―€λ‘œ, {} & null λ˜λŠ” {} & undefinedλŠ” μ œκ±°λ©λ‹ˆλ‹€. λ”°λΌμ„œ μ›ν–ˆλ˜ λŒ€λ‘œ stringκ³Ό void νƒ€μž…λ§Œ λ‚¨λŠ”λ‹€.
        image

πŸ“ŒSummary

  • keyof: 객체 νƒ€μž…μ˜ 킀듀을 μœ λ‹ˆμ˜¨ νƒ€μž…μœΌλ‘œ λ°˜ν™˜
  • Partial<T>: νƒ€μž… T의 λͺ¨λ“  ν”„λ‘œνΌν‹°λ₯Ό optional둜 λ³€κ²½ν•΄μ£ΌλŠ” μœ ν‹Έλ¦¬ν‹° νƒ€μž…
  • Required<T>: νƒ€μž… T의 λͺ¨λ“  ν”„λ‘œνΌν‹°λ₯Ό ν•„μˆ˜λ‘œ μ„€μ •ν•˜μ—¬, λͺ¨λ“  속성을 λ°˜λ“œμ‹œ μ‚¬μš©ν•˜κ²Œ 함
  • Readonly<T>: νƒ€μž… T의 λͺ¨λ“  ν”„λ‘œνΌν‹°λ₯Ό 읽기 μ „μš©μœΌλ‘œ μ„€μ •ν•˜μ—¬ μˆ˜μ • λΆˆκ°€λŠ₯ν•˜κ²Œ 함
  • Record<K, T>: KλŠ” ν‚€ νƒ€μž…, TλŠ” κ°’ νƒ€μž…μœΌλ‘œ, 킀와 κ°’μ˜ νƒ€μž…μ„ μ§€μ •ν•˜μ—¬ 객체λ₯Ό 생성할 수 있음
  • Pick<T, K>: νƒ€μž… Tμ—μ„œ νŠΉμ • ν‚€ K만 μ„ νƒν•΄μ„œ μ‚¬μš©ν•  수 있게 함
  • Omit<T, K>: νƒ€μž… Tμ—μ„œ νŠΉμ • ν‚€ K만 μ œμ™Έν•˜κ³  μ‚¬μš©ν•  수 있게 함.
  • Exclude<T1, T2>: νƒ€μž… T1μ—μ„œ νƒ€μž… T2λ₯Ό μ œμ™Έν•˜κ³  남은 νƒ€μž…μ„ λ°˜ν™˜
  • NonNullable<Type>: nullκ³Ό undefinedλ₯Ό μ œμ™Έν•œ νƒ€μž…μ„ 생성

@nyun-nye nyun-nye requested a review from 4BFC October 2, 2024 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant