void 타입

배병일 ㅣ 2023. 8. 3. 20:41

1번 코드

const a = (a: number, b: number) => a + b

const b = a(1, 2)

console.log(b) // 3

 

2번 코드

const a = (a: number, b: number) => {
  console.log(a + b); // 3
};

const b = a(1, 2);

console.log(b); // undefined

 

1번 코드와 2번 코드의 차이점을 보면 

 

b라는 상수에 값이 반환 됐는지 안됐는지 차이라는 걸 알 수 있다.

 

2번 코드 처럼 함수의 결과 값이 반환되지 않는 경우 void 타입이다.

( void 는 거의 생략가능 )

'TypeScript' 카테고리의 다른 글

다향성 ( Polymorphyism ), 제네릭 ( Generic )  (0) 2023.08.04
오버로딩 ( Overloading )  (0) 2023.08.03
함수의 call signature  (0) 2023.08.03
type 정해주는 법  (0) 2023.08.03
TypeScript 참고 자료  (0) 2023.08.01