본문 바로가기
Language/TypeScript

never 타입과 느낌표

by Zayne 2023. 2. 4.

■ never 타입

try {
  const array = []; // noImplicitAny가 false일 때
  array[0];
} catch(error) {
  error;
}

타입스크립트에서는 빈배열의 경우 never[] 타입이 할당된다. (never 좋은 설명 글)

 

 느낌표

const head = document.querySelector('#head')!;
console.log(head);

const head = document.querySelector('#head');
if (head) {
  console.log(head);
}

null 이 존재할 수 있는 값에 느낌표(!)를 붙이면 null 타입이 사라진다.

느낌표(!)는 값이 항상 있음을 개발자가 보증하는 것이므로 위험

느낌표(!) 대신 if 문으로 대체하는 것을 권장한다.