πŸ‘‰ Typescript

  1. ν•¨μˆ˜

    λ§€κ°œλ³€μˆ˜(parameter)의 νƒ€μž…
    : λ§€κ°œλ³€μˆ˜μ˜ 경우, λ³€μˆ˜μ˜ νƒ€μž…μ„ ν‘œκΈ°ν•  λ•Œμ™€ λ§ˆμ°¬κ°€μ§€λ‘œ λ§€κ°œλ³€μˆ˜ 뒀에 콜둠(:)을 뢙이고 νƒ€μž…μ„ μ λŠ”λ‹€. (param1: number)
    λ°˜ν™˜κ°’(return value)의 νƒ€μž…
    : λ°˜ν™˜ νƒ€μž…μ€ λ§€κ°œλ³€μˆ˜ λͺ©λ‘μ„ λ‹«λŠ” κ΄„ν˜Έ())와 ν•¨μˆ˜ 본문을 μ—¬λŠ” μ—¬λŠ” λŒ€κ΄„ν˜Έ({) 사이에 μ½œλ‘ μ„ 뢙이고 ν‘œκΈ°ν•œλ‹€. (function (): number { ... })

    ex) 두 숫자λ₯Ό λ°›μ•„ κ·Έ 합을 λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜

    function sum(a: number, b: number): number {
      return (a + b);
    }

    ex) ν•¨μˆ˜κ°€ μ•„λ¬΄λŸ° 값도 λ°˜ν™˜ν•˜μ§€ μ•Šκ³  μ’…λ£Œλœλ‹€λ©΄ λ°˜ν™˜ νƒ€μž… void

    function logGreetings(name: string): void {
      console.log(`Hello, ${name}!`);
    }

    void λ°˜ν™˜ νƒ€μž…μ„ κ°–λŠ” ν•¨μˆ˜κ°€ undefinedλ‚˜ null μ΄μ™Έμ˜ 값을 λ°˜ν™˜ν•˜λ©΄ νƒ€μž… μ—λŸ¬κ°€ λ°œμƒν•œλ‹€. voidκ°€ μ•„λ‹Œ λ°˜ν™˜ νƒ€μž…μ„ κ°–λŠ” ν•¨μˆ˜κ°€ 아무 값도 λ°˜ν™˜ν•˜μ§€ μ•ŠλŠ” κ²½μš°λ„ λ§ˆμ°¬κ°€μ§€λ‹€.

    function notReallyVoid(): void {
      return 1;
    }
    // error TS2322: Type '1' is not assignable to type 'void'.
    
    function actuallyVoid(): number { }
    // error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value