ν¨μ
맀κ°λ³μ(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