함수 타입 정의


함수 타입 정의

// 선택적 매개변수를 주어 선택적으로 사용할 수 있다. function hello(name?:string):string { return `Hello, ${name || "world"}` } const result = hello(); // undefined 가능 const result2 = hello("sam"); // default 값을 줄 수 있다. function hello2(name = "world"):string { return `Hello, ${name}` } // 선택적 매개변수가 필수 매개변수보다 앞에 오면 에러가 발생한다. function hello(age?: number, name: string) :string { if (age !== undefined) { return 'Hello'; } else { return 'hello'; } } // 위 코드를 사용하고 싶으면 이렇게 해야한다. function hello(age: number | undefined, name...


#TypeScript #함수 #함수타입정의

원문링크 : 함수 타입 정의