■ 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 문으로 대체하는 것을 권장한다.
'Language > TypeScript' 카테고리의 다른 글
union(|)과 intersection(&) (0) | 2023.02.09 |
---|---|
enum, keyof, typeof (0) | 2023.02.07 |
원시 래퍼 타입, 템플릿 리터럴 타입, rest, 튜플 (0) | 2023.02.05 |
JS 변환 시 사라지는 부분 (0) | 2023.02.02 |
타입스크립트는 변수, 매개변수, 리턴값에 타입 붙이는 것 (0) | 2023.02.01 |