[TypeScript] narrowing 4가지 방법

jm4293·2023년 2월 5일
0
// index.html
<h4 id="title">안녕하세요</h4>
<a href="naver.com">링크</a>
<button id="button">버튼</button>

<script src="변환된 자바스크립트파일.js"></script>

let 제목 = document.querySelector('#title');
if (제목 != null) {
  제목.innerHTML = '반갑습니다'
}
let 제목 = document.querySelector('#title');
if (제목 instanceof HTMLElement) {
  제목.innerHTML = '반갑습니다'
}
let 제목 = document.querySelector('#title') as HTMLElement;
제목.innerHTML = '반갑습니다'
let 제목 = document.querySelector('#title');
if (제목?.innerHTML != undefined) {
  제목.innerHTML = '반갑습니다'
}
profile
FE | Node.js Developer

0개의 댓글