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/7: Generics #10

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

Coding devil/7: Generics #10

wants to merge 7 commits into from

Conversation

nyun-nye
Copy link
Collaborator

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

✍ Coding devil/7: Generics

πŸ“Description

  • μ œλ„€λ¦­ ν•¨μˆ˜
    • class, function, interfaceλ₯Ό λ‹€μ–‘ν•œ νƒ€μž…μœΌλ‘œ 재 μ‚¬μš©ν•  수 μžˆλ‹€.
    • μ„ μ–Έν•  λ•ŒλŠ” νƒ€μž… νŒŒλΌλ―Έν„°λ§Œ μž‘μ„±ν•œλ‹€.
    • 생성 μ‹œμ μ— μ‚¬μš©ν•  νƒ€μž…μ„ κ²°μ •ν•œλ‹€.
    • μ‚¬μš© μ˜ˆμ‹œλŠ” μ•„λž˜μ™€ κ°™λ‹€.
       function getSize<T>(arr: T[]):number{
         return arr.length;
       }
       const arr1 = [1,2,3];
       getSize<number|string>(arr1); 
       // νŠΉμ • νƒ€μž…μœΌλ‘œ κ°•μ œν•˜κ³  싢을 λ•Œ <>μ•ˆμ— κ°’ λ„£μ–΄μ„œ μ‚¬μš©
       // λͺ…μ‹œ ν•˜μ§€ μ•ŠμœΌλ©΄ μžλ™μœΌλ‘œ 인식함
      
  • μ œλ„€λ¦­ μΈν„°νŽ˜μ΄μŠ€
    • 객체 ν˜•μ‹μœΌλ‘œ νƒ€μž…μ„ μ„€μ •ν•  수 μžˆλ‹€.
    • 객체의 μ„ΈλΆ€ λ‚΄μš©μ„ μ§€μ •ν•΄μ„œ νƒ€μž…μ„ μ„€μ •ν•  수 μžˆλ‹€.
       interface Mobile<T> {
         name: String;
         price: number;
         option: T; // κ°’ λ―Έμ •
       }
       // const m1: Mobile<object> ={
       const m1: Mobile<{color: string; coupon: boolean}> = {
         name: "s21",
         price: 1000,
         option:{
           color: "red",
           coupon: false,
         },
       }
      
  • μ œλ„€λ¦­ ν™•μž₯
    • extends ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄μ„œ ν™•μž₯ν•  수 μžˆλ‹€.
    • <T extends number | string>은 μ œλ„€λ¦­ Tκ°€ numberλ‚˜ string νƒ€μž…μ΄μ–΄μ•Ό ν•œλ‹€λŠ” μ˜λ―Έμ΄λ‹€.
        interface Book{
          price: number;
        }
        function showName<T extends {name: string}>(data: T): string{ // μ œλ„€λ¦­ Tκ°€ nameν˜•μ‹μ˜ string νƒ€μž…μ΄μ–΄μ•Ό ν•œλ‹€.
          return data.name;
        }
        // showName(book); // bookμ—λŠ” name ν˜•μ‹μ˜ string νƒ€μž…μ΄ μ—†μ–΄ 였λ₯˜κ°€ λ°œμƒν•œλ‹€.
      

πŸ“ŒSummary

  • μ œλ„€λ¦­ ν•¨μˆ˜:

    • λ‹€μ–‘ν•œ νƒ€μž…μ„ μž¬μ‚¬μš©ν•  수 μžˆλ„λ‘ ν•¨μˆ˜, 클래슀, μΈν„°νŽ˜μ΄μŠ€μ—μ„œ ν™œμš©λœλ‹€.
    • μ„ μ–Έ μ‹œ νƒ€μž… νŒŒλΌλ―Έν„°(T)λ₯Ό μ‚¬μš©ν•˜λ©°, ν˜ΈμΆœν•  λ•Œ νƒ€μž…μ„ 지정할 수 μžˆλ‹€.
    • νŠΉμ • νƒ€μž…μ„ λͺ…μ‹œν•  ν•„μš”κ°€ μ—†λ‹€λ©΄, νƒ€μž…μ€ μžλ™μœΌλ‘œ μΆ”λ‘ λœλ‹€.
  • μ œλ„€λ¦­ μΈν„°νŽ˜μ΄μŠ€:

    • 객체의 νƒ€μž…μ„ μœ μ—°ν•˜κ²Œ μ„€μ •ν•  수 μžˆλ„λ‘ 도와쀀닀.
    • optionκ³Ό 같은 속성을 μ œλ„€λ¦­μœΌλ‘œ μ„€μ •ν•˜μ—¬ λ‚˜μ€‘μ— νŠΉμ • νƒ€μž…μœΌλ‘œ 결정될 수 μžˆλ‹€.
  • μ œλ„€λ¦­ ν™•μž₯:

    • extends ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄ μ œλ„€λ¦­μ— νŠΉμ • 쑰건을 λΆ€μ—¬ν•  수 있으며, 이λ₯Ό 톡해 νƒ€μž…μ„ μ œν•œν•˜κ³  ν™•μž₯된 κΈ°λŠ₯을 μ œκ³΅ν•  수 μžˆλ‹€.

@nyun-nye nyun-nye requested a review from 4BFC October 2, 2024 07:03
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